有没有办法在ListView中显示TableLayout

时间:2014-05-19 21:30:24

标签: android xml android-arrayadapter android-tablelayout

我在xml Tablelayout中创建了10多个表,我正在尝试 将它们放在ListView而不是ScrollView中。 表格不可编辑,仅供参考...如何将它们作为 适配器中的数据源还是我需要Inflate或者什么? tnx和抱歉noobish q ..

2 个答案:

答案 0 :(得分:1)


您需要为ListView创建自定义适配器(请搜索此内容)。使用适配器,您需要创建ListView项目布局,您可以在列表视图中为每一行定义表格 希望这可以帮助。 :)

答案 1 :(得分:0)

只需将TableLayout添加到ListView行布局xml即可。

我当前申请的例子:

public class DistributorsListAdapter extends ArrayAdapter<String>{
private final Context context;
private final int rowResourceId;
private final JSONArray data;

public DistributorsListAdapter(Context context, int rowResourceId, String[] ids,   JSONArray data) {
        super(context, rowResourceId, ids);

        this.context = context;
        this.rowResourceId = rowResourceId;
        this.data = data;
}

@Override
public View getView(int position, View v, ViewGroup parent) {

    LayoutInflater inflater = (LayoutInflater) context.getSystemService(
        Context.LAYOUT_INFLATER_SERVICE);

    View rowView = inflater.inflate(rowResourceId, parent, false);

    try {
        JSONObject rowData = data.getJSONObject(position);

        // Заполняем данными строку списка
        setHTMLData(rowView, rowData, "name", R.id.distributor_table_title );
        setHTMLData(rowView, rowData, "address", R.id.distributor_address );
        setData(rowView, rowData, "website", R.id.distributor_website );
        setData(rowView, rowData, "phone", R.id.distributor_phone );
        setData(rowView, rowData, "fax", R.id.distributor_fax );
        setData(rowView, rowData, "email", R.id.distributor_email );

    } catch (JSONException e) {
        e.printStackTrace();
    }

    return rowView;
}

private void setData(View v, JSONObject data, String key, int textViewId) throws JSONException{
    TextView tv = (TextView) v.findViewById(textViewId);
    tv.setText(data.getString(key));
}

private void setHTMLData(View v, JSONObject data, String key, int textViewId) throws JSONException{
    TextView tv = (TextView) v.findViewById(textViewId);
    tv.setText( Html.fromHtml( data.getString(key) ) );
}

}

ListView行的XML布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" 
android:orientation="vertical"
android:paddingLeft="@dimen/activity_horizontal_padding"
android:paddingRight="@dimen/activity_horizontal_padding"

android:paddingBottom="@dimen/distributor_table_vertical_padding">

<TextView
    android:id="@+id/distributor_table_title"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="@dimen/distributor_table_vertical_padding"
    android:gravity="center_horizontal"
    android:textSize="@dimen/distributor_table_title_text_size"
    android:textStyle="bold"/>

<TableLayout 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <TableRow>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/distributor_address"
            android:textSize="@dimen/distributor_table_text_size"/>

        <TextView
            android:id="@+id/distributor_address"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="@dimen/distributor_table_text_size"
            android:layout_marginLeft="@dimen/distributor_table_columns_margin"/>
    </TableRow>

    <TableRow>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/distributor_website"
            android:textSize="@dimen/distributor_table_text_size"/>

        <TextView
            android:id="@+id/distributor_website"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="@dimen/distributor_table_text_size"
            android:layout_marginLeft="@dimen/distributor_table_columns_margin"/>
    </TableRow>

    <TableRow>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/distributor_phone"
            android:textSize="@dimen/distributor_table_text_size"/>

        <TextView
            android:id="@+id/distributor_phone"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="@dimen/distributor_table_text_size"
            android:layout_marginLeft="@dimen/distributor_table_columns_margin"/>
    </TableRow>

    <TableRow>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/distributor_fax"
            android:textSize="@dimen/distributor_table_text_size"/>

        <TextView
            android:id="@+id/distributor_fax"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="@dimen/distributor_table_text_size"
            android:layout_marginLeft="@dimen/distributor_table_columns_margin"/>
    </TableRow>

    <TableRow>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/distributor_email"
            android:textSize="@dimen/distributor_table_text_size"/>

        <TextView
            android:id="@+id/distributor_email"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="@dimen/distributor_table_text_size"
            android:layout_marginLeft="@dimen/distributor_table_columns_margin"/>
    </TableRow>

</TableLayout>

</LinearLayout>