0
所以我有一個自定義適配器的listView,我希望每當我按下按鈕,我可以從MainActivity的OnItemClickListener監聽它。如何通過listView中的按鈕訪問主Activity中的數據?
所以我的問題是,當我點擊ListView中的按鈕時,我會傳遞什麼,以便我可以從主Activity讀取它?
@Override
public View getView(final int pos, View convertView, ViewGroup parent) {
Holder holder = new Holder();
final CustomProductAdapter p=this;
final View rowView;
rowView = inflater.inflate(R.layout.product_list_test, null);
holder.productView=(TextView) rowView.findViewById(R.id.productView);
holder.descView=(TextView) rowView.findViewById(R.id.descView);
holder.imageView=(ImageView) rowView.findViewById(R.id.imageView);
holder.productCost=(TextView) rowView.findViewById(R.id.productCost);
holder.iButton=(ImageButton) rowView.findViewById(R.id.imageButton);
holder.iButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//What will I put here?
}
});
holder.productView.setText(proNames[pos]);
holder.descView.setText(R.string.description_product);
holder.productCost.setText("0.00");
ImageView image =holder.imageView;
imageloader.DisplayImage(images[pos],image);
rowView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "You Clicked "+proNames[pos], Toast.LENGTH_SHORT).show();
}
});
return rowView;
}
,這樣我可以從這裏像這樣讀它在我的MainActivity.java(我不知道這是否是corect方式)
listView = (ListView)findViewById(R.id.listView);
listView.setAdapter(new CustomProductAdapter(this, images, proNames1, desc));
AdapterView.OnItemClickListener mMessageClickedHandler = new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
// Do something in response to the click
long ids=v.getId();
if(ids==R.id.imageButton){
Toast.makeText(getBaseContext(),"Button clicked at "+position,Toast.LENGTH_SHORT).show();
}
}
};
listView.setOnItemClickListener(mMessageClickedHandler);
。我是初學者,所以請幫助我,如果我不正確。
感謝您的意見。 – theSereneRebel