對於EfficientAdapter我需要在onListItemClick下使用哪些代碼來獲取所選項目的文本? 我想:EfficientAdapter並獲取點擊文本的項目
str=(String) ((TextView)l.getItemAtPosition(position)).getText()
但這隻會帶來CastException,因爲它取了的LinearLayout視圖持有的TextView和ImageView的(見代碼here)
請幫幫忙!
某些代碼:
public class Bookmarks extends ListActivity {
public static Typeface mFace;
public EfficientAdapter eff;
private static class EfficientAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public EfficientAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return DAT.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item_text, null);
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.text);
holder.text.setTypeface(mFace);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text.setText(DAT[position]);
return convertView;
}
static class ViewHolder {
TextView text;
}
}
public Object getItem(int position) {
return position;
}
public void onResume(Bundle icicle) {
eff.notifyDataSetChanged();
}
protected void onListItemClick(ListView l, View v, int position, long id) {
//////////CRASHES on next line!!!!!!!!!!!!!!!!!!
TextView tv = (TextView) l.getItemAtPosition(position);
String str = tv.getText().toString();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
eff = (new EfficientAdapter(this));
setListAdapter(eff);
registerForContextMenu(getListView());
}
}
如果您發佈了點擊處理程序的完整代碼,尤其是類和函數定義,這將有所幫助。 – 2010-12-24 01:23:25