将数据从片段传递到自定义适配器(与ListView一起使用)



我正在尝试将片段中的捆绑包中检索到的数据发送到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,并且这些日志正在起作用 - 也许是因为它们涉及光标参数?

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

其他答案中提到的主要问题是您的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,但不是必需的。

而不是将addedGroceryaddedGroceryGrams值直接传递到构造函数中,这只会允许您在ListView中拥有一个项目,或具有相同值的多个项目,您应该通过IngredientArrayList传递包含信息的对象:

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

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

您的适配器应该有2件事要管理的第一个让图像。第一件事是每个列表视图项目的视图。您已经通过添加视图支架来完成。第二件事是视图的数据。因为您使用光标适配器,因此您无需准备此适配器内部的List<E>。光标为您做到了。但是,您忘了通过将Null传递到Constructer中来准备它。

请慢慢阅读本文。对你有好处。

您的问题在这里:

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

希望这个帮助。

最新更新