0

我正在尝试创建一个充满数据的主页。到目前为止,我一直在用数据填充列表视图,但是对于主页有 3 大量数据,我不能将它们放在列表视图中,因为它们都是独立滚动的。所以我想创建一个表格布局,所以它是:

header
xml header
table row
xml header 2
table row

xml header 3
table row
table row
table row
table row
table row

我希望每一行都有一个图像和一个文本视图,但也在 xml 标题 3 下面我希望根据数据加载行,所以如果我有 5 条数据,它会加载 5 行等。

表格行也可以有一个点击事件监听器。

这是我到目前为止所尝试的:

<?xml version="1.0" encoding="utf-8"?>


<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/white">

   <include layout="@layout/header" />
   <include layout="@layout/redcell" />

   <TableRow
       android:id="@+id/tableRow1"
       android:layout_width="wrap_content"
       android:layout_height="60dp" >
   </TableRow>

   <include layout="@layout/redcell" />

   <TableRow
       android:id="@+id/tableRow2"
       android:layout_width="wrap_content"
       android:layout_height="60dp" >
   </TableRow>

    <include layout="@layout/redcell" />


    <ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
   />

</TableLayout>

这是我加载数据的地方

public void loadData(){String myhash = buildHmacSignature(apiKey, fixturesFeedURL);





    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(fixturesFeedURL);

    List<NameValuePair> pairs = new ArrayList<NameValuePair>();
    pairs.add(new BasicNameValuePair("requestToken", myhash));
    pairs.add(new BasicNameValuePair("apiUser", apiUser));

    try {
        post.setEntity (new UrlEncodedFormEntity(pairs));
        HttpResponse response = client.execute(post);
        BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
        String json = reader.readLine();
        fulldata = String.valueOf(json);
        Log.v("myApp","newsdata" + fulldata);
        newsList = new ArrayList<String>();
        JSONObject obj = new JSONObject(json);    
        JSONObject objData = obj.getJSONObject("data");
        JSONArray jArray = objData.getJSONArray("news");


           for(int t = 0; t < newsAmount; t++){
               JSONObject newsTitleDict = jArray.getJSONObject(t);


               newsList.add(newsTitleDict.getString("title"));

           }


    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


    setListAdapter ( new ArrayAdapter<String>(this, R.layout.single_item, newsList));

       ListView list = getListView();

        list.setTextFilterEnabled(true);



    }   


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        checkPreferences();
        loadData();
4

1 回答 1

0

您是否尝试过使用自定义 listView,如果您在 Json 中接收数据,最好使用自定义 listView

这是一个很好的教程,解释了如何将文本和图像添加到列表中的每一行

http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/

此外,如果您需要 3 个独立滚动的列,您可以使用 3 个 listViews,您将看到如何在 google apidemos 上制作许多 scroolviews 的示例

于 2012-05-09T21:01:14.673 回答