Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>res/layout-v11/cell.xml</strong></p> <p>If you need to develop your application only API level 11 later, All you have to do is to write like following xml code.</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="?android:attr/activatedBackgroundIndicator" android:orientation="vertical"&gt; &lt;TextView android:id="@+id/name" android:layout_width="fill_parent" android:layout_height="wrap_content"/&gt; &lt;TextView android:id="@+id/text" android:layout_width="fill_parent" android:layout_height="wrap_content"/&gt; &lt;/LinearLayout&gt; </code></pre> <p><strong>res/layout/cell.xml</strong></p> <p>But, in API level &lt;= 10, the above code takes crashes. So it needs like following...</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical"&gt; &lt;TextView android:id="@+id/name" android:layout_width="fill_parent" android:layout_height="wrap_content"/&gt; &lt;TextView android:id="@+id/text" android:layout_width="fill_parent" android:layout_height="wrap_content"/&gt; &lt;/LinearLayout&gt; </code></pre> <p><strong>DataListCursorAdapter.java</strong></p> <p>At second, Your adapter is needed to be notified assign of data.//...(a)</p> <pre><code>class DataListCursorAdapter extends CursorAdapter { private Listener mListener; public static class ViewHolder { public TextView name; public TextView text; } @SuppressWarnings("deprecation") public DataListCursorAdapter(Context context, Cursor c, Listener listener) { super(context, c); mListener = listener; } public DataListCursorAdapter(Context context, Cursor c, Listener listener, boolean autoRequery) { super(context, c, false); mListener = listener; } public DataListCursorAdapter(Context context, Cursor c, Listener listener, int flags) { super(context, c, FLAG_REGISTER_CONTENT_OBSERVER); mListener = listener; } @Override public void bindView(View view, Context context, Cursor cursor) { try { ViewHolder holder = (ViewHolder) view.getTag(); JSONObject json = new JSONObject(cursor.getString(cursor.getColumnIndex(Table.DATA))); final Data data = new Data(json); mListener.onDataAssigned(data, view);//...(a) holder.name.setText(data.name); holder.text.setText(data.text); } catch (JSONException e) { } } @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(R.layout.cell, null); ViewHolder holder = new ViewHolder(); holder.name = (TextView) view.findViewById(R.id.name); holder.text = (TextView) view.findViewById(R.id.text); view.setTag(holder); return view; } public interface Listener { public void onDataAssigned(Data data, View view);//...(a) } } </code></pre> <p><strong>YourFragment.java or YourActivity.java</strong></p> <p>you implement the interface and ...</p> <pre><code>@Override public void onDataAssigned(Data data, View view) { if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.HONEYCOMB) { return; } if (TextUtils.equals(mLastSelectedDataId, data.id)) {// default color view.setBackgroundColor(0xfff7aecb); } else {// selected color view.setBackgroundColor(0x00000000); } } </code></pre> <p>you keep the selected item on the field of your class.</p> <pre><code>@Override public void onItemClick(AdapterView&lt;?&gt; listView, View view, int position, long id) { Cursor cursor = mArrayCursorAdapter.getCursor(); JSONObject json = new JSONObject(cursor.getString(cursor.getColumnIndex(Table.DATA))); final Data data = new Data(json); if (data != null &amp;&amp; TextUtils.equals(mLastSelectedDataId, data.id)) { getListView().setItemChecked(position, false); mLastSelectedDataId = null; } else if (data != null) { getListView().setItemChecked(position, true); mLastSelectedDataId = data.id; } } </code></pre> <p><strong>Important</strong></p> <ul> <li>On the list view, the number of item views is not the number of item. </li> <li>The selected view, which is changed color, is reused for another data.</li> </ul> <p><strong>Update</strong></p> <pre><code>public class YourAdapter extends BaseAdapter { private LayoutInflater mInflater; private Listener mListener; private Model_Category mData; static class ViewHolder { TextView category; } public YourAdapter(Context context, Model_Category data, LayoutInflater inflater, Listener listener) { mInflater = inflater; mListener = listener; mData = data; } @Override public int getCount() { return /*return count*/; } @Override public Object getItem(int position) { return /*return item by position*/; } @Override public long getItemId(int position) { return /*return id by position*/; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; int type = getItemViewType(position); if (convertView == null){ holder = new ViewHolder(); convertView = mInflater.inflate(R.layout.customitemlistview_menu, null); holder.category = (TextView) convertView.findViewById(R.id.category_menu); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } mListener.onDataAssigned(mData.get(position), convertView);//...(a) holder.category.setText(mData.get(position)); return convertView; } public interface Listener { public void onDataAssigned(Data data, View view);//...(a) } } </code></pre>
    singulars
    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. 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