我已经完成了自定义列表视图 它每行有6个textview和1个图像视图 什么是按下项目它只是进入我的第二个活动 但它想要的是将行的项目传递给第二个活动 我不知道该怎么做或在列表或适配器中添加它的位置 还有在第二个活动中做什么
这是我的第一个活动
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);
}
});
}
}
这是适配器
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;
}
}
答案 0 :(得分:1)
据我所知,您希望传输整个对象,即您单击的列表项的ImageUpload。为此,您可以将ImageUpload模型类创建为Parcelable,然后在您的意图中传递选定的ImageUpload对象,并使用getParcelable()从Intent中检索它。
列表项上的单击:
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);
}
});
在Buy1Activity上,在onCreate()中获取所选的ImageUpload对象:
ImageUpload imgUploadObj = getIntent().getExtras().getParcelable("selected_image_upload");
您可以从here获取提示以创建Parcelable类。
答案 1 :(得分:0)
试试这个
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);
}
});
在您的Buy1Activity中
@Override
protected void onCreate(Bundle savedInstanceState) {
ImageUpload img = (ImageUpload) getIntent().getParcelableArrayListExtra("parcel_data");
}
希望有所帮助!