Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.editdetails);
    final EditText sqlName = (EditText)findViewById(R.id.editName);
    final EditText sqlNumber = (EditText)findViewById(R.id.editNumber);
    name = CustomListView.name;
    num = CustomListView.number;
    Button bUpdate = (Button)findViewById(R.id.editUpdate);
    Button bView = (Button)findViewById(R.id.editView);
    sqlName.setText(name);
    sqlNumber.setText(num);

    bUpdate.setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {
            nameChanged = sqlName.getText().toString();
            numChanged = sqlNumber.getText().toString();
            GroupDb info = new GroupDb(EditDetails.this);
            info.open();
            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();
            info.close();
            }
        });
    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;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    GroupDb info = new GroupDb(this);
    info.open();
    ArrayList<Contact> searchResults = info.getView();


    final ListView lv = (ListView) findViewById(R.id.srListView);
    lv.setAdapter(new MyCustomBaseAdapter(this, searchResults));
    info.close();

    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);
            alertDialogBuilder
            .setMessage("Select action")
            .setCancelable(false)
            .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");
                    startActivity(contactIntent);
                }
              })

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(R.id.name);

        holder.txtPhone = (TextView) convertView.findViewById(R.id.phone);

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

    holder.txtName.setText(searchArrayList.get(position).getName());

    holder.txtPhone.setText(searchArrayList.get(position).getPhoneNumber());

    holder.status.setText(searchArrayList.get(position).getStatus());

    return convertView;
}

static class ViewHolder {
    TextView txtName;
    TextView txtPhone;
    TextView status;
}
}
like image 927
Usui Takumi Avatar asked Dec 02 '12 19:12

Usui Takumi


5 Answers

create one custom method in BaseAdapter

like:

 public void updateAdapter(ArrayList<Contact> arrylst) {
        this.arrylst= arrylst;

        //and call notifyDataSetChanged
        notifyDataSetChanged();
    }

and this function call where u want to call: e.g

adapterObject.updateAdapter(Here pass ArrayList);

done.

like image 187
Ganesh Katikar Avatar answered Nov 16 '22 18:11

Ganesh Katikar


Simply with BaseAdapter, no need to use context

listsOfNotes.remove(listsOfNotes.get(position));
notifyDataSetChanged();

just place this code on setOnClickListner

like image 41
Vishnu Singh Avatar answered Nov 16 '22 18:11

Vishnu Singh


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
        notifyDataSetChanged();
    }

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

HTH

like image 40
devunwired Avatar answered Nov 16 '22 20:11

devunwired


Thanks guys with solution above worked for me. I am calling listupdate method in every event

  public void updateResults(List<TalebeDataUser> results) {
    talebeList = results;
    //Triggers the list update
    notifyDataSetChanged();
}

and after updatng list I also refreshing my button action in every touch. For instance I have lots of buttons to click in my listview item so every touch chaging others style

    private void setColor(TalebeDataUser talebeDataUser) {
    if (talebeDataUser.isVar()) {
        holder.mVar.setBackgroundResource(R.drawable.aw_secili);
        holder.mGorevli.setBackgroundResource(R.drawable.aw_shadow);
        holder.mYok.setBackgroundResource(R.drawable.aw_shadow);
        holder.mIzinli.setBackgroundResource(R.drawable.aw_shadow);
        holder.mHatimde.setBackgroundResource(R.drawable.aw_shadow);
    } else if (talebeDataUser.isGorevli()) {
        holder.mVar.setBackgroundResource(R.drawable.aw_shadow);
        holder.mGorevli.setBackgroundResource(R.drawable.aw_secili);
        holder.mYok.setBackgroundResource(R.drawable.aw_shadow);
        holder.mIzinli.setBackgroundResource(R.drawable.aw_shadow);
        holder.mHatimde.setBackgroundResource(R.drawable.aw_shadow);
    } else if (talebeDataUser.isYok()) {
        holder.mVar.setBackgroundResource(R.drawable.aw_shadow);
        holder.mGorevli.setBackgroundResource(R.drawable.aw_shadow);
        holder.mYok.setBackgroundResource(R.drawable.aw_secili);
        holder.mIzinli.setBackgroundResource(R.drawable.aw_shadow);
        holder.mHatimde.setBackgroundResource(R.drawable.aw_shadow);
    } else if (talebeDataUser.isIzinli()) {
        holder.mVar.setBackgroundResource(R.drawable.aw_shadow);
        holder.mGorevli.setBackgroundResource(R.drawable.aw_shadow);
        holder.mYok.setBackgroundResource(R.drawable.aw_shadow);
        holder.mIzinli.setBackgroundResource(R.drawable.aw_secili);
        holder.mHatimde.setBackgroundResource(R.drawable.aw_shadow);
    } else if (talebeDataUser.isHatimde()) {
        holder.mVar.setBackgroundResource(R.drawable.aw_shadow);
        holder.mGorevli.setBackgroundResource(R.drawable.aw_shadow);
        holder.mYok.setBackgroundResource(R.drawable.aw_shadow);
        holder.mIzinli.setBackgroundResource(R.drawable.aw_shadow);
        holder.mHatimde.setBackgroundResource(R.drawable.aw_secili);
    }

}

Just an example inside one of my button

  holder.mYok.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //talebeList.remove(currentTalebe);
            setOgrenciNameByDurum(talebeList.get(i));
            talebeList.get(i).setYok(true);
            //setOgrenciNameByDurum(currentTalebe);
            talebeList.get(i).setVar(false);
            talebeList.get(i).setGorevli(false);
            talebeList.get(i).setIzinli(false);
            talebeList.get(i).setHatimde(false);
            updateResults(talebeList);
            setColor(talebeList.get(i));
            //saveCurrentTalebeOnShare(currentTalebe);
        }
    });

talebeList is just of List<MyModel> talebeList

like image 2
Samir Avatar answered Nov 16 '22 19:11

Samir


I solved this issue adding this function to my custom adapter

public void newCursor(Cursor cursor)
 {
  this.cursor=cursor;
  this.notifyDataSetChanged();
 }

From my main class I create a new cursor doing a re-query to database and then send to my custom adapter via this function.

good luck

like image 1
ibai Avatar answered Nov 16 '22 19:11

ibai