Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Finally I ended up creating my own custom adapter, this way I could at least understand more easily what was happening.</p> <p>I had to create actually several multiselect lists, some populated with data from the database, others from the shared preferences.</p> <p>For this one displaying data from the DB, I created the following adapter (I commented out the lines about the icons because I did not set them up yet):</p> <pre><code>public class CategoriesLVAdapter extends BaseAdapter { private Context mContext; private LayoutInflater mInflater; private List&lt;Category&gt; categoriesList; // Constructor public CategoriesLVAdapter(Context c, List&lt;Category&gt; categories_list){ mContext = c; mInflater = LayoutInflater.from(c); categoriesList = categories_list; } public List&lt;Category&gt; getCategoriesList(){ return categoriesList; } @Override public int getCount() { return categoriesList.size(); } @Override public Object getItem(int position) { return categoriesList.get(position); } @Override public long getItemId(int position) { return categoriesList.get(position).getID(); } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; if (convertView == null) { convertView = mInflater.inflate(R.layout.categories_list_row, null); //convertView.setLayoutParams(new ListView.LayoutParams(200, 90)); holder = new ViewHolder(); holder.title = (TextView) convertView.findViewById(R.id.categories_list_row_tv); //holder.icon = (ImageView) convertView.findViewById(R.id.categories_list_row_iv); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } //holder.icon.setImageResource(categoriesList.get(position).getDrawableID()); //holder.icon.setAdjustViewBounds(true); //holder.icon.setScaleType(ImageView.ScaleType.CENTER_CROP); holder.title.setText(categoriesList.get(position).getName()); return convertView; } static class ViewHolder { TextView title; //ImageView icon; } </code></pre> <p>}</p> <p>In my activity, I use this adapter when the AlertDialog is called to populate the ListView, then I pre-select the categories using the last ones saved in the shared preferences:</p> <pre><code>private void categoriesFilter(){ AlertDialog.Builder alt_bld = new AlertDialog.Builder(this); alt_bld.setTitle(resources.getText(R.string.select_categories).toString()); LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); View layout = inflater.inflate(R.layout.categories_list,(ViewGroup) findViewById(R.id.categories_layout_root)); categoriesLV = (ListView) layout.findViewById(R.id.categories_list); alt_bld.setPositiveButton("Ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { String selectedCategoriesString = getSelectedValues(categoriesLV); //Update the shared preferences prefs.edit().putString(RateDayApplication.PREF_KEY_CATEGORIES, selectedCategoriesString).commit(); updateFilterDisplay(resources.getText(R.string.cat_title).toString(), selectedCategoriesString, searchedCategoriesTV, "Category"); } }); alt_bld.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { dialog.cancel(); } }); String selectedCategoriesString = prefs.getString(RateDayApplication.PREF_KEY_CATEGORIES, new String()); categoriesLV.setAdapter(new CategoriesLVAdapter(this, categoriesList)); String[] selectedCategoriesArray = selectedCategoriesString.split(","); int categoriesLVLength = categoriesLV.getCount(); for(int i = 0; i &lt; categoriesLVLength; i++){ int categoryID = ((Category) categoriesLV.getItemAtPosition(i)).getID(); if(Arrays.asList(selectedCategoriesArray).contains(String.valueOf(categoryID))){ categoriesLV.setItemChecked(i, true); } } alt_bld.setView(layout); AlertDialog alert = alt_bld.create(); alert.show(); } </code></pre> <p>Finally here is the function I call from my database handler to get the list of catagories:</p> <pre><code>// Getting All Categories By ID desc public List&lt;Category&gt; getCategoriesList() { String selectQuery = "SELECT " + CATEGORY_ID + ", " + CATEGORY_NAME + " FROM " + CATEGORY_TABLE + " ORDER BY " + CATEGORY_ID + " ASC"; SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = db.rawQuery(selectQuery, null); List&lt;Category&gt; categoriesList = new ArrayList&lt;Category&gt;();//String[] categoriesList = {}; // looping through all rows and adding to list if (cursor.moveToFirst()) { do { Category category = new Category(cursor.getInt(0), cursor.getString(1), false); categoriesList.add(category); } while (cursor.moveToNext()); } cursor.close(); db.close(); return categoriesList; } </code></pre> <p>I think my problem before was coming from the fact that the function "setItemChecked" is a little misleading because it does not mean necessarily that anything is checked. When you use the function "setItemChecked", the item in the list view becomes selected, with or without a checkbox (my rows only contain text views).</p> <p>The rows selected in my list appear in a different color, and that's enough in my opinion for a simple multi selection list.</p> <p>The layouts I used are quite simple, "categories_list" contains a ListView in a LinearLayout and "categories_list_row" contains a TextView in a LinearLayout.</p> <p>Hope it may guide someone!</p>
 

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