Android:Listview数据未在另一个活动中传递

时间:2016-08-05 18:14:23

标签: android listview

我制作了一个listview并从数据库中对其进行了充实。我使用以下命令在listview项目上设置了onclicklistener

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_books);
    listView = (ListView) findViewById(R.id.listView);
    call=(Button)findViewById(R.id.call);
    userDbHelper = new UserDbHelper(getApplicationContext());
    sqLiteDatabase = userDbHelper.getReadableDatabase();
    cursor = userDbHelper.getInformation(sqLiteDatabase);
    listDataAdapter = new ListDataAdapter(getApplicationContext(), R.layout.row_layout);
    final TextView txt=(TextView) findViewById(R.id.pr);
    listView.setAdapter(listDataAdapter);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
           Intent in=new Intent(Books.this,List1.class);
            TextView txt=(TextView)findViewById(R.id.pr);
            String getPrice=txt.getText().toString();
            in.putExtra("getPrice",getPrice);
           startActivity(in);
        }
    });

    if (cursor.moveToFirst()) {
        do {
            id = cursor.getInt(0);
            category = cursor.getString(1);
            name = cursor.getString(2);
            phone = cursor.getString(3);
            price = cursor.getString(4);
            desc = cursor.getString(5);
            photo = cursor.getBlob(6);

            //Bitmap bmp = BitmapFactory.decodeByteArray(photo, 0, photo.length);
            DataProvider dataProvider = new DataProvider(id,category, name, phone, price, desc, photo);
            listDataAdapter.add(dataProvider);
        } while (cursor.moveToNext());
    }
}

并在List1.class我试图从点击了listview的数据库中检索id,这样我就可以根据它来扩充其他布局。怎么做到呢?
List1.class

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list1);
      TextView txt=(TextView)findViewById(R.id.pr);
      String getPrice=txt.getText().toString();
      userDbHelper = new UserDbHelper(getApplicationContext());
      sqLiteDatabase = userDbHelper.getReadableDatabase();
      ImageView img1=(ImageView)findViewById(R.id.image2);
      Intent in=getIntent();
      Cursor cursor=userDbHelper.getContact(getPrice,sqLiteDatabase);
      if(cursor.moveToFirst())
      {
          String NAME=cursor.getString(0);
          String PHONE=cursor.getString(1);
          String DESC=cursor.getString(2);
          byte[] PHOTO=cursor.getBlob(3);
          Bitmap bmp = BitmapFactory.decodeByteArray(photo, 0, photo.length);
          img1.setImageBitmap(bmp);
      }
    userDbHelper=new UserDbHelper(getApplicationContext());
    sqLiteDatabase=userDbHelper.getReadableDatabase();
      price1=(TextView)findViewById(R.id.Price);

}

2 个答案:

答案 0 :(得分:1)

设置要传递的ListView ID / Position:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
   @Override
      public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

       Intent in=new Intent(Books.this,List1.class);
         ///String text = (String) lv.getItemAtPosition(i); //Or you can get the text for the List item you clicked on. 
         in.putExtra("ID_VAR", i); /// ID clicked
       startActivity(in);
     }
 });

获取onCreate

中的数据
Intent mIntent = getIntent();
int intValue = mIntent.getIntExtra("ID_VAR", 0);
System.out.println("Debugging:" + intValue); ///Debugging

答案 1 :(得分:0)

启动意图本身不会添加数据。您必须将数据显式添加到intent中。 这可以通过putExtra方法完成。该方法需要两个参数首先是一个通常是键的字符串。您将使用此密钥检索数据。其他参数是一个值,可以是String,int,long,boolean等。 以下是值为字符串类型的示例。

in.putExtra("key","value");

在你的情况下。在onItemClick方法中,提取Contact对象并将其数据与意图一起传递,如

in.putExtra("id",contact.getId());

在onCreate方法的List1活动中,您可以像这样获取ID

Intent in = getIntent();
int id = in.getIntExtra("id",-1);

这里-1是默认值。另请注意,我们使用的是用于将数据放入intent中的相同密钥,同时检索它是“id”。然后,您可以从数据库中查询数据。