gpt4 book ai didi

android-listview - 自定义 ListView 中的 setOnItemClickListener

转载 作者:行者123 更新时间:2023-11-30 23:55:30 33 4
gpt4 key购买 nike

这是事件 ShowData.java 我正在其中设置自定义 ListView 。现在我需要单击该 ListView 上的 View 并需要显示另一个事件但会强制关闭。它会抛出错误在 listview.setAdapter(adapter);

public class ShowData extends Activity
{

ListView listview;
Intent intent;
ArrayList<BeanClass> datalist;
CustomAdapter adapter;


@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);


listview = (ListView) findViewById(R.id.listView1);

Intent intent = getIntent();
datalist = (ArrayList<BeanClass>) intent.getSerializableExtra("list");
Log.i("Size of ArrayList", "is " +datalist.size());

adapter = new CustomAdapter(ShowData.this, R.layout.customrow , datalist);

listview.setAdapter(adapter);
listview.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3)
{
Intent i = new Intent(ShowData.this,DetailActivity.class);
i.putExtra("datalist", datalist);
i.putExtra("position", arg2);
startActivity(i);
}
}
}

这是 CustomAdapter.java

public class CustomAdapter extends ArrayAdapter<BeanClass>
{
Context context;
int layoutResourceId;
BeanClass bc;
ArrayList<BeanClass> vbc;

public CustomAdapter(Context context, int layoutResourceId, ArrayList<BeanClass> vbc)
{
super(context, layoutResourceId, vbc);
this.context=context;
this.layoutResourceId=layoutResourceId;
this.vbc=vbc;
}

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
StringReaderHolder holder;

if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent,false);

holder= new StringReaderHolder();


holder.mytext1 = (TextView) row.findViewById(R.id.textView1);
holder.mytext2 = (TextView) row.findViewById(R.id.textView2);
holder.mytext3 = (TextView) row.findViewById(R.id.textView3);

row.setTag(holder);
}
else
{
holder = (StringReaderHolder) row.getTag();
}

BeanClass bsObject = vbc.get(position);

holder.mytext1.setText(bsObject.name);
holder.mytext2.setText(bsObject.address);
holder.mytext3.setText(bsObject.phone);


return row;
}

static class StringReaderHolder
{

TextView mytext1,mytext2,mytext3;

}

}

我应该如何使用 setOnItemClickListener...???谢谢...

最佳答案

在您的 getView 方法中尝试:

row.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(context,DetailActivity.class);
i.putExtra("datalist", vbc);
i.putExtra("position", position);
startActivity(i);
}
});

关于android-listview - 自定义 ListView 中的 setOnItemClickListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12258059/

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