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

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

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

于 2013-02-28T09:23:37.097 に答える
0

For fetching the array items and show it into a listview:

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

Next you make this arraylist a source to a listview like this:

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

Edit

Whatever I said that should already solve your question. But, I can see that you are using the same activity to communicate to a remote server to get data. I suggest it would be best if you make a separate class for this which would return the json text data. Then you can call this class from your activity to get data and set the list views in your activity. It would avoid unnecessary lags and force closes in your app.

Update

You need to implement a custom adapter for that. You need to define a single listitem.xml with Layouts according to your requirement. Then inflate it to the list view.

Follow this tutorial.

Sample Row:

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>

which would make your list row look like:

enter image description here

于 2013-02-28T09:13:05.440 に答える