Usui Takumi Usui Takumi - 4 months ago 36x
Android Question

how to refresh custom listview using baseadapter in android

sir, how can i refresh my custom listview using baseadapter. i don't know what to place, or where to place it in my code. please help me. thanks in advance

public class EditDetails extends Activity{
public String nameChanged;
public String numChanged;
public String name;
public String num;
public void onCreate(Bundle savedInstanceState) {
final EditText sqlName = (EditText)findViewById(;
final EditText sqlNumber = (EditText)findViewById(;
name =;
num = CustomListView.number;
Button bUpdate = (Button)findViewById(;
Button bView = (Button)findViewById(;

bUpdate.setOnClickListener(new OnClickListener() {

public void onClick(View arg0) {
nameChanged = sqlName.getText().toString();
numChanged = sqlNumber.getText().toString();
GroupDb info = new GroupDb(EditDetails.this);;
long rowid = info.getRowId(name, num);
info.updateNameNumber(rowid, nameChanged, numChanged);
ArrayList<Contact> searchResults = info.getView();
MyCustomBaseAdapter mcba = new MyCustomBaseAdapter(EditDetails.this, searchResults);
Toast.makeText(getApplicationContext(), "Update Successful!", Toast.LENGTH_LONG).show();
bView.setOnClickListener(new OnClickListener() {

public void onClick(View arg0) {
Intent intent = new Intent();
intent.setClass(EditDetails.this, CustomListView.class);

startActivityForResult(intent, 0);


here is where i displayed my listview

public class CustomListView extends Activity {
final Context context = this;
public static String name;
public static String number;
public void onCreate(Bundle savedInstanceState) {
GroupDb info = new GroupDb(this);;
ArrayList<Contact> searchResults = info.getView();

final ListView lv = (ListView) findViewById(;
lv.setAdapter(new MyCustomBaseAdapter(this, searchResults));

lv.setOnItemClickListener(new OnItemClickListener() {

public void onItemClick(AdapterView<?> a, View v, int position, long id) {
// TODO Auto-generated method stub
Object o = lv.getItemAtPosition(position);
final Contact fullObject = (Contact)o;
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
.setMessage("Select action")
.setPositiveButton("Edit", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
Toast.makeText(getApplicationContext(), "Edit ", Toast.LENGTH_LONG).show();
name = fullObject.getName();
number = fullObject.getPhoneNumber();
Intent contactIntent = new Intent("myfolder.proj.EDITDETAILS");

and here is my baseadapter class

public class MyCustomBaseAdapter extends BaseAdapter {
private static ArrayList<Contact> searchArrayList;

private LayoutInflater mInflater;

public MyCustomBaseAdapter(Context context, ArrayList<Contact> results) {
searchArrayList = results;
mInflater = LayoutInflater.from(context);

public int getCount() {
return searchArrayList.size();

public Object getItem(int position) {
return searchArrayList.get(position);

public long getItemId(int position) {
return position;

public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.custom_row_view, null);
holder = new ViewHolder();
holder.txtName = (TextView) convertView.findViewById(;

holder.txtPhone = (TextView) convertView.findViewById(;

holder.status = (TextView) convertView.findViewById(;
} else {
holder = (ViewHolder) convertView.getTag();




return convertView;

static class ViewHolder {
TextView txtName;
TextView txtPhone;
TextView status;


Two options: either hold onto the reference for the ArrayList that you passed into the constructor so you can modify the actual list data later (since the list isn't copied, modifying the data outside the Adapter still updates the pointer the Adapter is referencing), or rewrite the Adapter to allow the list to be reset to another object.

In either case, after the ArrayList has changed, you must call notifyDataSetChanged() to update your ListView with the changes. This can be done inside or outside the adapter. So, for example:

public class MyCustomBaseAdapter extends BaseAdapter {
    //TIP: Don't make this static, that's just a bad idea
    private ArrayList<Contact> searchArrayList;

    private LayoutInflater mInflater;

    public MyCustomBaseAdapter(Context context, ArrayList<Contact> initialResults) {
        searchArrayList = initialResults;
        mInflater = LayoutInflater.from(context);

    public void updateResults(ArrayList<Contact> results) {
        searchArrayList = results;
        //Triggers the list update

    /* ...The rest of your code that I failed to copy over... */