如何建立ArrayAdapter从web服务接收的结果? [英] How to Build ArrayAdapter from result received from webservice?

查看:257
本文介绍了如何建立ArrayAdapter从web服务接收的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的开发asp.net返回数据类型列表((串)列表)的Web服务,
我使用Ksoap2 API调用上面的Web服务,并从中得到的结果是类似如下:

I am using web service which developed in asp.net that return data type List (List(of String)), I am using Ksoap2 api to call the above Web Service and to get the result from it which is something like the following:

从Web服务接收到其结果是:

anyType{string=username1; string=username2; string=username3; string=username4; }
anyType{string=Text_news1; string=Text_News2; string=Text_News3; string=Text_News4; }
anyType{string=01-Apr-2013; string=01-Apr-2013; string=02-Apr-2013; string=02-Apr-2013; }

收到上面的第一个结果是关于用户名来回谁发布的消息,而第二个结果是针对Text_News本身和时text_News已经公布了用户的日期第三行用户和code我以前得到上面的结果如下:

the first result received above is regarding the usernames fro users who post the news while the second result is for the Text_News itself and the third line for the date when that text_News has been posted by the users, and the code that I used to get the above result is as below:

我曾经在 doInBackground(字符串... PARAMS)

I used In the doInBackground(String...params) method

List<String> data= new ArrayList<String>();
    SoapObject result = (SoapObject)envelope.bodyIn;
    if(result != null)
    {
        data.add(((SoapObject)result.getProperty(0)).getPropertyAsString(0));
            data.add(((SoapObject)result.getProperty(0)).getPropertyAsString(1));
            data.add(((SoapObject)result.getProperty(0)).getPropertyAsString(2));
    }

和结果将通过以下方法进行处理,以得出与上述结果

and the result will be treated by the following method to come up with the above result

protected void onPostExecute(List<String> result){
dialog.dismiss();
if(!result.isEmpty())
 {
   System.out.println("First Element :"+result.get(0));
   System.out.println("Second Element :"+result.get(1));
   System.out.println("Third Element :"+result.get(2));
}
}

但是我真的不知道我怎么能建立一个 ArrayAdapter 将用于插入数据的列表视图以下表格

But I really have no clue how can I build an ArrayAdapter that will used to insert data to list view in the following form

Username1    Text_News1   date1   to be the first Item in listView 
userName2    Text_News2   date2   to be the second item in listView
.........
.......

任何帮助将AP preciated。

any Help will be appreciated.

推荐答案

定义对象为你的自我,说行:

Define an object for your self, say "Row":

Class Row{
    String text;
    String date;
    ....

然后创建这些对象的的ArrayList ,而你解析你的回应:

List<Row> rows = new ArrayList<Row>();
for (all your parsed string data)
{
    Row row = new Row();
    row.setText("your parsed text");
    row.setDate("your parsed date");
    rows.add(row); 
}

最后,创建一个适配器:

Finally create an adapter:

private class CustomAdapter extends ArrayAdapter<Row>
{   
    private ArrayList<Row> list;

    public CustomAdapter(Context context, int textViewResourceId, ArrayList<Row> rowsList) 
    {
        super(context, textViewResourceId, rowsList);
         this.list = new ArrayList<SubTask>();
         this.list.addAll(rowsList);
    }

    public View getView(final int position, View convertView, ViewGroup parent)
    {
        ViewHolder holder = new ViewHolder();

            LayoutInflater inflator = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflator.inflate(R.layout.list_item_new, null);

            holder.text = (TextView) convertView.findViewById(R.id.tvText);
            holder.date = (TextView) convertView.findViewById(R.id.tvDate); 

            holder.text.setText(rows.get(position).getText());
            holder.date.setText(rows.get(position).getDate());
            return convertView;
    }
}

当你的 ViewHolder 是:

static class ViewHolder 
{
     TextView text;
     TextView date;
}

不要忘了创建 list_item_new 这将是您的列表项自定义布局文件。

And don't forget to create list_item_new which will be your list item custom layout file.

这篇关于如何建立ArrayAdapter从web服务接收的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆