我正在尝试使用自定义适配器实现listview。问题是每当触发onItemClickListener事件时它都会崩溃。
java.lang.ClassCastException:imp.translator.diana.lang.CardItemData 无法强制转换为java.lang.String at imp.translator.diana.lang.Speak $ 1.onItemClick(Speak.java:41) 在android.widget.AdapterView.performItemClick
这是我的班级:
final ListView list = (ListView) findViewById(R.id.list_view);
list.addHeaderView(new View(this));
list.addFooterView(new View(this));
BaseInflaterAdapter<CardItemData> adapter = new BaseInflaterAdapter<CardItemData>(new CardInflater());
CardItemData data = new CardItemData("Translate");
adapter.addItem(data, false);
CardItemData data2 = new CardItemData("Voice to Text");
adapter.addItem(data2, false);
CardItemData data3 = new CardItemData("Record");
adapter.addItem(data3, false);
CardItemData data4 = new CardItemData("Info");
adapter.addItem(data4, false);
list.setAdapter(adapter);
list.setClickable(true);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView myAdapter, View myView, int position, long mylng) {
String selectedFromList = (String) (myAdapter.getItemAtPosition(position));
}
});
和我的适配器:
public class BaseInflaterAdapter<T> extends BaseAdapter
{
private List<T> m_items = new ArrayList<T>();
private IAdapterViewInflater<T> m_viewInflater;
public BaseInflaterAdapter(IAdapterViewInflater<T> viewInflater)
{
m_viewInflater = viewInflater;
}
public BaseInflaterAdapter(List<T> items, IAdapterViewInflater<T> viewInflater)
{
m_items.addAll(items);
m_viewInflater = viewInflater;
}
public void setViewInflater(IAdapterViewInflater<T> viewInflater, boolean notifyChange)
{
m_viewInflater = viewInflater;
if (notifyChange)
notifyDataSetChanged();
}
public void addItem(T item, boolean notifyChange)
{
m_items.add(item);
if (notifyChange)
notifyDataSetChanged();
}
public void addItems(List<T> items, boolean notifyChange)
{
m_items.addAll(items);
if (notifyChange)
notifyDataSetChanged();
}
public void clear(boolean notifyChange)
{
m_items.clear();
if (notifyChange)
notifyDataSetChanged();
}
@Override
public int getCount()
{
return m_items.size();
}
@Override
public Object getItem(int pos)
{
return getTItem(pos);
}
public T getTItem(int pos)
{
return m_items.get(pos);
}
@Override
public long getItemId(int pos)
{
return pos;
}
@Override
public View getView(int pos, View convertView, ViewGroup parent)
{
return m_viewInflater != null ? m_viewInflater.inflate(this, pos, convertView, parent) : null;
}
}
和CardItemData:
public class CardItemData
{
private String m_text1;
public CardItemData(String text1)
{
m_text1 = text1;
}
public String getText1()
{
return m_text1;
}
}
知道这里可能有什么问题吗?
答案 0 :(得分:1)
知道这里可能有什么问题吗?
您的getItem
正在返回T
,在您的情况下,这是CardItemData
的实例,但在此处:
String selectedFromList = (String) (myAdapter.getItemAtPosition(position));
您将其投射到String
。您可能希望将其强制转换为CardItemData
。顺便说一句,Java支持协变返回类型,因此您应该能够使用
@Override
public T getItem(int pos) {
return m_items.get(pos);
}
答案 1 :(得分:0)
它是一个类型转换问题,你是类型转换&#34; CardItemData&#34;到&#34;字符串&#34;
String selectedFromList =(String)(myAdapter.getItemAtPosition(position));
使用&#34; getItem&#34;获得点击的方法&#34; CardItemData&#34;项目
检查下面给出的链接 Android Docs link