0

I made an Activity for searching people that also shows history of recent research.

If I long click on an item of the history it asks me if I want to delete it. If I press "Yes" it deletes the list item.

So, I write something and click to "Search" button. This brings me in another Activity with results. Here I click on result so it stores the person info and brings me in the person page.

When I come back I don't see the new person in the history.

So I overwritten onResume() but it still not work and now I cannot delete items from the history list.

Here the code:

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();
    }
}
Egidio Caprino
  • 553
  • 10
  • 18

2 Answers2

1

You haven't set any new data to list view. Thats why your new contact isn't added to the list after notifyDataSetChanged(). You need to add some method into adapter like

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

and then call notifyDataSetChanged(). So the final onResume will be :

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

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

Enjoy.

And using onResume() for this task is bad idea. Is better to use onActivityResult.

Yakiv Mospan
  • 8,174
  • 3
  • 31
  • 35
  • This is how implemented my adapter: http://pastebin.com/xyPREkL8 . I use the internal storage system to insert and retrieve items. When I change the ArrayList it changes in the adapter too. I'm gonna try with onActivityResult(). – Egidio Caprino Jun 27 '13 at 11:21
  • Before trying onActivityResult(), just try to reinit your listview instead of callinig notify. – Yakiv Mospan Jun 27 '13 at 12:04
0

notifyDataSetChanged() didn't work for me either. I was able to solve this a little bit differently:

  1. I use OnStart() (in a derived class from Fragment)
  2. I use setNotifyOnChange() of the ArrayAdapter:

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

I create the adapter once:

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

in onViewCreated().

Ashkan Mobayen Khiabani
  • 33,575
  • 33
  • 102
  • 171
Thomas
  • 816
  • 5
  • 16