如何在Android中将数据库中的字符串响应逐个显示出来?

5

我是一个新手Android开发者。 我正在使用SoapObject在我的应用程序中与.net db服务进行通信。 我从DB服务器获取的响应是字符串。 但是我的意图是,当我从DB服务器获取字符串作为响应时,立即将其视为文本视图,类似地,我正在获取编码字符串的映像。 如何立即获得响应作为视图。 我已经编写了以下代码:

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

我正在将请求以XML格式发送到数据库服务器

数据库服务器的响应是一个列表:

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

               } 

我已经写了一个基础适配器类,在这个类中我们有一个名为getView的方法,我已经按照以下方式实现:

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

从上面的代码中,我一次性显示所有消息。但在这种情况下,如果响应时间过长,我会得到空白屏幕。我的意图是当我收到一个字符串响应时,我将把该字符串作为文本视图查看,然后依次进行,直到响应大小完成。

2个回答

1
你可以在不等待响应的情况下显示Listview,并从后台线程将响应添加到messages中,然后调用。
mAdapter.notifyDatasetChanged();

这是关于LazyLoading的概念,我希望它能够正常运作。

更新

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

嗨ingsaurabh,我已经创建了LazyLoading mAdater对象并调用了mAdapter.notifyDatasetChanged(); 但是它不适用于立即查看,当我得到一个字符串消息时。请问您能否给出一个好的解决方案? - prasad.gai
更新了答案检查。 - ingsaurabh
嘿,没有区别,同样的事情,没有影响。 - prasad.gai

1
使用AsyncTask类来在后台进程中工作,就像这样。
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.
     }
}

嘿,Pratik,我从后台收到消息,但是没有应用于视图。如果我得到一个“hai”的响应,那么我想立即查看它。请给出好的解决方案。 - prasad.gai

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接