由于片段列表视图未在allData arrayalist中添加新项目,因此我无法使用listfragment。数据来自服务器,在接口的帮助下传递给listfragment类,结果到达passData ArrayList<SparseArray<String>> allData
。我检查了大小,在passData()
方法中没用,但listfragemtn仍未更新。
我尝试了每种工作方法,使listfragment与notifyDataSetChanged()
和notifydatasetinvalidated()
一起使用,但它不起作用。
片段:
public class ViewFeedback extends ListFragment implements PassDataToFeedback
{
ArrayList<SparseArray<String>> allData;
CustomAdapter customAdapter;
@Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity);
Context context=activity;
((Activity_ViewFeedback)context).setListener(this);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
/* db = new DatabaseManager(getActivity());
lists = db.getAllLists();
pAdapter = new PayListAdapter(getActivity(), lists);
setListAdapter(pAdapter);
registerForContextMenu(getListView());*/
allData=new ArrayList<SparseArray<String>>();
customAdapter=new CustomAdapter(allData);
setListAdapter(customAdapter);
setListShown(true);
setListShownNoAnimation(true);
}
适配器:
public class CustomAdapter extends BaseAdapter
{
ArrayList<SparseArray<String>> allData;
public CustomAdapter(final ArrayList<SparseArray<String>> allData)
{
// TODO Auto-generated constructor stub
if(allData!=null && allData.size()>0)
this.allData=allData;
else
this.allData=new ArrayList<SparseArray<String>>();
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return allData.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
ViewHolder viewHolder=null;
if(arg1==null)
{
arg1=LayoutInflater.from(arg2.getContext()).inflate(R.layout.row_layout_view_feedback, arg2, false);
viewHolder=new ViewHolder(arg1);
arg1.setTag(viewHolder);
}else
viewHolder=(ViewHolder)arg1.getTag();
viewHolder.tv_msg.setText(allData.get(arg0).get(0));
viewHolder.tv_from.setText(allData.get(arg0).get(1));
viewHolder.tv_from.setSelected(true);
viewHolder.tv_when.setText(allData.get(arg0).get(2));
viewHolder.tv_when.setSelected(true);
return arg1;
}
public class ViewHolder
{
TextView tv_msg;
TextView tv_from;
TextView tv_when;
public ViewHolder(final View view)
{
tv_msg=(TextView)view.findViewById(R.id.avf_tv_email);
tv_from=(TextView)view.findViewById(R.id.avf_tv_from);
tv_when=(TextView)view.findViewById(R.id.avf_tv_when);
}
}
}
@Override
public void passData(ArrayList<SparseArray<String>> allData) {
// TODO Auto-generated method stub
if(allData!=null)
{
if(this.allData.size()>0)
{
this.allData.clear();
customAdapter.notifyDataSetInvalidated();
}
this.allData.addAll(allData);
customAdapter.notifyDataSetChanged();
}
}
}
片段布局:
<fragment
android:id="@+id/myfragment"
android:name="android.support.v4.app.ListFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/avf_layout_linear"
class="com.fragments.ViewFeedback" />
答案 0 :(得分:1)
问题是您通过调用this.allData=new ArrayList<SparseArray<String>>();
初始化allData
列表并将其设置为适配器的数据源后,您绝不能将引用更改为新列表。
不需要进行空检查,因为在将其传递给适配器之前对其进行初始化,并且即使它是一个空列表,也应设置allData
数据源,它将位于创作的时间。
保证不会为null,因为您在将此文件初始化之前将其提供给CustomAdapter
构造函数:
allData=new ArrayList<SparseArray<String>>();
customAdapter=new CustomAdapter(allData);
此外,以空列表作为适配器的数据源开始是完全有效的(它不会在该点显示任何行)。将数据添加到allData
并致电notifyDataSetChanged()
后,系统会在该点填充该列表。
public CustomAdapter(final ArrayList<SparseArray<String>> allData)
{
// TODO Auto-generated constructor stub
//if(allData!=null && allData.size()>0) //no need for this
this.allData=allData;
//This was your problem, remove this code:
//else
//this.allData=new ArrayList<SparseArray<String>>();
}
然后,您可以根据需要修改allData
,并调用notifyDataSetChanged()
来更新ListView。