2013-06-27 88 views
0

我做了一个活动搜索的人也显示了最近的研究历史。Android的ListView不更新onResume

如果我长时间点击一个历史项目,它会询问我是否要删除它。如果按“是”,则删除列表项目。

所以,我写了一些东西,然后点击“搜索”按钮。这带来了另一个带有结果的活动。在这里,我点击结果,以便存储人员信息并将我带入人员页面。

当我回来时,我没有看到历史上的新人。

因此,我覆盖onResume(),但它仍然无法正常工作,现在我无法从历史列表中删除项目。

下面的代码:

package com.lpsmt.proffinder; 

import java.util.List; 

import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.content.res.Resources; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemLongClickListener; 
import android.widget.EditText; 
import android.widget.ListView; 
import android.widget.Toast; 

import com.lpsmt.R; 

public class HomeActivity extends Activity 
{ 
    protected Db db = null; 
    protected List<ProfBean> historyProfs = null; 
    protected ProfListItemAdapter listAdapter = null; 
    protected ListView listView = null; 

    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     this.db = new Db(this); 

     this.setContentView(R.layout.prof_finder_home); 

     this.historyProfs = this.db.getHistory(-1); // -1 means with no limits 
     this.listAdapter = new ProfListItemAdapter(HomeActivity.this, R.id.prof_finder_history_list_view, this.historyProfs); 

     this.listView = (ListView) this.findViewById(R.id.prof_finder_history_list_view); 
     listView.setAdapter(this.listAdapter); 

     listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
       Intent intent = new Intent(HomeActivity.this, ProfPageActivity.class); 
       Bundle bundle = new Bundle(); 
       bundle.putString("profId", HomeActivity.this.historyProfs.get(position).getProfId()); 
       intent.putExtras(bundle); 
       HomeActivity.this.startActivity(intent); 
      } 
     }); 

     listView.setOnItemLongClickListener(new OnItemLongClickListener() { 
      @Override 
      public boolean onItemLongClick(AdapterView<?> parent, View view, 
              int position, long id) 
      { 
       Resources resources = HomeActivity.this.getResources(); 
       String title = resources.getString(R.string.prof_finder_history_delete_title); 
       String message = resources.getString(R.string.prof_finder_history_delete_message); 
       AlertDialog.Builder adb=new AlertDialog.Builder(HomeActivity.this); 
       adb.setTitle(title); 
       adb.setMessage(message); 

       final int positionToRemove = position; 
       String positive = resources.getString(R.string.prof_finder_history_delete_positive); 
       String negative = resources.getString(R.string.prof_finder_history_delete_negative); 
       adb.setNegativeButton(negative, null); 
       adb.setPositiveButton(positive, new AlertDialog.OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) { 
         ProfBean prof = HomeActivity.this.historyProfs.get(positionToRemove); 
         HomeActivity.this.db.deleteProf(prof.getProfId()); 

         HomeActivity.this.historyProfs.remove(positionToRemove); 
         HomeActivity.this.runOnUiThread(new Runnable() { 
          public void run() { 
           HomeActivity.this.listAdapter.notifyDataSetChanged(); 
          } 
         }); 
        }}); 
       adb.show(); 

       return true; 
      } 
     }); 
    } 

    public void searchProf(View view) throws Exception 
    { 
     EditText queryEditText = (EditText) this.findViewById(R.id.prof_finder_search_query); 
     String query = queryEditText.getText().toString().trim(); 
     queryEditText.setText(query); 

     if (query.length() < 3) { 
      String message = this.getResources().getString(R.string.prof_finder_query_too_short); 
      Toast.makeText(this, message, Toast.LENGTH_LONG).show(); 
      return; 
     } 

     Intent intent = new Intent(HomeActivity.this, SearchResultActivity.class); 
     Bundle bundle = new Bundle(); 
     bundle.putString("query", query); 
     intent.putExtras(bundle); 
     this.startActivity(intent); 
    } 

    public void onResume() 
    { 
     super.onResume(); 

     this.historyProfs = this.db.getHistory(-1); 
     this.listAdapter.notifyDataSetChanged(); 
    } 
} 

回答

1

您没有设置任何新的数据列表视图。这就是为什么你的新联系人没有被添加到notifyDataSetChanged()之后的列表中。您需要在适配器中添加一些方法,如

setData(List<ProfBean> data) 
{ 
this.currentAdaptersList= data; 
} 

然后请拨notifyDataSetChanged()。所以最后的onResume将是:

public void onResume() 
{ 
    super.onResume(); 

    this.historyProfs = this.db.getHistory(-1); 
    this.listAdapter.setData(this.historyProfs); 
    this.listAdapter.notifyDataSetChanged(); 
} 

享受。

而使用onResume()这个任务是个坏主意。最好使用onActivityResult。

+0

这是我的适配器如何实现的:http://pastebin.com/xyPREkL8。我使用内部存储系统插入和检索项目。当我更改ArrayList时,它也在适配器中更改。我要用onActivityResult()来尝试。 –

+0

在尝试onActivityResult()之前,只需尝试重新启动listview而不是callinig notify。 –

0

notifyDataSetChanged()也不适用于我。我能解决这个问题有点不同:

  1. 我用的OnStart()(从片段派生类))一个ArrayAdapter的
  2. 我用setNotifyOnChange(:

ListView listView = (ListView) findViewById(R.id.logListView); listView.setAdapter(logAdapter); logAdapter.setNotifyOnChange(true);

我创建适配器一次:

logAdapter =新ArrayAdapter(活性,android.R.layout.simple_list_item_1,activity.logMessages);

in onViewCreated()。