我正在尝试通过列表项目进行检查,但是,有一个问题:一旦检查了一个项目,另一个项目被攻击,但是,对于每个已检查或未选中的项目,正确显示了Toast消息。但我找不到设置删除线功能的方法。将不胜感激任何帮助:)
OnItemClickListener itemClickListener = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
ListView lv = (ListView) a;
if (lv.isItemChecked(position)) {
Toast.makeText(getBaseContext(), "You checked " + values.get(position), Toast.LENGTH_SHORT).show();
TextView row = (TextView) findViewById(android.R.id.text1);
row.setPaintFlags(row.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
}
else {
Toast.makeText(getBaseContext(), "You unchecked " + values.get(position), Toast.LENGTH_SHORT).show();
TextView row = (TextView) findViewById(android.R.id.text1);
row.setPaintFlags(row.getPaintFlags() & ~Paint.STRIKE_THRU_TEXT_FLAG);
}
}
};
如果使用TextView row =(TextView)lv.getItemAtPosition(position),则应用程序会崩溃 - logcat:
1-08 16:39:30.373:E / AndroidRuntime(1934):未捕获的处理程序:由于未捕获的异常导致线程主要退出 01-08 16:39:30.383:E / AndroidRuntime(1934):java.lang.ClassCastException:java.lang.String 01-08 16:39:30.383:E / AndroidRuntime(1934):at com.jms.purchaseexamples.MainActivity $ 1.onItemClick(MainActivity.java:72) 01-08 16:39:30.383:E / AndroidRuntime(1934):在android.widget.AdapterView.performItemClick(AdapterView.java:284) 01-08 16:39:30.383:E / AndroidRuntime(1934):在android.widget.ListView.performItemClick(ListView.java:3285) 01-08 16:39:30.383:E / AndroidRuntime(1934):在android.widget.AbsListView $ PerformClick.run(AbsListView.java:1640) 01-08 16:39:30.383:E / AndroidRuntime(1934):在android.os.Handler.handleCallback(Handler.java:587) 01-08 16:39:30.383:E / AndroidRuntime(1934):在android.os.Handler.dispatchMessage(Handler.java:92) 01-08 16:39:30.383:E / AndroidRuntime(1934):在android.os.Looper.loop(Looper.java:123) 01-08 16:39:30.383:E / AndroidRuntime(1934):在android.app.ActivityThread.main(ActivityThread.java:4363) 01-08 16:39:30.383:E / AndroidRuntime(1934):at java.lang.reflect.Method.invokeNative(Native Method) 01-08 16:39:30.383:E / AndroidRuntime(1934):at java.lang.reflect.Method.invoke(Method.java:521) 01-08 16:39:30.383:E / AndroidRuntime(1934):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:860) 01-08 16:39:30.383:E / AndroidRuntime(1934):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 01-08 16:39:30.383:E / AndroidRuntime(1934):at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
我没看到您实际选择点击的行进行检查的位置? findViewById(android.R.id.text1)似乎不是你想要的......
怎么样:
//Actually select the correct item from the listview
TextView row = (TextView)lv.getItemAtPosition(position);
row.setPaintFlags(row.getPaintFlags() & ~Paint.STRIKE_THRU_TEXT_FLAG);