How to Build ArrayAdapter from result received from webservice?

半世苍凉 提交于 2019-12-11 18:23:10

问题


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:

The result Received from Web Service:

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

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:

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

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

any Help will be appreciated.


回答1:


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

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

Then create an ArrayList of those objects while you parsing your response:

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

When your ViewHolder would be:

static class ViewHolder 
{
     TextView text;
     TextView date;
}

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



来源:https://stackoverflow.com/questions/15799814/how-to-build-arrayadapter-from-result-received-from-webservice

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!