EfficientAdapter和项目的抢夺文字点击 [英] EfficientAdapter and grabbing text of item clicked
问题描述
有关EfficientAdapter什么code,我需要onListItemClick下要使用获得所选项目的文本?
我想:
For EfficientAdapter What code I need to use under onListItemClick to get text of selected item? I tried:
str=(String) ((TextView)l.getItemAtPosition(position)).getText()
但是,这不仅带来了CastException,因为它取了的LinearLayout视图持有的TextView和ImageView的(见code <一个href=\"http://developer.android.com/intl/de/resources/samples/ApiDemos/src/com/example/android/apis/view/List14.html\"相对=nofollow>这里)
请帮帮忙!
有些code的:
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());
}
}
推荐答案
有没有真正足够的信息,在你的问题(见上面我的意见),但在这里的答案是一个猜测:
There isn't really enough information in your question (see my comment above), but here's a guess at an answer:
首先,的getText()
是的记载返回的CharSequence,而不是字符串。它的会返回一个字符串,但你不知道它。因此,这将是更加安全的写:
First, getText()
is documented to return a CharSequence, not a String. It might return a String, but you don't know that it does. So it would be safer to write:
str = ((TextView)l.getItemAtPosition(position)).getText().toString()
第二,如果还是不行,请尝试打破这种说法,让你可以在异常是来自一个更好的想法。像这样的东西,也许,可能是更清晰的:
Second, if that doesn't work, try breaking down that statement so you can get a better idea of where the exception is coming from. Something like this, perhaps, might be clearer:
TextView tv = (TextView) l.getItemAtPosition(position);
str = tv.getText().toString();
修改根据您的更新:
1)如果你打算实施 onListItemClick
,要确保你通过调用基类开头的方法,如下图所示。
1) If you're going to implement onListItemClick
, be sure that you begin the method by calling up to the base class, as shown below.
2)这里的问题:(我意识到这在一个不同的例子,我认为不会是现在有必要)复制和粘贴后:<一href=\"http://developer.android.com/reference/android/widget/AdapterView.html#getItemAtPosition%28int%29\"相对=nofollow> ListView.getItemAtPosition 不返回查看所有;它从你的适配器(一个光标,一个数组项,等等。)来获取返回的TextView一个项目,你需要使用 findViewById
,或者更好的是,你的ViewHolder。我的认为的这将工作:
2) Here's the problem: (I realized this after copying and pasting in a different example, which I think won't be necessary now): ListView.getItemAtPosition doesn't return a View at all; it returns an item from your Adapter (a Cursor, an array entry, whatever.) To get the TextView, you need to use findViewById
, or better still, your ViewHolder. I think this will work:
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
ViewHolder holder = (ViewHolder) v.getTag();
TextView tv = holder.text;
String str = tv.getText().toString();
}
如果你仍然有问题,请复制并粘贴你得到异常的回溯。
If you're still having problems, please copy and paste the traceback for the exception that you're getting.
这篇关于EfficientAdapter和项目的抢夺文字点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!