0

我想listview从我的JSON回复中添加行。这是我从中获取JSON并在 cosole 中打印它们的代码:

HttpClient hClient = new DefaultHttpClient();
        HttpGet hGet = new HttpGet(
                "APIHere");
        ResponseHandler<String> rHandler = new BasicResponseHandler();
        data = hClient.execute(hGet, rHandler);

        JSONObject rootObj = new JSONObject(data);
        JSONObject searchObj = rootObj.getJSONObject("searchdata");
        JSONArray titlesObj = searchObj.getJSONArray("titles");
        JSONArray descsObj = searchObj.getJSONArray("desc");
        JSONArray linksObj = searchObj.getJSONArray("links");

        for (int i = 0; i < titlesObj.length(); i++) {
            String title = titlesObj.getString(i);
            System.out.println("Titles: " + title);
        }

        for (int i = 0; i < descsObj.length(); i++) {
            String desc = descsObj.getString(i);
            System.out.println("Desc: " + desc);
        }

        for (int i = 0; i < linksObj.length(); i++) {
            String link = linksObj.getString(i);
            System.out.println("Link: " + link);
        }

我正在迭代整个JSONArray,我可以在控制台中打印它们。现在,我想将这些响应放在列表视图中。我对此一无所知。

任何形式的帮助将不胜感激。

4

3 回答 3

1
    HttpClient hClient = new DefaultHttpClient();
    HttpGet hGet = new HttpGet(
            "APIHere");
    ResponseHandler<String> rHandler = new BasicResponseHandler();
    data = hClient.execute(hGet, rHandler);

    JSONObject rootObj = new JSONObject(data);
    JSONObject searchObj = rootObj.getJSONObject("searchdata");
    JSONArray titlesObj = searchObj.getJSONArray("titles");
    JSONArray descsObj = searchObj.getJSONArray("desc");
    JSONArray linksObj = searchObj.getJSONArray("links");

    String[] a = new String[titlesObj.length()];

    String[] b = new String[descsObj.length()];

    String[] c = new String[linksObj.length()];

    for (int i = 0; i < titlesObj.length(); i++) {

    String title = titlesObj.getString(i);

    a[i] = title;

     }

    for (int i = 0; i < descsObj.length(); i++) {

    String desc = descsObj.getString(i);

    b[i] = desc;

    }

    for (int i = 0; i < linksObj.length(); i++) {

    String link = linksObj.getString(i);

   c[i] = link;

   }

  ArrayList<String> al=new ArrayList<String>();


  //if three jsonarrays having same length

  for(i=0;i<linksObj.length();i++)

   {

   al.add(" " +a[i]+" " +b[i]+""+c[i]+"");

   }

   ArrayAdapter<String> adapter=new ArrayAdapter<String>this,android.R.layout.simple_list_item_1,al);

  ListView lv=(ListView)findViewById(R.id.listview);

 lv.setAdapter(adapter);
  }
于 2013-02-28T10:19:55.447 回答
0

获取数组项并将其显示到列表视图中:

ArrayList titles = new ArrayList();
ArrayList descs= new ArrayList();
ArrayList links= new ArrayList();

 for (int i = 0; i < titlesObj.length(); i++) {
        String title = titlesObj.getString(i);
        titles.add(title); 
        System.out.println("Titles: " + title);
    }

    for (int i = 0; i < descsObj.length(); i++) {
        String desc = descsObj.getString(i);
        descs.add(title);
        System.out.println("Desc: " + desc);
    }

    for (int i = 0; i < linksObj.length(); i++) {
        String link = linksObj.getString(i);
        links.add(title);
        System.out.println("Link: " + link);
    }

接下来,您将这个数组列表作为列表视图的源,如下所示:

   // Get a handle to the list views

//get your instance of the listview for titles

        ListView lvTitle = (ListView) findViewById(R.id.ListView01);
     lv.setAdapter(new ArrayAdapter<string>((Your activity class).this,
                android.R.layout.simple_list_item_1, titles ));

//get your instance of the listview for descriptions

ListView lvDesc = (ListView) findViewById(R.id.ListView02);
     lv.setAdapter(new ArrayAdapter<string>((Your activity class).this,
                android.R.layout.simple_list_item_1, descs));

//get your instance of the listview for links

ListView lvLinks = (ListView) findViewById(R.id.ListView03);
     lv.setAdapter(new ArrayAdapter<string>((Your activity class).this,
                android.R.layout.simple_list_item_1, links));

编辑

无论我说什么,都应该已经解决了你的问题。但是,我可以看到您正在使用相同的活动与远程服务器通信以获取数据。我建议最好为此创建一个单独的类来返回 json text data。然后你可以从你的活动中调用这个类来获取数据并在你的活动中设置列表视图。它将避免您的应用程序中不必要的滞后和强制关闭。

更新

您需要为此实现一个自定义适配器。listitem.xml您需要根据您的要求使用 Layouts定义单个。然后将其膨胀到列表视图。

按照本教程

样品行:

list_row.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/list_selector"
android:orientation="horizontal"
android:padding="5dip" >

<!--  ListRow Left sied Thumbnail product image -->
<LinearLayout android:id="@+id/thumbnail"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="3dip"
    android:layout_alignParentLeft="true"
    android:background="@drawable/image_bg"
    android:layout_marginRight="5dip">

    <ImageView
        android:id="@+id/list_image"
        android:layout_width="50dip"
        android:layout_height="50dip"
        android:src="@drawable/someImage"/>

</LinearLayout>

<!-- Your title-->
<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/thumbnail"
    android:layout_toRightOf="@+id/thumbnail"
    android:text="Big Title"
    android:textColor="#040404"
    android:typeface="sans"
    android:textSize="15dip"
    android:textStyle="bold"/>

<!-- Your subtitle -->
<TextView
    android:id="@+id/subtitle"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/title"
    android:textColor="#343434"
    android:textSize="10dip"
    android:layout_marginTop="1dip"
    android:layout_toRightOf="@+id/thumbnail"
    android:text="Smaller sub title" />

<!-- Rightend info -->
<TextView
    android:id="@+id/duration"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@id/title"
    android:gravity="right"
    android:text="info"
    android:layout_marginRight="5dip"
    android:textSize="10dip"
    android:textColor="#10bcc9"
    android:textStyle="bold"/>

 <!-- Rightend Arrow -->
 <ImageView android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/arrow"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"/>

</RelativeLayout>

这将使您的列表行看起来像:

在此处输入图像描述

于 2013-02-28T09:13:05.440 回答
0

1.根据您的需要/响应创建一个 POJO

public class SearchData {
    private String title;
    private String Description;
    private String link;

    //getter & setter methods for each field
}

2.创建一个List<SearchData>对象并通过解析响应来填充它

List<SearchData> list = new ArrayList<SearchData>();
//parse JSON array to SearchData object and add it to list
list.add(searchDataObject);

3a。如果您愿意只显示一个标题(即如果简单的布局就足够了)

ArrayAdapter<SearchData> adapter = new ArrayAdapter<Product>(this, android.R.layout.simple_list_item_1, list);
listview.setAdapter(adapter);

** 你还需要重写 POJO 的toString()方法SearchData来返回 title 即

public class SearchData {
    ....
    @Override
    public String toString() {
        return title;
    }
}

3b。另一方面,如果您愿意使用自定义布局,那么您需要扩展一个ArrayAdapterand overridegetView方法,然后以与我们在步骤 3a 中相同的方式绑定新适配器,除了这次替换ArrayAdapter为自定义适配器

资源:

1 按照 Ram Kiran 的建议解析对 ListView 的 JSON 响应:http: //p-xr.com/android-tutorial-how-to-parse-read-json-data-into-a-android-listview/

2 自定义 ListView 和使用自定义适配器:http ://www.ezzylearning.com/tutorial.aspx?tid=1763429

于 2013-02-28T09:23:37.097 回答