0

我做了一个搜索人的活动,它也显示了最近的研究历史。

如果我长按某个历史项目,它会询问我是否要删除它。如果我按“是”,它将删除列表项。

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

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

所以我覆盖了 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();
    }
}
4

2 回答 2

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。

于 2013-06-27T10:56:03.933 回答
0

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

  1. 我使用 OnStart() (在 Fragment 的派生类中)
  2. 我使用 ArrayAdapter 的 setNotifyOnChange():

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() 中。

于 2014-05-30T14:22:50.950 回答