res/layout-v11/cell.xml
如果您以后只需要开发 API 级别 11 的应用程序,您所要做的就是编写如下 xml 代码。
<?xml version="1.0" encoding="utf-8"?>
<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">
<TextView
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
资源/布局/cell.xml
但是,在 API 级别 <= 10 中,上述代码会崩溃。所以它需要像以下......
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
DataListCursorAdapter.java
其次,需要通知您的适配器分配数据。//...(a)
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)
}
}
YourFragment.java 或 YourActivity.java
您实现接口并...
@Override
public void onDataAssigned(Data data, View view) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
return;
}
if (TextUtils.equals(mLastSelectedDataId, data.id)) {// default color
view.setBackgroundColor(0xfff7aecb);
}
else {// selected color
view.setBackgroundColor(0x00000000);
}
}
您将所选项目保留在班级的字段中。
@Override
public void onItemClick(AdapterView<?> 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 && TextUtils.equals(mLastSelectedDataId, data.id)) {
getListView().setItemChecked(position, false);
mLastSelectedDataId = null;
}
else if (data != null) {
getListView().setItemChecked(position, true);
mLastSelectedDataId = data.id;
}
}
重要的
- 在列表视图上,项目视图的数量不是项目的数量。
- 已更改颜色的选定视图被重新用于其他数据。
更新
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)
}
}