点击后如何显示与列表项位置相关的数据?

时间:2019-06-25 19:24:05

标签: android

我已经使用了意图来传递和检索下一个活动中的位置。但是,当我单击该项目时,显示的数据与单击的项目不相关,它将显示数据库中最后添加的数据。所有项目都相同。所有项目均显示相同的数据,即最后添加的数据。

我试图使用光标显示数据库中的数据

第一个活动:

public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view=inflater.inflate(R.layout.fragment_homefragment,container,false);


    db = new DataBaseHelper(getContext());

    listItem = new ArrayList<String>();
    userList = view.findViewById(R.id.users_list1);

   // viewData();

    listItem.clear();
    userList.setAdapter(null);

    viewData1();
    userList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String yourString = listItem.get(position);

           // String text = userList.getItemAtPosition(position).toString();
            Intent intent= new Intent(view.getContext(),display.class);
            intent.putExtra("KEY", yourString);
            startActivityForResult(intent, 0);
        }
    });





    return view;
}

private void viewData1() {

    cursor = db.viewData();

   // StringBuffer buffer = new StringBuffer();


    if(cursor.getCount() ==0){
        Toast.makeText(getContext(),"no data", Toast.LENGTH_SHORT).show();
    }

    else{



        while (cursor.moveToNext()) {

            //listItem.clear();
            //buffer.append("ID :" + cursor.getString(0) + "\n");
            //buffer.append("Scheme Name :" + cursor.getString(1) + "\n");
            //buffer.append("Date :" + cursor.getString(2) + "\n\n");
           // buffer.append("Latitute:"+cursor.getString(4)+"\n\n");
            //buffer.append("Longitute:"+cursor.getString(5)+"\n\n");

            listItem.add("ID" +cursor.getString(0)+"\n"+"Scheme Name"+cursor.getString(1)+"\n"+"Date:"+cursor.getString(2));
        }
        }


        adapter = new ArrayAdapter(getContext(),android.R.layout.simple_list_item_1,listItem);
        userList.setAdapter(adapter);



       // listItem.clear();
    }


}

下一个活动:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display);
    textView=(TextView) findViewById(R.id.textView5);
    imageView=(ImageView)findViewById(R.id.imageView2);
    mydb = new DataBaseHelper(this);
    Intent intent = getIntent();
    String select = intent.getStringExtra("KEY");
    Toast.makeText(display.this,select,Toast.LENGTH_LONG).show();
    cursor =  mydb.viewData();
   // boolean s= Boolean.parseBoolean(select);




            if (cursor.getCount() == 0) {
                Toast.makeText(display.this, "no data", Toast.LENGTH_SHORT).show();
            } else {
                while (cursor.moveToNext()) {


                    textView.setText(cursor.getString(1));
                    //buffer.append("ID :" + cursor.getString(0) + "\n");
                    //buffer.append("Scheme Name :" + cursor.getString(1) + "\n");
                    //buffer.append("Date :" + cursor.getString(2) + "\n\n");
                    // buffer.append("Latitute:"+cursor.getString(4)+"\n\n");
                    //buffer.append("Longitute:"+cursor.getString(5)+"\n\n");


                        imageView.setImageBitmap(getImageFromBLOB(cursor.getBlob(3)));
                }
            }




}



 public static Bitmap getImageFromBLOB(byte[] mBlob)
{
    byte[] bb = mBlob;
    return BitmapFactory.decodeByteArray(bb, 0, bb.length);
}


}
}

0 个答案:

没有答案