Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to append latest data to custom base adapter list view in android?

I have implemented an application with Custom base adapter for display data in list view.In my application i have displayed some content to list view by using Custom base adapter that content will come from web service.I have used a button for get the latest data from service.when i get the latest data from service then i would like to append the latest data to list view at bottom without deleting previous data in list view.

I have implemented my application as follows:

  result = new ParseXml().convertMessages(new Model().getMessages("0"));
           count = Integer.parseInt(result.get(0).getMessageID());
           ((Button)findViewById(R.id.oldMessagesButton)).setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                fetchNewMessages();

                }
            }); 



  protected void fetchOldMessages() {
    // TODO Auto-generated method stub
    waitProgreess = ProgressDialog.show(this, "Please wait", "Loading...");
    new Thread() {
        public void run() {
            try {

                Thread.sleep(800);
                } catch (InterruptedException e) {
                }
                newmessageHandler.sendEmptyMessage(0);
        }
    }.start();
}


  private Handler newmessageHandler = new Handler() {

    public void handleMessage(Message msg) {
        super.handleMessage(msg);

        Log.v("000000", "count :"+count);
        if(count>0){
        newmsgResult = new ParseXml().convertMessages(new Model().getNewMessages(""+count));

        CustomeAdapter adapter = new CustomeAdapter(GetMsgsScreen.this,newmsgResult);
        lst.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        count=Integer.parseInt(newmsgResult.get(oldmsgResult.size()-1).getMessageID());
    }
        waitProgreess.dismiss();
    }
};

I have implemented the above code based on user clicking on button count value will be send to service then get latest from service.When i get the latest list from servie the that list would like append to list view.

from the above code i can get only latest previous are deleting.

How can i append latest data(list) to listview in my above case?

please any body help me....

like image 792
prasad.gai Avatar asked Dec 27 '25 16:12

prasad.gai


2 Answers

    CustomeAdapter adapter = new CustomeAdapter(GetMsgsScreen.this,newmsgResult);

define this adapter andnewmsgResult at top of your class..not a local variable inside a class..

Now, whenever you want to update the data in list view, update the values/data in newmsgResult and call adapter.notifyDataSetChanged()

like image 100
Rahul garg Avatar answered Dec 30 '25 06:12

Rahul garg


I think it is happening because you are creating a new custom adapter every time you are getting new messages.. in this line

CustomeAdapter adapter = new CustomeAdapter(GetMsgsScreen.this,newmsgResult);

dont do this and try to use adapter.add(Newmessage); you can use array list to make work easier

ok.. you should have an add function in your custom adapter class for that.. you can do so by adding this to your custom adapter class

private void customadd(String newmsg)
{
  //ArrayList<String> msg=new List<String>; create this array list as a source to your adapter
  msg.add(newmsg);
adapter.notifyDataSetChanged();
}
like image 31
5hssba Avatar answered Dec 30 '25 06:12

5hssba



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!