質問

my wish is, to have a listView and when i choose delete in the contextmenu appearing when I do a longclick on a listview item, there should appear checkboxes in each listview element and a buttonbar on the top and one on the bottom...and all at the same time. So i wrote the code following below. But when I click on delete, only the buttonbars appear and I have to do a second click on delete to make the checkboxes appear. By testing with different "configurations" of my code (adding or removing some parts) and checking everything step by step with the debugger I found out, that the checkboxes appear immediately, when I remove the buttonbars and all code is reached. So now my question is: Does anyone know, why the checkboxes and the bars don't appear on the same click and what to make, so they do?

public boolean onContextItemSelected(MenuItem menu) 
{ 
AdapterView.AdapterContextMenuInfo info = (AdapterView
    .AdapterContextMenuInfo) menu.getMenuInfo();
ListView tripList = (ListView) tripTracker.findViewById(R.id.tripList);
tripTracker.findViewById(R.id.buttonBar).setVisibility(View.VISIBLE);
tripTracker.findViewById(R.id.buttonBarCB).setVisibility(View.VISIBLE); 
checkBox(false, View.VISIBLE);
((CheckBox) tripList.findViewById(info.targetView.getId())
    .findViewById(R.id.checkBox)).setChecked(true); 
return true; 
}

private void checkBox(boolean checked, int visibility)
{
    ListView tripList = (ListView) tripTracker.findViewById(R.id.tripList);
    for (int i = 0; i < tripList.getAdapter().getCount(); i++)
    {
        int id = ((Trip) tripList.getAdapter().getItem(i)).getId();
        CheckBox cb = (CheckBox) tripList.findViewById(id).findViewById(
            R.id.checkBox);
        cb.setVisibility(visibility);
        cb.setChecked(checked);
    }
}

XML with ListView

    <LinearLayout
      android:id="@+id/buttonBarCB"
      style="?android:attr/buttonBarStyle"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:visibility="gone" >

        <Button
          android:id="@+id/allCB"
          style="?android:attr/buttonBarButtonStyle"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="@string/all" />

        <Button
          android:id="@+id/noneCB"
          style="?android:attr/buttonBarButtonStyle"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="@string/none" />
    </LinearLayout>

    <TextView
      android:id="@+id/noTrip"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="center_horizontal"
      android:layout_marginTop="@dimen/activity_horizontal_margin"
      android:text="@string/noTrip" />

    <ListView
      android:id="@+id/tripList"
      android:layout_width="match_parent"
      android:layout_height="0dp"
      android:layout_weight="1" >

    </ListView>

    <LinearLayout
      android:id="@+id/buttonBar"
      style="?android:attr/buttonBarStyle"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:visibility="gone" >

        <Button
          android:id="@+id/cancelTr"
          style="?android:attr/buttonBarButtonStyle"
          android:layout_width="0dp"
          android:layout_height="wrap_content"
          android:layout_weight="0.5"
          android:text="@android:string/cancel" />

        <Button
          android:id="@+id/okTr"
          style="?android:attr/buttonBarButtonStyle"
          android:layout_width="0dp"
          android:layout_height="wrap_content"
          android:layout_weight="0.5"
          android:text="@android:string/ok" />
    </LinearLayout>

</LinearLayout>

XML for rows

    <RelativeLayout
      android:layout_width="wrap_content"
      android:layout_height="fill_parent" >

        <CheckBox
          android:id="@+id/checkBox"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentLeft="true"
          android:layout_centerVertical="true"
          android:visibility="gone" />

    </RelativeLayout>

    <RelativeLayout
      android:id="@+id/RelativeLayout1"
      android:layout_width="match_parent"
      android:layout_height="match_parent" >

        <TextView
          android:id="@+id/h1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentLeft="true"
          android:layout_alignParentTop="true"
          android:text="Large Text"
          android:textAppearance="?android:attr/textAppearanceLarge"
          android:textColor="?android:attr/textColorPrimaryInverse" />

        <TextView
          android:id="@+id/date"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentRight="true"
          android:layout_alignTop="@+id/h1"
          android:text="date"
          android:textColor="?android:attr/textColorSecondaryInverse" />

        <TextView
          android:id="@+id/h2"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignLeft="@+id/h1"
          android:layout_below="@+id/h1"
          android:text="Small Text"
          android:textAppearance="?android:attr/textAppearanceSmall"
          android:textColor="?android:attr/textColorSecondaryInverse" />
    </RelativeLayout>

</LinearLayout>
役に立ちましたか?

解決

I realy suggest you do not reinvent the wheel and use Contexual Action Bar. Here you can find my example how to select rows in listview and work with them.

And of cause, do not forget to call adapter.notifyDataSetChanged(), after changes of data that is under adapter.

enter image description here

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top