0

i have done custom listview it has 6 textview and 1 image view in each row whati did is when press on item it just goes to my second activity but what it want is to pass the items of the row to the second activity i don't know how to do it or where to add it in the list or in the adapter also what to do in the second activity

here is my first activity

public class ImageListActivity extends AppCompatActivity {

private DatabaseReference mDatabaseRef;
private List<ImageUpload> imgList;
private ListView lv;
private ImageListAdapter adapter;
private ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_image_list);
    imgList = new ArrayList<>();
    lv = (ListView) findViewById(R.id.listViewImage);
    //Show progress dialog during list image loading
    progressDialog = new ProgressDialog(this);
    progressDialog.setMessage("Please wait loading list image...");
    progressDialog.show();

    mDatabaseRef = FirebaseDatabase.getInstance().getReference(ListActivity.FB_Database_Path);



            mDatabaseRef.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    progressDialog.dismiss();

                    //Fetch image data from firebase database
                    for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                        //ImageUpload class require default constructor
                        ImageUpload img = snapshot.getValue(ImageUpload.class);
                        imgList.add(img);
                    }


                    //Init adapter
                    adapter = new ImageListAdapter(ImageListActivity.this, R.layout.image_item, imgList);
                    //Set adapter for listview
                    lv.setAdapter(adapter);
                }


                @Override
                public void onCancelled(DatabaseError databaseError) {

                    progressDialog.dismiss();
                }

            });
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            Intent myintent = new Intent(view.getContext(), Buy1Activity.class);
            startActivityForResult(myintent, 0);


        }
    });


}
}

and this is adapter

public class ImageListAdapter extends ArrayAdapter<ImageUpload> {
private Activity context;
private int resource;
private List<ImageUpload> listImage;

public ImageListAdapter(@NonNull Activity context, @LayoutRes int resource, @NonNull List<ImageUpload> objects) {
    super(context, resource, objects);
    this.context = context;
    this.resource = resource;
    listImage = objects;
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    LayoutInflater inflater = context.getLayoutInflater();

    View v = inflater.inflate(resource, null);
    TextView tvName = (TextView) v.findViewById(R.id.tvImageName);
    TextView tvModel = (TextView) v.findViewById(R.id.tvImageModel);
    TextView tvBrand = (TextView) v.findViewById(R.id.tvImageBrand);
    TextView tvPrice = (TextView) v.findViewById(R.id.tvImagePrice);
    TextView tvyear = (TextView) v.findViewById(R.id.tvImageYear);
    TextView tvDesc = (TextView) v.findViewById(R.id.tvImageDesc);
    ImageView img = (ImageView) v.findViewById(R.id.imgView);

    tvModel.setText(listImage.get(position).getModel());
    tvName.setText(listImage.get(position).getName());
    tvBrand.setText(listImage.get(position).getBrand());
    tvyear.setText(listImage.get(position).getYear());
    tvDesc.setText(listImage.get(position).getDesc());
    tvPrice.setText(listImage.get(position).getPrice());

    Glide.with(context).load(listImage.get(position).getUrl()).into(img);

    return v;

}

}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • Possible duplicate of [How do I pass data between Activities in Android application?](https://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android-application) – riadrifai Jan 03 '18 at 16:46
  • i saw this and more than 25 pages of similar topics and tried them all but i couldn't do it – Momo Mazarita Jan 03 '18 at 16:55
  • and I'm not expert i just a beginner – Momo Mazarita Jan 03 '18 at 16:57
  • You can do what's mentioned in the link for each item or you cold create an object and pass it through the intent. Read more tutorials on how to do it such as: https://www.101apps.co.za/articles/passing-data-between-activities.html , Also mention what problems you faced while trying, this would help us help you out better. – riadrifai Jan 03 '18 at 16:58

2 Answers2

1

What I understand, you want to transfer the entire object namely ImageUpload of the list item you clicked on. For this you can create ImageUpload model class as Parcelable and and then pass selected ImageUpload object in your intent and retrieve it back from Intent using getParcelable().

on list item click:

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        Intent myintent = new Intent(view.getContext(), Buy1Activity.class);
        myintent.putExtra("selected_image_upload", ((ImageUpload) adapter.getItem(position)));
        startActivityForResult(myintent, 0);
    }
});

on Buy1Activity, to get the selected ImageUpload object in onCreate():

ImageUpload imgUploadObj = getIntent().getExtras().getParcelable("selected_image_upload");

You can get hint from here to create a class Parcelable.

MSC
  • 422
  • 5
  • 14
  • thank you for you help , it work fine in getting the text view 's in the second activity but i can't get the image – Momo Mazarita Jan 05 '18 at 12:33
  • Sorry for the delay, I believe the ImageUpload class have an attribute to hold URL. once you have retrieved the ImageUpload object form the intent using getParcelable(), you can use the getter method to get the image URL form object. And load the image from that url. – MSC Jan 08 '18 at 04:07
0

Try this

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        ImageUpload img = adapter.get(position);
        Intent myintent = new Intent(view.getContext(), Buy1Activity.class);
        myintent.putParcelableArrayListExtra("parcel_data", img);
        startActivityForResult(myintent, 0);
    }
});

In your Buy1Activity

@Override
protected void onCreate(Bundle savedInstanceState) {
  ImageUpload img = (ImageUpload) getIntent().getParcelableArrayListExtra("parcel_data");
}

Hope it help!