我在MyTaskFragment中有一个Listview扩展Fragment类,它显示任务名称和任务的状态。当我点击listview的特定行时,我得到一个名为MyTaskonClick的活动,我可以在其中更新任务的状态。问题是,在我关闭活动后,状态不会在MytaskFragment中的列表视图上更新。有谁能提出解决方案。我的代码如下:
public class MyTaskFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_pages, container, false);
Databasehandler db=new Databasehandler(getActivity().getApplicationContext());
final ListView l=(ListView) rootView.findViewById(R.id.listViewmytask);
int count=db.getcounttask();
if(count==0)
{
Toast.makeText(getActivity(), "No Task to Display", Toast.LENGTH_SHORT).show();
}
else
{
if(id.compareTo("empty")!=0)
{
val1.clear();
val1=db.getTaskReceived(id);
ListAdapter k=new SimpleAdapter(getActivity(),val1,R.layout.mytask,new String[]{"TaskId","heading","status"},new int[]{R.id.View1,R.id.View2,R.id.ViewStatus});
l.setAdapter(k);
}
Toast.makeText(getActivity(), "arg2"+val1, Toast.LENGTH_SHORT).show();
l.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
TextView tv2=(TextView) arg1.findViewById(R.id.View1);
String t=tv2.getText().toString();
Intent i4=new Intent(getActivity(),Mytaskonclick.class);
i4.putExtra("taskId",t);
// startActivityForResult(i4, 0);
startActivity(i4);
}
});
}
return rootView;
}
Mytaskonclick.class s1.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
TextView tv=(TextView)findViewById(R.id.sentstatus);
tv.setText("accepted");
queryValues.put("status","accepted");
queryValues.put("taskid",id);
db.updateTask(queryValues);
//Log.w("id",""+id+queryValues);
}
});
答案 0 :(得分:0)
几个选项:
onResume()
并刷新其中的ListView
(但是当您第一次启动活动时,请注意不要刷新它。)startActivityForResult(...)
启动活动并覆盖包含onActivityResult(...)
的活动/片段中的ListView
,并在此时刷新。要刷新,您必须致电listView.notifyDataSetChanged()
。
答案 1 :(得分:0)
使用startActivityForResult(i4, 0);
,然后在onActivityResult()
来电getListView().invalidateVìews();
答案 2 :(得分:0)
使用ArrayAdapter而不是ListAdapter。