开发者

How to display string response from db as one by one in Android?

I am new developer in android.开发者_如何学C I am working with soap object in my application for communicate with the .net db services.I am getting response as strings from DB server. But my intention is when I get a string from db server as response then imediatly view as text view similarly I am getting images encoded string.how to get response imedialty as view. I have written code as follows:

String xml="<spGetUserMessages><SearchLocation></SearchLocation><LoginUserID>"+Userid+"</LoginUserID></spGetUserMessages>"; 

I am sending request as XML to db server

The response from db server is in list:

 List<MessageClass> response=new ParseXml().getUserMessages(new Generic().getMessages(xml));

  String messages=new String[response.size()];

  for(int i=0;i<response.size();i++)
         {

           //the response values are saved in messages array
             messages[i]=response.get(i).getmessage();

               } 

I have written a base adapter class in that class we have a method as getView I have implemented as follows:

    public View getView(int position, View convertView, ViewGroup parent) {
    View vi=convertView;


    if(convertView==null)
        vi = inflater.inflate(R.layout.item, null);



     TextView text=(TextView)vi.findViewById(R.id.text);;
     ImageView image=(ImageView)vi.findViewById(R.id.image);

     Log.v("rrrrrrrrrr", "rrrrrrrr"+messages[position]);

     text.setText(messages[position]);
    }

From the above code I am displaying all messages at a time. But in this situation the response is taking time then I am getting blank screen. Here my intention is when I get a string response then I will view that string as text view next time next similarly untill reposnse size has completed.


What you can do is display the Listview without waiting for response and from background thread add the responses to messages and call

mAdapter.notifyDatasetChanged();

This is concept of LazyLoading and I hope it should work

Update

runOnUiThread(new Runnable() {
public void run() {
    adapter.notifyDataSetChanged();
}
});


use the AsyncTask class to work in background process like this way

private String messages[];
class BackgroundTask extends AsyncTask<Void, Void, Void>{

     public void doInBackground(Void... arg){

      List<MessageClass> response=new ParseXml().getUserMessages(new Generic().getMessages(xml));

        messages=new String[response.size()];

       for(int i=0;i<response.size();i++){
       //the response values are saved in messages array
         messages[i]=response.get(i).getmessage();

        } 
     }

     public void postExecute(Void result){
           // here you initialize the listview so the getview() method will call after fetching the response and store into the array.
     }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜