0
最終,我想單擊ListView中的一個項目(最終是一個回收站列表視圖),並讓它將一個ID傳送到下一個活動以便從數據庫中檢索。將數據存儲在列表項中
我有一個ListView,由CursorAdapter(subclassed)提供。當我在CursorAdapter.bindView()中設置斷點時,傳入的視圖的類型爲TwoItemListItem,因此我無法在RecipeListItem上設置該ID,我相信這是我需要通過ListView將信息傳遞給下一個活動的操作.setOnItemClickListener()。
在源活動中,我得到的列表視圖和適配器設置:
RecipeCursorAdapter adapter = new RecipeCursorAdapter(this, cur);
ListView listView = (ListView) findViewById(R.id.recipe_list_list);
listView.setAdapter(adapter);
RecipeCursorAdapter:
public class RecipeCursorAdapter extends CursorAdapter {
protected LayoutInflater cursorInflator;
public RecipeCursorAdapter(Context context, Cursor cursor){
super(context, cursor, 0);
this.cursorInflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
// The newView method is used to inflate a new view and return it,
// you don't bind any data to the view at this point.
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent){
return this.cursorInflator.inflate(R.layout.activity_recipe_list_item, parent, false);
}
@Override
public void bindView(View view, Context context, Cursor cursor){
// Find fields to populate in inflated template
TextView tvTitle = (TextView) view.findViewById(R.id.recipeListItemTitle);
TextView tvSubtitle = (TextView) view.findViewById(R.id.recipeListItemSubtitle);
// Extract properties from cursor
String name = cursor.getString(cursor.getColumnIndexOrThrow("name"));
String description = cursor.getString(cursor.getColumnIndexOrThrow("description"));
// Populate fields with extracted properties
tvTitle.setText(name);
tvSubtitle.setText(description);
}
}
activity_recip_list_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/listPreferredItemHeight"
android:mode="twoLine"
android:orientation="horizontal"
tools:context="com.smadacm.reciperepo.RecipeListItem" >
<TextView
android:id="@+id/recipeListItemTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/recipeListItemSubtitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_below="@id/recipeListItemTitle"
android:layout_alignStart="@id/recipeListItemTitle"
android:textColor="@color/colorListSecondary"
android:textAppearance="?android:attr/textAppearanceListItemSecondary" />
</TwoLineListItem>
RecipeListItem:
public class RecipeListItem extends AppCompatActivity {
protected int itemId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recipe_list_item);
}
public void setItemId(int id){
this.itemId = id;
}
public int getItemId(){
return this.itemId;
}
}