Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>So I believe I have resolved the main problem I was facing.</p> <p>I believe the problem I was having was that my view was not representing the state of the ListView. In lamens; I thought a checkbox was needed to set whether a list item was checked or not (this is the web developer in me). However, the ListView maintains its own state of whats checked and whats not, regardless if you have a view (CheckBox) to represent that or not.</p> <p>layout/list_item.xml</p> <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" &gt; &lt;ProgressBar android:id="@+id/progressBar1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="5dip" /&gt; &lt;LinearLayout android:id="@+id/linearLayout2" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:orientation="vertical" &gt; &lt;TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Line 1" android:textAppearance="?android:attr/textAppearanceMedium" /&gt; &lt;TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Line 2" /&gt; &lt;/LinearLayout&gt; &lt;CheckBox android:id="@+id/checkBox1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:clickable="false" android:focusable="false" android:visibility="gone" /&gt; &lt;/LinearLayout&gt; </code></pre> <p>menu/menu.xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;menu xmlns:android="http://schemas.android.com/apk/res/android" &gt; &lt;item android:id="@+id/edit_menu_item" android:title="KAMEHAMEHA!!!!!"/&gt; &lt;/menu&gt; </code></pre> <p>menu/edit.xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;menu xmlns:android="http://schemas.android.com/apk/res/android" &gt; &lt;item android:id="@+id/commit_menu_item" android:title="Commit"/&gt; &lt;item android:id="@+id/cancel_menu_item" android:title="Cancel"/&gt; &lt;/menu&gt; </code></pre> <p>ListViewTestActivity.java</p> <pre><code>package com.loesak.listviewtest; import android.app.ListActivity; import android.content.Context; import android.os.Bundle; import android.util.Log; import android.util.SparseBooleanArray; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.CheckBox; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; public class ListViewTestActivity extends ListActivity { private static final String[] GENRES = new String[] { "Action", "Adventure", "Animation", "Children", "Comedy", "Documentary", "Drama", "Foreign", "History", "Independent", "Romance", "Sci-Fi", "Television", "Thriller" }; private ListView listView = null; private MyArrayAdapter myArrayAdapter = null; private boolean editMode = false; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.listView = getListView(); this.myArrayAdapter = new MyArrayAdapter(this, GENRES); this.setListAdapter(this.myArrayAdapter); } @Override public boolean onPrepareOptionsMenu(Menu menu) { menu.clear(); MenuInflater inflater = this.getMenuInflater(); if(!this.editMode) { inflater.inflate(R.menu.menu, menu); } else { inflater.inflate(R.menu.edit, menu); } return super.onPrepareOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId() == R.id.edit_menu_item) { Toast.makeText(this, "entering edit mode", Toast.LENGTH_SHORT).show(); this.editMode = true; this.listView.setItemsCanFocus(false); this.listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); this.myArrayAdapter.setEditMode(editMode); } else if(item.getItemId() == R.id.commit_menu_item || item.getItemId() == R.id.cancel_menu_item) { if(item.getItemId() == R.id.commit_menu_item) { Toast.makeText(this, "committing changes", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, "leaving edit mode", Toast.LENGTH_SHORT).show(); } this.editMode = false; this.listView.setItemsCanFocus(true); this.listView.setChoiceMode(ListView.CHOICE_MODE_NONE); this.listView.clearChoices(); this.myArrayAdapter.setEditMode(editMode); } return super.onOptionsItemSelected(item); } @Override protected void onListItemClick(ListView l, View v, int position, long id) { if(!this.editMode) { Toast.makeText(this, "list item selected at position " + position, Toast.LENGTH_SHORT).show(); } else { String str = ""; SparseBooleanArray wtfit = l.getCheckedItemPositions(); for(int i = 0; i &lt; GENRES.length; i++) { if(wtfit.get(i)) { str += i + ", "; } } Toast.makeText(this, "Selected item positions: " + str, Toast.LENGTH_SHORT).show(); } super.onListItemClick(l, v, position, id); } class MyArrayAdapter extends ArrayAdapter&lt;String&gt; { private Context context; private String[] entires; private boolean editMode = false; public MyArrayAdapter(Context context, String[] entries) { super(context, R.layout.list_item, entries); this.context = context; this.entires = entries; } @Override public View getView(final int position, View view, ViewGroup parent) { final ListView listView = (ListView) parent; final ViewHolder viewHolder; if(view == null) { LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); view = layoutInflater.inflate(R.layout.list_item, null, true); viewHolder = new ViewHolder(); viewHolder.text1 = (TextView) view.findViewById(R.id.textView1); viewHolder.text2 = (TextView) view.findViewById(R.id.textView2); viewHolder.checkbox1 = (CheckBox) view.findViewById(R.id.checkBox1); view.setTag(viewHolder); } else { viewHolder = (ViewHolder) view.getTag(); } viewHolder.text1.setText(this.entires[position] + "_1"); viewHolder.text2.setText(this.entires[position] + "_2"); viewHolder.checkbox1.setChecked(listView.isItemChecked(position)); if(this.editMode) { viewHolder.checkbox1.setVisibility(View.VISIBLE); } else { viewHolder.checkbox1.setVisibility(View.GONE); } return view; } public void setEditMode(boolean editMode) { this.editMode = editMode; this.notifyDataSetChanged(); } class ViewHolder { TextView text1; TextView text2; CheckBox checkbox1; } } } </code></pre> <p>To have the checkbox reflect the actual selected mode of the list item:</p> <pre><code>viewHolder.checkbox1.setChecked(listView.isItemChecked(position)); </code></pre> <p>That's it. Kind of obvious once you know how it all works. Too bad it took me this long to figure out. Sometimes I feel the android programming is a bit too complex at times.</p> <p>The only problem I have now is that the ListView items are always redrawn when a list item is clicked. This is wreaking havoc on my list items which contain asynchronously loaded images from file. Hopefully when I beef up the performance on that portion of my code, this will disappear.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload