最终,我想点击ListView中的一个项目(最终是一个回收者列表视图)并让它将一个id传输到下一个活动,以便从db中进行检索。
我有一个ListView,由CursorAdapter(子类)提供。当我在CursorAdapter.bindView()中设置断点时,传入的View的类型为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;
}
}
答案 0 :(得分:0)
我能够绊倒一些功能性的东西。我不知道这是否正确,我可以接受纠正。
在我的主要活动中,我添加了一个听众:
RecipeCursorAdapter adapter = new RecipeCursorAdapter(this, cur);
ListView listView = (ListView) findViewById(R.id.recipe_list_list);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
int ii = 0; // A line on which I can set a breakpoint
}
});
我添加了一个新类来扩展LinearLayout。这主要是调用超类,但也添加了设置和检索任意数据的方法:
public class RecipeListItem extends LinearLayout {
protected int recipeId;
public RecipeListItem(Context context) {
super(context, (AttributeSet)null, 0, 0);
}
public RecipeListItem(Context context, AttributeSet attrs) {
super(context, attrs, 0, 0);
}
public RecipeListItem(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr, 0);
}
public RecipeListItem(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
public void setRecipeId(int id){
this.recipeId = id;
}
public int getRecipeId(){
return this.recipeId;
}
}
我使用LinearLayout子类来定义列表项:
<com.smadacm.reciperepo.widget.RecipeListItem 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:orientation="vertical"
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" />
</com.smadacm.reciperepo.widget.RecipeListItem>
最后,在我的CursorAdapter中,我按项设置id:
@Override
public void bindView(View viewRaw, Context context, Cursor cursor){
RecipeListItem view = (RecipeListItem) viewRaw;
// 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"));
int id = cursor.getInt(cursor.getColumnIndexOrThrow("_id"));
// Populate fields with extracted properties
tvTitle.setText(name);
tvSubtitle.setText(description);
view.setRecipeId(id);
}