给定每行包含许多数据字段的列表视图,如何使用onClickListener()仅从行中分隔一个字段(id)

时间:2011-11-09 03:58:20

标签: android

如果listview每行包含许多数据字段,我们如何才能将onClickListener()

的行中的一个字段(例如id)分开
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
                Object dataRow = listView.getItemAtPosition(position);
                Log.e("hi, this is the full row of data, i just want 1 of the fields", dataRow);
            }

1 个答案:

答案 0 :(得分:1)

这可以通过自定义listview将BaseAdapter扩展到类来实现..在类中你可以

在oncreate中:

    listview.setAdapter(new newListview(this);  //listview is the object of ListView in xml file              

在onCreate之外,newListview是活动的内部类

    class newListview extends BaseAdapter{
    private Context context;


    public PrayerList(Context context) {
    this.context=context;


    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub

             return 0;// here return size of listview
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }

             @Override
    public View getView(final int position, View view, ViewGroup viewgroup) {


            ViewHolder holder = null;
            LayoutInflater inflater =  LayoutInflater.from(context);
         if (view == null) 
         {

             view = inflater.inflate(R.layout.mydatalistview, null);
            holder = new ViewHolder();
            holder.txtViewTitle = (TextView) view.findViewById(R.id.title);
            holder.txtViewDescription = (TextView) view.findViewById(R.id.description);
            holder.arrowImage=(ImageView)view.findViewById(R.id.arrow_imageview);
            view.setTag(holder);
         }
         else
         {
             holder = (ViewHolder) view.getTag();
         }
              view.setOnClickListener(new OnClickListener() {

                 @Override
                 public void onClick(View v)
                 {
                                 //here you can use onclick of particular item of listview
                           }

             }
     class ViewHolder{

    TextView txtViewTitle;
    TextView txtViewDescription;
    ImageView arrowImage;
}