How to cross out text items in a list if they are checked?

I try to cross out the list items when they are checked, however there is one problem: after one item is checked, the other gets stacks, however, toast messages are displayed correctly for each item that is marked or unchecked. But I cannot find a way to set the position for the strikethrough function. I would be grateful for any help :)

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); } } }; 

if TextView row = (TextView) lv.getItemAtPosition (position) is used, the application breaks - logcat:

1-08 16: 39: 30.373: E / AndroidRuntime (1934): sewage handler: main thread due to an uncaught exception 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): at android.widget.AdapterView.performItemClick (AdapterView.java:284) 01-08 16: 39: 30.383: E / AndroidRuntime (1934): at android.widget.ListView.performItemClick (ListView.java:3285) 01 -08 16: 39: 30.383: E / AndroidRuntime (1934): at android.widget.AbsListView $ PerformClick.run (AbsListView.java:1640) 01-08 16: 39: 30.383: E / AndroidRuntime (1934): at android .os.Handler.handleCallback (Handler.javaβˆ—87) 01-08 16: 39: 30.383: E / AndroidRuntime (1934): at android.os.Handler.dispatchMessage (Handler.java:92) 01-08 16: 39 : 30.383: E / AndroidRuntime (1934): at and roid.os.Looper.loop (Looper.java:123) 01-08 16: 39: 30.383: E / AndroidRuntime (1934): at 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.javaPoint21) 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)

+4
source share
1 answer

I don’t see where you actually select the line that was pressed to check it? findViewById (android.R.id.text1) doesn't look like what you want.

What about:

 //Actually select the correct item from the listview TextView row = (TextView)lv.getItemAtPosition(position); row.setPaintFlags(row.getPaintFlags() & ~Paint.STRIKE_THRU_TEXT_FLAG); 
+1
source

All Articles