我有一个由我的MainActivity实现的ListFragmentA(它也覆盖onListItemClick并且是必需的)。 CustomArrayAdapter有两个图像和3个textViews。一切正常,除非我制作我的一个图像“Clickable = onEditDelete”。在ListFragmentA中找不到onEditDelete方法,但在将方法移动到MainActivity时找到了IS。
我想了解为什么会这样?一旦图像被点击,它是否不再属于ListFragment?
如果方法只能在MainActiviy中找到,那么获取单元格中单击图像索引的最佳方法是什么?
- 感谢你。
public class MainActivity extends BaseActivity {
public void goEditDelete(View ed) {
Log.d("myLog", "edit ******************* Item prod clicked: ");
}
}
public class CustomListViewAdapter extends ArrayAdapter<RowItem> {
Context context;
public CustomListViewAdapter(Context context, int resourceId, List<RowItem> items) {
super(context, resourceId, items);
this.context = context;
}
/*private view holder class*/
private class ViewHolder {
ImageView imageView;
TextView txtTitle;
TextView txtSubTitle;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
RowItem rowItem = getItem(position);
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.row, null);
holder = new ViewHolder();
holder.txtTitle = (TextView) convertView.findViewById(R.id.row_title);
holder.txtSubTitle = (TextView) convertView.findViewById(R.id.row_subTitle);
holder.imageView = (ImageView) convertView.findViewById(R.id.row_icon); // make clickable ****************
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();
holder.txtSubTitle.setText(rowItem.getsubTitle());
holder.txtTitle.setText(rowItem.getTitle());
holder.imageView.setImageResource(rowItem.getImageId());
return convertView;
}
}
public class MainView extends SherlockListFragment {
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// chekc data before hand
Log.e("myLog", "SAVED Data1 ListSize = " + myProdArrayHash.size());
for (int i = 0; i < myProdArrayHash.size(); i++) {
Log.e("myLog", "SAVED DataLoooop = "+ myProdArrayHash.get(i).toString());
}
//
// Keys used in Hashmap
String[] from = {"editIcon", "prod", "dist", "repEmail","repPhone"};
// Ids of views in listview_layout
int[] to = {R.id.row_icon, R.id.row_title, R.id.row_subTitle, R.id.tvRepEmail, R.id.tvRepCell};
// Instantiating an adapter to store each items
adapter = new SimpleAdapter(getActivity(), myProdArrayHash, R.layout.row, from, to);
// Getting a reference to listview of main.xml layout file
setListAdapter(adapter);
// double heck
Log.d("myDataCheck_storedItems", " sizeOfCurrentData() MainViewFrag onStart() END= " + sizeOfCurrentData());
}
}
答案 0 :(得分:1)
在xml中声明的单击方法总是转到活动。从来没有碎片。多数民众赞成如何设计。
您必须手动将事件转发到您的片段。在onClick-method活动中(无论你在xml中声明了什么),都可以对你的片段进行反馈并在那里调用一个公共方法。
请参阅:How to handle button clicks using the XML onClick within Fragments