2

This is my list layout:

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
       xmlns:android="http://schemas.android.com/apk/res/android"
         android:orientation="vertical"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent">
     <LinearLayout android:layout_width="fill_parent" android:id="@+id/linearLayout1"              
         android:layout_height="wrap_content">
    <LinearLayout android:layout_width="wrap_content" android:id="@+id/linearLayout2" 
         android:layout_height="fill_parent">
    <CheckBox android:id="@+id/checkBox1" android:layout_width="wrap_content" 
     android:layout_height="wrap_content"></CheckBox>
    </LinearLayout>
    <LinearLayout android:layout_width="wrap_content" android:id="@+id/linearLayout3" android:layout_height="fill_parent" android:orientation="vertical">
        <TextView android:text="TextView" android:id="@+id/row1" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
        <TextView android:text="TextView" android:id="@+id/row2"     android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
    </LinearLayout>
    </LinearLayout>

</LinearLayout>

Although I have an excess of Layouts, all this is is a checkbox and a couple of TextViews. I want each listItem to be clickable(as in go to a different intent), but when I click a button (DELETE BUTTON)in my activity, I want to be able to show the checkboxes and select the items to delete and delete it. Sample code appreciated.

Java code:

adapter = new SimpleAdapter(this, painItems, R.layout.mylistlayout,
            from, to);
    listthings.setAdapter(adapter);
    listthings.setOnItemClickListener(this);
    public void onItemClick(AdapterView<?> a, View v, int position, long id) {
    Intent intent = new Intent(this, Item1.class);
    intent.putExtra("com.painLogger.painLevel", painLevelString);
    intent.putExtra("com.painLogger.painTime", timeOfPainString);
    intent.putExtra("com.painLogger.treatment", textTreatmentString);
    intent.putExtra("painLocation", painLocation);
    startActivity(intent);
}
1
  • This has been asked so many times on SO. Commented Jul 26, 2011 at 18:22

1 Answer 1

3

Delete checked items

private void deleteCheckedItems() {
    int count = this.mainListView.getAdapter().getCount();
    for (int i = 0; i < count; i++) {
        if (this.mainListView.isItemChecked(i)) {
            painItems.remove(i)
        }
    }
}

More info: http://developer.android.com/reference/android/widget/AbsListView.html#isItemChecked(int)

and then notify the adapter to update:

adapter.notifyDataSetChanged();

public void notifyDataSetChanged ()

Since: API Level 1 Notifies the attached View that the underlying data has been changed and it should refresh itself.

Sign up to request clarification or add additional context in comments.

9 Comments

how exactly would I delete the item at position i?
Sorry, maybe I'm not clear on this. I just need to delete it from my list. Is there no method for this or way to do this?
Im using this : List<HashMap<String, String>> painItems = new ArrayList<HashMap<String, String>>(); ListView listthings;
thank you so much, but one problem still remains. I cannot have a working checkbox and click the item of the list. If I add the checkbox, I cannot even click the item anymore. Is there a reason, solution to this?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.