如何从列表视图中单击一行从列表视图中获取特定列值?

时间:2013-08-23 07:16:49

标签: android listview

我有一个列表视图,其中我有四到五列。列表视图的值是通过数据库查询分配的。列表视图使用SimpleCursorAdapter创建。我试图获取列表视图的第一列的值...但是我无法检索它。我怎么做??? 这是我的自定义适配器

private class MyCursorAdapter extends SimpleCursorAdapter
{

    @SuppressWarnings("deprecation")
    public MyCursorAdapter(Context context, int layout, Cursor c,
            String[] from, int[] to,int flags) {
        super(context, layout, c, from, to);
        // TODO Auto-generated constructor stub
    }

     @Override 
      public View getView(int position, View convertView, ViewGroup parent) { 

       //get reference to the row
       View view = super.getView(position, convertView, parent);
       //check for odd or even to set alternate colors to the row background
       if(position % 2 == 0){ 
        view.setBackgroundColor(Color.rgb(238, 233, 233));
       }
       else {
        view.setBackgroundColor(Color.rgb(255, 255, 255));
       }
       return view; 
      } 


}

这里是我将我的适配器分配给listview

的地方
MyCursorAdapter myDataAdapter=new MyCursorAdapter(this,R.layout.list_row_layout,cursor, columns, to, 0);

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

    listView.addHeaderView(getLayoutInflater().inflate(R.layout.list_header, null, false));

    listView.setAdapter(myDataAdapter);

这是onclick监听器

listView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                long arg3) {
            // TODO Auto-generated method stub
            //View view= 
            String ttsl_can=(( listView.getItemAtPosition(position).toString()));
            Toast.makeText(PendingActivity.this, ttsl_can, Toast.LENGTH_LONG).show();
        }
    });

单击列表视图的特定行,获取响应为 android.database.SQLiteCursor@40d2f13d

我如何得到响应作为第一列的值,我在哪一行点击? 我在代码中做了什么改变才能实现呢???

请帮助!!在此先感谢!!

1 个答案:

答案 0 :(得分:0)

尝试这样:

    listView.setOnItemClickListener(new OnItemClickListener() {

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

            Cursor cursor = listView.getItemAtPosition(position);

            // Here you have to getString using index
            String str = cursor.getString(<you column index>);

            Toast.makeText(PendingActivity.this, str, Toast.LENGTH_LONG).show();
        }
    });