2017-09-05 162 views
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; 
    } 
} 

回答

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); 
}