3

这是ShowData.java我设置自定义列表视图的活动..现在我需要单击该列表视图上的视图并需要显示另一个活动但会强制关闭..它会引发错误 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 ...???谢谢...

4

1 回答 1

4

在您的 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);
            }
        });
于 2012-09-04T07:55:52.007 回答