修复listview上的JSONResponse

时间:2013-02-28 08:59:59

标签: android json android-listview arrays

我想从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,我可以在控制台中打印它们。现在,我想将这些响应放在listview中。我对此没有任何线索。

任何形式的帮助都将受到赞赏。

3 个答案:

答案 0 :(得分: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);
  }

答案 1 :(得分: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);
    }

接下来,你让这个arraylist成为这样的listview的源:

   // 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文本data。然后,您可以从活动中调用此类以获取数据并在活动中设置列表视图。它可以避免在您的应用中出现不必要的滞后和强制关闭。

更新

您需要为此实现自定义适配器。您需要根据您的要求定义带有布局的单个listitem.xml。然后将它膨胀到列表视图。

关注此tutorial

示例行:

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>

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

enter image description here

答案 2 :(得分: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);

**您还需要覆盖toString() POJO的SearchData方法以返回标题,即

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

3B。另一方面,如果您愿意使用自定义布局,则需要扩展ArrayAdapter并覆盖getView方法,然后以与步骤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