如何在Android中将数据库的字符串响应一一显示?

发布于 2024-12-04 12:36:07 字数 1351 浏览 0 评论 0原文

我是 Android 的新开发者。我正在我的应用程序中使用soap对象与.net数据库服务进行通信。我从数据库服务器获取字符串形式的响应。但我的意图是,当我从数据库服务器获取字符串作为响应时,然后立即查看为文本视图,类似地,我正在获取图像编码字符串。如何立即获取响应作为视图。我编写的代码如下:

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

根据上面的代码,我一次显示所有消息。但在这种情况下,响应需要时间,然后我就会出现黑屏。这里我的意图是,当我收到字符串响应时,我将在下次类似地将该字符串视为文本视图,直到响应大小完成。

I am new developer in android. 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吻泪 2024-12-11 12:36:07

您可以做的是显示 Listview,而不等待响应,并从后台线程将响应添加到 messages 并调用

mAdapter.notifyDatasetChanged();

这是 LazyLoading 的概念,我希望它能够工作

更新

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

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();
}
});
流云如水 2024-12-11 12:36:07

使用 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.
     }
}

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.
     }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文