【问题标题】:Passing data from a fragment to a custom adapter (for use with ListView)将数据从片段传递到自定义适配器(用于 ListView)
【发布时间】:2018-02-19 00:44:52
【问题描述】:

我正在尝试将从片段内的包中检索到的数据发送到 ListView 的适配器,以便可以显示数据,具体取决于以前的用户选择。

我可以记录正确的数据正在发送到我的片段,但是当我尝试将该数据传递到我的适配器的构造函数时,ListView 不会被填充。我不确定这是否是将数据发送到适配器的正确方法,尤其是因为数据不是来自游标。

在我的片段中,这是相关代码:

public class AddEditRecipeActivityFragment extends Fragment {
ListView mListView;
private IngredientListAdapter mIngredientListAdapter;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    Log.d(TAG, "onCreateView: starts");
    View view = inflater.inflate(R.layout.fragment_add_edit_recipe, container, false);


    mNameTextView = (EditText) view.findViewById(R.id.addedit_name);
    mAddItem = (ImageButton) view.findViewById(R.id.addGrocery);
    mDescriptionTextView = (EditText) view.findViewById(R.id.addedit_description);
    mMealType = (EditText) view.findViewById(R.id.addedit_meal);
    mSaveButton = (Button) view.findViewById(R.id.addedit_save);

    mListView = (ListView) view.findViewById(R.id.lstIngredient);


    Bundle arguments = getArguments();

    String addedGrocery = arguments.getString("GroceryName");

    String addedGroceryGrams = arguments.getString("grams");

    Log.d(TAG, "onCreateView: retrieved ingredient = " + addedGrocery
            + " retrieved amount = " + addedGroceryGrams );

   mIngredientListAdapter = new IngredientListAdapter(getActivity(), null,
            0, addedGrocery, addedGroceryGrams ) ;
    mListView.setAdapter(mIngredientListAdapter);


.... }

这是我的适配器代码,修改自 CursorAdapter 类:

public class IngredientListAdapter  extends CursorAdapter {
private static final String TAG = "IngredientListAdapter";

private LayoutInflater mInflater;
private Context mContext;
private String mIngredient;
private String mGrams;

public IngredientListAdapter(Context context, Cursor c, int flags, String ingredient, String grams) {
    super(context, c, flags);
    mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    this.mIngredient = ingredient;
    this.mGrams = grams;
}

public View newView(Context context, Cursor cursor, ViewGroup parent) {
    View view = mInflater.inflate(R.layout.ingrediend_listview, parent, false);
    ViewHolder holder = new ViewHolder();
    holder.ingredient = (TextView) view.findViewById(R.id.ingredient);
    holder.amount = (TextView) view.findViewById(R.id.amount);
    view.setTag(holder);

    return view;
}


public void bindView(View view, Context context, Cursor cursor) {
    ViewHolder holder = (ViewHolder) view.getTag();

    Log.d(TAG, "bindView: Starts");

    Log.d(TAG, "bindView: ingredient = " + mIngredient);

    holder.ingredient.setText(mIngredient);
    holder.amount.setText(mGrams);


}

static class ViewHolder {
    TextView ingredient;
    TextView amount;
 }
}

当我尝试查看我的 IngredientListAdapter 类的日志时,什么都没有显示。这很奇怪,因为我有另一个扩展 CursorAdapter 的适配器并且这些日志正在工作 - 可能是因为它们涉及游标参数?

我显然在这里遗漏了一些重要的东西,任何帮助将不胜感激!

【问题讨论】:

    标签: java android listview android-fragments adapter


    【解决方案1】:

    正如另一个答案中提到的那样,主要问题是您的Cursor 参数是null,这告诉CursorAdapter 您根本不想在ListView 中包含任何内容,因此什么都没有显示。

    由于您实际上并未使用Cursor,因此扩展CursorAdapter 不是一个好的选择。扩展ArrayAdapter 会更合适。

    您的适配器可能如下所示:

    public class IngredientAdapter extends ArrayAdapter<Ingredient> {
        public IngredientAdapter(Context context, ArrayList<Ingredient> ingredients) {
           super(context, 0, ingredients);
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
           // Get the data item for this position
           Ingredient ingredient = getItem(position);    
           // Check if an existing view is being reused, otherwise inflate the view
           if (convertView == null) {
              convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_ingredient, parent, false);
           }
           // Lookup view for data population
           TextView ingredientName = (TextView) convertView.findViewById(R.id.ingredientName);
           TextView ingredientAmt = (TextView) convertView.findViewById(R.id.ingredientAmt);
           // Populate the data into the template view using the data object
           ingredientName.setText(ingredient.name);
           ingredientAmt.setText(ingredient.amt);
           // Return the completed view to render on screen
           return convertView;
       }
    }
    

    Ingredient 将是您创建的用于保存每种成分信息的类。您也可以将ViewHolder 添加到适配器,但这不是必需的。

    与其将addedGrocery 和addedGroceryGrams 值直接传递到构造函数中,不如像你所拥有的那样,这只会让你在ListView 中有一个项目,或者多个具有相同值的项目,你应该传入包含信息的Ingredient 对象中的ArrayList:

    mIngredientListAdapter = new IngredientAdapter(getActivity(), ingredients);
    mListView.setAdapter(mIngredientListAdapter);
    

    【讨论】:

    • 感谢您的回复。它现在正在工作,除了如果我去添加另一种成分,添加的成分及其数量不会保存到列表中。我需要以某种方式保存 ArrayList 的状态,尽管我不确定如何在代码中实现它......有什么建议吗? :)
    • 没关系。只需要将 List 设为静态,因此只存在一个实例。
    【解决方案2】:

    我不想在这里做出新的回答,但评论行太短,无法扩展我的观点。

    您的适配器应该有两件事要管理的第一个让图像。首先是每个列表视图项目的视图。您已经通过添加视图持有者来完成它。第二件事是视图的数据。因为您使用光标适配器,所以您不需要在此适配器内部准备List&lt;E&gt;。光标为您完成了它。但是,您忘记通过将 null 传递给构造函数来准备它。

    请慢慢阅读this article。这对你有好处。

    你的问题在这里:

    public class AddEditRecipeActivityFragment extends Fragment {
    ...
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
    ...
        // The cursor's null
        mIngredientListAdapter = new IngredientListAdapter(getActivity(), null, 0, addedGrocery, addedGroceryGrams ) ;
        mListView.setAdapter(mIngredientListAdapter);
    ...
    }
    

    这里:

    public class IngredientListAdapter  extends CursorAdapter {
    ...
    public IngredientListAdapter(Context context, Cursor c, int flags, String ingredient, String grams) {
        super(context, c, flags);  // c null now
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.mIngredient = ingredient;
        this.mGrams = grams;
    }
    ...
    

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-06
      • 2018-09-09
      • 1970-01-01
      • 2019-04-14
      • 2021-06-20
      • 1970-01-01
      相关资源
      最近更新 更多