gpt4 book ai didi

android - 仅使用一行 listview 中的 imageview 来启动一个新的 intent

转载 作者:行者123 更新时间:2023-11-30 03:41:27 25 4
gpt4 key购买 nike

我正在尝试使 listview 连续包含多个项目,其中包含 imageview 和 textview 。现在我只想访问 ListView 一行的 ImageView ,然后通过单击它来使用它启动一个新的 Intent 。这是我的主要 Activity :

public class MTCRichGraphicsActivity extends Activity {
int ELEMENT_COUNT = 1;
int position=0;
ImageButton bAddView;
ImageView bEventPage;
int r =0 ;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bAddView = (ImageButton) findViewById(R.id.bNewEvent);
bEventPage = (ImageView) findViewById(R.id.ivEvent);
final ListView list = (ListView) findViewById(R.id.list3d);

bAddView.setOnClickListener(new View.OnClickListener() {
List<String> element = new ArrayList<String>();

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.bNewEvent :
//String[] elements = new String[ELEMENT_COUNT];
//for (int i = 0; i< ELEMENT_COUNT; i++) {
//elements[i] = String.valueOf(i);
element.add(String.valueOf(ELEMENT_COUNT++));
//}
final MyAdapter adapter = new MyAdapter(MTCRichGraphicsActivity.this,element);
//list.setScrollY(currentPosition);
//list.setTranslationY(currentPosition);
list.setDivider( null );
list.setAdapter(adapter);
list.setTranscriptMode(1);
list.setStackFromBottom(true);
//ELEMENT_COUNT = ELEMENT_COUNT + 1;
adapter.notifyDataSetChanged();
}

}
});

list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View v, int position,
long id) {
// TODO Auto-generated method stub

bEventPage.setFocusable(false);
bEventPage.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent("de.inovex.mtc.EventsPage");
startActivity(i);
}
});

}

});

}

现在这里 bEventPage 是一个 ImageView ,当我单击任何 ListView 行时,整行都会突出显示并给我一个空指针异常,但未单击 ImageView 并且新 Activity 不会启动。这是我的适配器类:

public class MyAdapter extends BaseAdapter {

private final LayoutInflater mInflater;
private final List<String> mItems;
TextView t;
ViewHolder holder;
public MyAdapter(Activity c,List<String> objects) {
mInflater = c.getLayoutInflater();
mItems = objects;
}

public int getCount() {
return mItems.size();
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView t;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.listitem, parent,false);
}
holder = new ViewHolder();
holder.t1 = (TextView) convertView.findViewById(R.id.textView1);
holder.t2 = (TextView) convertView.findViewById(R.id.textView2);
holder.t3 = (TextView) convertView.findViewById(R.id.textView3);
holder.t4 = (TextView) convertView.findViewById(R.id.textView4);
holder.bEventPage = (ImageView) convertView.findViewById(R.id.ivEvent);
holder.t1.setText("Title"+position);


//((ImageView)convertView).setTextAlignment(1);

return convertView;
}

@Override
public Object getItem(int position) {
return mItems.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

那么请告诉我如何只使用一行的 ImageView 根据它在 ListView 中的位置开始一个新的 Intent ?

最佳答案

将监听器添加到适配器中的 ImageView ..并将其从列表项监听器中删除

Activity activity;

public MyAdapter(Activity c,List<String> objects) {
mInflater = c.getLayoutInflater();
mItems = objects;
activity = c;
}

public View getView(int position, View convertView, ViewGroup parent) {
TextView t;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.listitem, parent,false);
}
holder = new ViewHolder();
holder.t1 = (TextView) convertView.findViewById(R.id.textView1);
holder.t2 = (TextView) convertView.findViewById(R.id.textView2);
holder.t3 = (TextView) convertView.findViewById(R.id.textView3);
holder.t4 = (TextView) convertView.findViewById(R.id.textView4);
holder.bEventPage = (ImageView) convertView.findViewById(R.id.ivEvent);
holder.t1.setText("Title"+position);


holder.bEventPage.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent("de.inovex.mtc.EventsPage");
activity.startActivity(i);
}
});

return convertView;
}

关于android - 仅使用一行 listview 中的 imageview 来启动一个新的 intent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15638107/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com