Domanda

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....

È stato utile?

Soluzione

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

Altri suggerimenti

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

Initially load the your list view with customAdapter using result(By assumption result is arraylist)

   CustomeAdapter adapter = new CustomeAdapter(GetMsgsScreen.this,result);
    lst.setAdapter(adapter);

once you have new value no need to create new result and adapter object, simply append you result in your adapter object,

result.add("append your new result");

then simply adapter.notifyDataSetChanged();

Dont create a new adapter. Keep the original one and pass the new data to it for appending it.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top