Android ListView不会更新onResume

时间:2013-06-27 10:45:56

标签: 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();
    }
}

2 个答案:

答案 0 :(得分: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。

答案 1 :(得分:0)

notifyDataSetChanged()也不适用于我。我能够以不同的方式解决这个问题:

  1. 我使用OnStart()(来自Fragment的派生类)
  2. 我使用ArrayAdapter的setNotifyOnChange():
  3. ListView listView = (ListView) findViewById(R.id.logListView); listView.setAdapter(logAdapter); logAdapter.setNotifyOnChange(true);

    我创建了一次适配器:

    logAdapter = new ArrayAdapter(activity,android.R.layout.simple_list_item_1,activity.logMessages);

    在onViewCreated()中