private void updateList() {
// For a ListActivity we need to set the List Adapter, and in order to do
//that, we need to create a ListAdapter. This SimpleAdapter,
//will utilize our updated Hashmapped ArrayList,
//use our single_post xml template for each item in our list,
//and place the appropriate info from the list to the
//correct GUI id. Order is important here.
ListAdapter adapter = new SimpleAdapter(this, mOutletsList,
R.layout.single_outlet, new String[] { TAG_OUTLET_NAME, TAG_SPARKLING_CHANNEL,
TAG_SPARKLING_CLASSIFICATION, TAG_CLASS}, new int[]
{ R.id.outlet_name, R.id.sparkling_channel, R.id.sparkling_classification,
R.id.cls_state});
// I shouldn't have to comment on this one:
setListAdapter(adapter);
// Optional: when the user clicks a list item we
//could do something. However, we will choose
//to do nothing...
final ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
HashMap<String, String>map = (HashMap<String, String>)parent.getItemAtPosition(position);
String foutname = map.get(TAG_OUTLET_NAME);
String fchannel = map.get(TAG_SPARKLING_CHANNEL);
String fclass = map.get(TAG_SPARKLING_CLASSIFICATION);
String fclass_state = map.get(TAG_CLASS);
String compr = "GDB";
String compr2 = "QSRG"
if(compr == fclass_state){
Intent i = new Intent(OutletsList.this, GdgScoreSheeet.class);
i.putExtra("outlt", foutname);
i.putExtra("chnl", fchannel);
i.putExtra("cls", fclass);
i.putExtra("clsstate", fclass_state);
startActivity(i);
}
if(compr2 == fclass_state){
Intent i = new Intent(OutletsList.this, QsrgScoreSheeet.class);
i.putExtra("outlt", foutname);
i.putExtra("chnl", fchannel);
i.putExtra("cls", fclass);
i.putExtra("clsstate", fclass_state);
startActivity(i);
}
}
});
}
当我点击它在logcat中显示此错误时,java.lang.ClassCastExeption:java.util.HashMap无法转换为java.lang.String.Ypur非常感谢帮助,甚至替代代码和解决方案。提前谢谢人
答案 0 :(得分:0)
问题是你正在将一个HashMap转换为一个字符串,这将给你一个错误ClassCastExeption
<强>问题:强>
此代码将返回String而不是HashMap
parent.getItemAtPosition(position);
从你的SimpleAdapter
开始,你为ListView中的每个项目添加了一个String
数组而不是一个HashMap。
new SimpleAdapter(this, mOutletsList,
R.layout.single_outlet,
new String[] { TAG_OUTLET_NAME, TAG_SPARKLING_CHANNEL, TAG_SPARKLING_CLASSIFICATION, TAG_CLASS},
new int[]{ R.id.outlet_name, R.id.sparkling_channel, R.id.sparkling_classification, R.id.cls_state});
解决方案1
除非您为它创建BaseAdapter,否则无法将其强制转换为HashMap,您可以click here学习制作适配器。
解决方案2
其他解决方案是从ListView
获取所有项目数组 String foutname = parent.getItemAtPosition(0);
String fchannel = parent.getItemAtPosition(1);
String fclass = parent.getItemAtPosition(2);
String fclass_state = parent.getItemAtPosition(3);
答案 1 :(得分:0)
可能会有所帮助 -
String classes[]="Start","Send","Tabs","Browser","Flipper","SharedPrefs","InternalData","Sqlite","Checking"};
//从此字符串数组生成列表视图
单击列表项上的//使用此...请注意try块中的第一行。
String cheese=classes[position];
try{
Class cl=Class.forName("com.example.test."+cheese);
Intent i=new Intent(Menu.this,cl);
startActivity(i);
}catch(ClassNotFoundException e){}