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);
}
Kgrover
  • 2,106
  • 2
  • 36
  • 54

1 Answers1

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.

Waza_Be
  • 39,407
  • 49
  • 186
  • 260
  • how exactly would I delete the item at position i? – Kgrover Jul 26 '11 at 18:35
  • 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? – Kgrover Jul 26 '11 at 18:39
  • http://stackoverflow.com/questions/112503/how-do-i-remove-objects-from-an-array-in-java – Waza_Be Jul 26 '11 at 18:39
  • Im using this : `List> painItems = new ArrayList>(); ListView listthings;` – Kgrover Jul 26 '11 at 18:39
  • 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? – Kgrover Jul 26 '11 at 18:43
  • http://stackoverflow.com/questions/5417339/android-listview-with-checkbox-and-all-clickable – Waza_Be Jul 26 '11 at 18:44
  • @Kgrover let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/1857/discussion-between-profete162-and-kgrover) – Waza_Be Jul 26 '11 at 18:44
  • android:focusable="false" android:focusableInTouchMode="false" – Kgrover Jul 26 '11 at 18:44
  • BTW, if you have more questions, please accept/vote for my answer if that helped you and then create an other question thread. Other people might have the same question and might get lost. – Waza_Be Jul 26 '11 at 18:47