[英]Android table layout adding rows with data
我正在嘗試創建一個充滿數據的主頁。 到目前為止,我一直在用數據填充列表視圖,但是對於主頁,有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行等。
表格行也可以具有on Click Event偵聽器。
到目前為止,這是我嘗試過的方法:
<?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();
您是否嘗試過使用自定義listView,如果您要在Json中接收數據,最好使用自定義listView
這是一個很好的教程,它解釋了如何向列表的每一行添加文本和圖像
http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/
另外,如果您需要3列獨立滾動,則可以使用3個listViews,您將看到一個如何在Google apidemos上創建許多scroolviews的示例
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.