4

I'm trying to strikethrough list items when they are checked, however, there's one problem: once one item is checked, another gets striked, however, the toast messages are shown correctly for each item checked or unchecked. But I can't find the way to set the position for strikethrough function. 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 down - the logcat:

1-08 16:39:30.373: E/AndroidRuntime(1934): Uncaught handler: thread main exiting due to 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:587) 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 android.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.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)

Juta
  • 41
  • 1
  • 5

1 Answers1

3

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

how about:

//Actually select the correct item from the listview
TextView row = (TextView)lv.getItemAtPosition(position);
row.setPaintFlags(row.getPaintFlags() & ~Paint.STRIKE_THRU_TEXT_FLAG);
krilovich
  • 3,385
  • 1
  • 20
  • 33