我在MyTaskFragment中有一個Listview extends Fragment類,它顯示任務名稱和任務的狀態。當我點擊列表視圖的特定行時,我得到一個名爲MyTaskonClick的活動,我可以在其中更新任務的狀態。問題是,在關閉活動後,狀態不會在MytaskFragment的listview中更新。任何人都可以提出解決方案我的代碼如下:Listview沒有得到更新
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(新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);
}
});
listView.notifyDataSetChanged()沒有幫助 – user3256145
這可能意味着您沒有更新模型,或者適配器不知道您在模型中所做的更改。 – thomasg