将listview的行id设置为android中数据库的主键列

时间:2015-03-17 12:17:57

标签: android sqlite listview android-listview cursor

我在android中有一个listview,它显示了数据库中的数据列表,当我选择任何一行时,它显示了被点击的数据,而不是数据,我想要那个数据的主键id ListView应该显示列表bt onclick它应该获取点击该数据的主键ID

  public void onClick(View v) {

                 Cursor c = db.rawQuery("SELECT * FROM Deltas", null);
                    values=new ArrayList<String>();
                  while(c.moveToNext()){
                       String uname = c.getString(c.getColumnIndex("delta"));
                       if(uname!="")
                       values.add(uname);
                       System.out.println("data"+uname);
                    }

      //fetch all data one by one


       ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
              android.R.layout.simple_list_item_activated_1, values);
             listView.setAdapter(adapter);
}

这是我如何创建listview如何将主键id标记为listview

中的行

1 个答案:

答案 0 :(得分:1)

您需要构建自定义适配器。试试这个:

public class CustomAdapter extends BaseAdapter {

        private Context mContext;
        private List<Pair<Integer, String>> mData;

        public CustomAdapter(Context context, List<Pair<Integer, String>> data) {
            this.mContext = context;
            this.mData = data;
        }

        @Override
        public int getCount() {
            return mData.size();
        }

        @Override
        public Pair<Integer, String> getItem(int position) {
            return mData.get(position);
        }

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

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

            if(view == null)
                view = LayoutInflater.from(mContext).inflate(android.R.layout.simple_list_item_activated_1, parent, false);

            String text = getItem(position).second;

            ((TextView) view).setText(text);

            return view;
        }
}

在您的活动中,请改为:

 List<Pair<Integer, String>> values;

public void onClick(View v) {

             Cursor c = db.rawQuery("SELECT * FROM Deltas", null);
                values=new ArrayList<Pair<Integer, String>();
              while(c.moveToNext()){
                   String uname = c.getString(c.getColumnIndex("delta"));
                   int id = c.getInt(c.getColumnIndex("id"));
                   if(uname!="")
                   Pair<Integer, String> pair = new Pair<Integer, String>(id, uname);

                   values.add(pair);
                   System.out.println("data"+uname);
              }
              c.close();

      //fetch all data one by one
      CustomAdapter adapter = new CustomAdapter(MainActivity.this, values);
      listView.setAdapter(adapter);
   }

然后得到你的东西:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    CustomAdapter adapter = (CustomAdapter)parent.getAdapter();
                    Pair<Integer, String> pair = adapter.getItem(position);
                    Log.e("the information you want","id:" pair.first + " name:" + pair.second);
                }
            });