Java 将数据从片段传递到自定义适配器(用于ListView)
我试图将从片段中的捆绑包中检索到的数据发送到ListView的适配器中,以便根据以前的用户选择显示数据 我可以记录正确的数据被发送到我的片段,但是当我尝试将该数据传递到适配器的构造函数时,ListView不会被填充。我不确定这是否是将数据获取到适配器的正确方法,尤其是因为数据不是来自光标 在我的片段中,以下是相关代码:Java 将数据从片段传递到自定义适配器(用于ListView),java,android,listview,android-fragments,adapter,Java,Android,Listview,Android Fragments,Adapter,我试图将从片段中的捆绑包中检索到的数据发送到ListView的适配器中,以便根据以前的用户选择显示数据 我可以记录正确的数据被发送到我的片段,但是当我尝试将该数据传递到适配器的构造函数时,ListView不会被填充。我不确定这是否是将数据获取到适配器的正确方法,尤其是因为数据不是来自光标 在我的片段中,以下是相关代码: public class AddEditRecipeActivityFragment extends Fragment { ListView mListView; private
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;
}
}
当我尝试查看IngreditListAdapter类的日志时,根本没有显示任何内容。这很奇怪,因为我有另一个扩展CursorAdapter的适配器,而这些日志正在工作——可能是因为它们涉及cursor参数
很明显我错过了一些重要的东西,任何帮助都将不胜感激 我不想在这里给出新的答案,但评论行太短,无法扩展我的观点 适配器应该有两件事情要管理的第一个let映像。首先是每个列表视图项的视图。您已经通过添加视图保持架完成了此操作。第二件事是视图的数据。因为您使用的是游标适配器,所以不需要在此适配器内准备列表。光标为你做了这件事。但是,您忘记了通过将null传递给构造函数来准备它 请慢慢读。那对你有好处 你的问题在这里:
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;
}
...
希望有帮助。另一个答案中提到的主要问题是游标参数为null,这会告诉游标适配器您不想在ListView中有任何内容,因此不会显示任何内容 因为您实际上没有使用游标,所以扩展游标适配器不是一个好的选择。扩展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;
}
}
谢谢你的回复。它现在起作用了,除了添加的成分和它们的数量没有被保存到列表中,如果我去添加另一种成分。我需要以某种方式保存ArrayList的状态,尽管我不知道如何在代码中实现它。。。有什么建议吗没有关系。只需要将列表设置为静态,这样就只存在一个实例。
mIngredientListAdapter = new IngredientAdapter(getActivity(), ingredients);
mListView.setAdapter(mIngredientListAdapter);