从活动中更新片段中的适配器
我正在学习如何使用新的 Fragment 类,并且遇到了我找不到答案的情况。
我设置了一个侦听器,以便当在列表片段中单击某个项目时,该项目应该反映在另一个片段的 GridView 中。
这是侦听器:
public void onBulkScanAttributesFragmentListItemClicked(View v)
{
TextView nameID = (TextView) v.findViewById(R.id.list_item_with_checkbox_dialog_item_name);
String name = (String) nameID.getText();
String value = "Set value";
if (!names.contains(name))
{
names.add(name);
items.add(new Row(name, value));
}
for (Row row : items)
{
Log.d(TAG, row.getRowName());
Log.d(TAG, row.getRowValue());
}
BulkScanChosenAttributes chosen = new BulkScanChosenAttributes(items);
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.chosen_att_fragment, chosen);
transaction.commit();
}
以下是 GridView 片段中的构造函数以及 onActivityCreate 和 onCreateView 方法: 公共类 BulkScanChosenAttributes 扩展 Fragment { 私有静态最终字符串标记=“BulkScanChosenAttributes”; 私有 ArrayList 项目; 私有 GridAttributeAdapter 适配器;
public BulkScanChosenAttributes()
{
items = null;
}
public BulkScanChosenAttributes(ArrayList<Row> items)
{
this.items = items;
}
@Override
public void onActivityCreated(Bundle savedInstance)
{
super.onActivityCreated(savedInstance);
adapter = new GridAttributeAdapter(getActivity(), items);
if (items != null)
{
LayoutInflater gridInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = gridInflater.inflate(R.layout.bulk_scan_chosen, null);
GridView gridView = (GridView) v.findViewById(R.id.gridview);
gridView.setAdapter(adapter);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
if (adapter != null)
adapter.notifyDataSetChanged();
return inflater.inflate(R.layout.bulk_scan_chosen, container, false);
}
下面是 GridView 的 XML 布局 (R.layout.bulk_scan_chosen):
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center" />
GridView 中视图的 XML 布局 (R.layout.grid_item_layout):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/grid_att_name"
android:layout_width = "fill_parent"
android:layout_height = "wrap_content"
android:textColor = "@color/black"
android:textSize = "@dimen/font_large"
android:textStyle = "bold"
android:gravity = "center_horizontal" />
<TextView
android:id="@+id/grid_att_value"
android:layout_width = "fill_parent"
android:layout_height = "wrap_content"
android:textColor = "@color/black"
android:textSize = "@dimen/font_large"
android:textStyle = "bold"
android:gravity = "center_horizontal" />
</LinearLayout>
(这太长了!) 最后,我的适配器:
public class GridAttributeAdapter extends BaseAdapter
{
private int count;
private LayoutInflater inflater;
private Context context;
private String name;
private String value;
private ArrayList<Row> items;
public GridAttributeAdapter(Context context, ArrayList<Row> items)
{
int count = 0;
this.context = context;
this.items = items;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount()
{
return count;
}
@Override
public Object getItem(int position)
{
return position;
}
@Override
public long getItemId(int position)
{
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View v = convertView;
//try to inflate the view
try
{
v = inflater.inflate(R.layout.grid_item_layout, null);
}
catch (NullPointerException npe)
{
npe.printStackTrace();
Log.e(TAG, "view is null");
}
Row row = items.get(position);
try
{
TextView name = (TextView) v.findViewById(R.id.grid_att_name);
TextView value = (TextView) v.findViewById(R.id.grid_att_value);
if (ListMetaData.withDates.contains(ListMetaData.reverseScreenNames.get(row.getRowName())))
{
try
{
Date date = new Date(new Long(row.getRowValue()));
value.setText(date.getMonth() + "/" + date.getDate() + "/" + date.getYear());
}
catch (NumberFormatException nfe)
{
value.setText(row.getRowValue());
}
}
else
{
value.setText(row.getRowValue());
}
name.setText(row.getRowName());
}
catch (NullPointerException npe)
{
npe.printStackTrace();
Log.e(TAG, "something went wrong in TextView assignment");
}
return v;
}
}
适配器已声明在 BulkScanChosenAttributes 中。
我想要更新的 BulkScanChosenAttributes 片段是空白的。我已经检查过以确保代码正在执行,但我对 Fragments 仍然有点不稳定。任何帮助将不胜感激。
I'm learning how to use the new Fragment classes and have run across a situation that I can't find an answer to.
I have a listener set up so that when an item is clicked in a list fragment that item is supposed to be reflected in a GridView in another fragment.
Here is the listener:
public void onBulkScanAttributesFragmentListItemClicked(View v)
{
TextView nameID = (TextView) v.findViewById(R.id.list_item_with_checkbox_dialog_item_name);
String name = (String) nameID.getText();
String value = "Set value";
if (!names.contains(name))
{
names.add(name);
items.add(new Row(name, value));
}
for (Row row : items)
{
Log.d(TAG, row.getRowName());
Log.d(TAG, row.getRowValue());
}
BulkScanChosenAttributes chosen = new BulkScanChosenAttributes(items);
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.chosen_att_fragment, chosen);
transaction.commit();
}
Here are the constructors and onActivityCreate and onCreateView methods from the fragment with the GridView:
public class BulkScanChosenAttributes extends Fragment
{
private static final String TAG = "BulkScanChosenAttributes";
private ArrayList items;
private GridAttributeAdapter adapter;
public BulkScanChosenAttributes()
{
items = null;
}
public BulkScanChosenAttributes(ArrayList<Row> items)
{
this.items = items;
}
@Override
public void onActivityCreated(Bundle savedInstance)
{
super.onActivityCreated(savedInstance);
adapter = new GridAttributeAdapter(getActivity(), items);
if (items != null)
{
LayoutInflater gridInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = gridInflater.inflate(R.layout.bulk_scan_chosen, null);
GridView gridView = (GridView) v.findViewById(R.id.gridview);
gridView.setAdapter(adapter);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
if (adapter != null)
adapter.notifyDataSetChanged();
return inflater.inflate(R.layout.bulk_scan_chosen, container, false);
}
Here is the XML layout for the GridView (R.layout.bulk_scan_chosen):
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center" />
The XML layout for the view in the GridView (R.layout.grid_item_layout):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/grid_att_name"
android:layout_width = "fill_parent"
android:layout_height = "wrap_content"
android:textColor = "@color/black"
android:textSize = "@dimen/font_large"
android:textStyle = "bold"
android:gravity = "center_horizontal" />
<TextView
android:id="@+id/grid_att_value"
android:layout_width = "fill_parent"
android:layout_height = "wrap_content"
android:textColor = "@color/black"
android:textSize = "@dimen/font_large"
android:textStyle = "bold"
android:gravity = "center_horizontal" />
</LinearLayout>
(This is getting long!) And finally, my adapter:
public class GridAttributeAdapter extends BaseAdapter
{
private int count;
private LayoutInflater inflater;
private Context context;
private String name;
private String value;
private ArrayList<Row> items;
public GridAttributeAdapter(Context context, ArrayList<Row> items)
{
int count = 0;
this.context = context;
this.items = items;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount()
{
return count;
}
@Override
public Object getItem(int position)
{
return position;
}
@Override
public long getItemId(int position)
{
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View v = convertView;
//try to inflate the view
try
{
v = inflater.inflate(R.layout.grid_item_layout, null);
}
catch (NullPointerException npe)
{
npe.printStackTrace();
Log.e(TAG, "view is null");
}
Row row = items.get(position);
try
{
TextView name = (TextView) v.findViewById(R.id.grid_att_name);
TextView value = (TextView) v.findViewById(R.id.grid_att_value);
if (ListMetaData.withDates.contains(ListMetaData.reverseScreenNames.get(row.getRowName())))
{
try
{
Date date = new Date(new Long(row.getRowValue()));
value.setText(date.getMonth() + "/" + date.getDate() + "/" + date.getYear());
}
catch (NumberFormatException nfe)
{
value.setText(row.getRowValue());
}
}
else
{
value.setText(row.getRowValue());
}
name.setText(row.getRowName());
}
catch (NullPointerException npe)
{
npe.printStackTrace();
Log.e(TAG, "something went wrong in TextView assignment");
}
return v;
}
}
The adapter is declared within BulkScanChosenAttributes.
The BulkScanChosenAttributes fragment that I want to be updated is blank. I've checked to make sure that the code is executing, but I'm still a little shaky on Fragments. Any help would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题出在我的 BaseAdapter 实现中。 getCount 方法返回 0。修复该问题后,它按预期工作。
The problem was in my BaseAdapter implementation. The getCount method was returning 0. After fixing that, it worked as expected.