2

我正在尝试在每个 1 个列表项中创建一个包含 2 个项目的 ListView,但它似乎不起作用,它强制关闭,请帮忙?

这是课程

   package egypt.interfaceAct;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import egypt.database.EgyptDataSource;
import egypt.database.MySQLiteHelper;
import egypt.database.TouringPrograms;
import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;


public class ProgramsList2 extends Activity{

    EgyptDataSource datasource;
    MySQLiteHelper sqlHelper;
    //ArrayAdapter<TouringPrograms> touringProgramAdapter;
    final String ACTIVITY_KEY = "activity";
    final String TIME_KEY = "time";
    ListView list;
    ArrayList<Map<String, String>> al;
    Map<String, String> map;
    SimpleAdapter sa;
    String[] from = {ACTIVITY_KEY, TIME_KEY};
    int[] to = {R.id.tableTextID, R.id.tableText2ID};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.programsmain);
        datasource = new EgyptDataSource(this);
        datasource.open();
        list = (ListView) findViewById(R.id.programsMainList);
        al = new ArrayList<Map<String,String>>();
        Bundle programNum = getIntent().getExtras();
        final String progNum = programNum.getString("ProgNum");
        final String dayNum = programNum.getString("DayNum");

        List<TouringPrograms> Activityvalues = datasource.getTouringPrograms(progNum, dayNum);
        List<TouringPrograms> Timevalues = datasource.getTouringProgramsStartTime(progNum, dayNum);
        Object[] ActivityArray = Activityvalues.toArray();
        Object[] TimeArray = Timevalues.toArray();
        for (int i = 0; i < Activityvalues.size(); i++) {
            map = new HashMap<String, String>();
            map.put(ACTIVITY_KEY, ActivityArray[i].toString());
            map.put(TIME_KEY, TimeArray[i].toString());
            al.add(map);
        }

        sa = new SimpleAdapter(this, al, R.layout.programstable, from, to);
        list.setAdapter(sa);

    }
}

这是其中只有 ListView 的主要布局

   <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

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

        </ListView>
</LinearLayout>

这是解释 ListView 中每个项目的表格布局

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

<TableRow>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tableTextID"
        />

</TableRow>


<TableRow>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tableText2ID"
        />

</TableRow>


</LinearLayout>

有人可以帮帮我吗!我不知道出了什么问题,我一遍又一遍地搜索,我无法弄清楚出了什么问题!

这是 LogCat

06-22 22:57:42.192: E/AndroidRuntime(306): FATAL EXCEPTION: main
06-22 22:57:42.192: E/AndroidRuntime(306): java.lang.RuntimeException: Binary XML file line #7: You must supply a layout_width attribute.
06-22 22:57:42.192: E/AndroidRuntime(306):  at android.content.res.TypedArray.getLayoutDimension(TypedArray.java:491)
06-22 22:57:42.192: E/AndroidRuntime(306):  at android.view.ViewGroup$LayoutParams.setBaseAttributes(ViewGroup.java:3592)
06-22 22:57:42.192: E/AndroidRuntime(306):  at android.view.ViewGroup$MarginLayoutParams.<init>(ViewGroup.java:3672)
06-22 22:57:42.192: E/AndroidRuntime(306):  at android.widget.LinearLayout$LayoutParams.<init>(LinearLayout.java:1395)
06-22 22:57:42.192: E/AndroidRuntime(306):  at android.widget.LinearLayout.generateLayoutParams(LinearLayout.java:1321)
06-22 22:57:42.192: E/AndroidRuntime(306):  at android.widget.LinearLayout.generateLayoutParams(LinearLayout.java:45)
06-22 22:57:42.192: E/AndroidRuntime(306):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:620)
06-22 22:57:42.192: E/AndroidRuntime(306):  at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
06-22 22:57:42.192: E/AndroidRuntime(306):  at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
06-22 22:57:42.192: E/AndroidRuntime(306):  at android.widget.SimpleAdapter.createViewFromResource(SimpleAdapter.java:121)
06-22 22:57:42.192: E/AndroidRuntime(306):  at android.widget.SimpleAdapter.getView(SimpleAdapter.java:114)
06-22 22:57:42.192: E/AndroidRuntime(306):  at android.widget.AbsListView.obtainView(AbsListView.java:1315)
06-22 22:57:42.192: E/AndroidRuntime(306):  at android.widget.ListView.makeAndAddView(ListView.java:1727)
06-22 22:57:42.192: E/AndroidRuntime(306):  at android.widget.ListView.fillDown(ListView.java:652)
06-22 22:57:42.192: E/AndroidRuntime(306):  at android.widget.ListView.fillFromTop(ListView.java:709)
06-22 22:57:42.192: E/AndroidRuntime(306):  at android.widget.ListView.layoutChildren(ListView.java:1580)
06-22 22:57:42.192: E/AndroidRuntime(306):  at android.widget.AbsListView.onLayout(AbsListView.java:1147)
06-22 22:57:42.192: E/AndroidRuntime(306):  at android.view.View.layout(View.java:7035)
06-22 22:57:42.192: E/AndroidRuntime(306):  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
06-22 22:57:42.192: E/AndroidRuntime(306):  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
06-22 22:57:42.192: E/AndroidRuntime(306):  at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
06-22 22:57:42.192: E/AndroidRuntime(306):  at android.view.View.layout(View.java:7035)
06-22 22:57:42.192: E/AndroidRuntime(306):  at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
06-22 22:57:42.192: E/AndroidRuntime(306):  at android.view.View.layout(View.java:7035)
06-22 22:57:42.192: E/AndroidRuntime(306):  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
06-22 22:57:42.192: E/AndroidRuntime(306):  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
06-22 22:57:42.192: E/AndroidRuntime(306):  at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
06-22 22:57:42.192: E/AndroidRuntime(306):  at android.view.View.layout(View.java:7035)
06-22 22:57:42.192: E/AndroidRuntime(306):  at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
06-22 22:57:42.192: E/AndroidRuntime(306):  at android.view.View.layout(View.java:7035)
06-22 22:57:42.192: E/AndroidRuntime(306):  at android.view.ViewRoot.performTraversals(ViewRoot.java:1045)
06-22 22:57:42.192: E/AndroidRuntime(306):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
06-22 22:57:42.192: E/AndroidRuntime(306):  at android.os.Handler.dispatchMessage(Handler.java:99)
06-22 22:57:42.192: E/AndroidRuntime(306):  at android.os.Looper.loop(Looper.java:123)
06-22 22:57:42.192: E/AndroidRuntime(306):  at android.app.ActivityThread.main(ActivityThread.java:4627)
06-22 22:57:42.192: E/AndroidRuntime(306):  at java.lang.reflect.Method.invokeNative(Native Method)
06-22 22:57:42.192: E/AndroidRuntime(306):  at java.lang.reflect.Method.invoke(Method.java:521)
06-22 22:57:42.192: E/AndroidRuntime(306):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-22 22:57:42.192: E/AndroidRuntime(306):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-22 22:57:42.192: E/AndroidRuntime(306):  at dalvik.system.NativeStart.main(Native Method)
4

2 回答 2

2

试试这个:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >


    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tableTextID"
        />




    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tableText2ID"
        />



</LinearLayout>

如果您正在创建列表视图,则不需要表格标签。这是我使用的一个很棒的教程,它对我的​​列表视图有所帮助。它有图片和文字,但我相信你可以自己制作:

http://www.ezzylearning.com/tutorial.aspx?tid=1763429

于 2012-06-22T20:02:04.517 回答
1

您正在尝试在 LinearLayout 中使用 TableRows 并且没有为 TableRows 指定高度或宽度,也许如果您将 XML 更改为:

<TableLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    ... >

    <TableRow 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        ... >

    </TableRow>
</TableLayout>

但这对于两行列表项来说似乎有点过头了,你见过内置的TwoLineListItem吗?

评论中的补充

如果我希望项目中的第二行有两个相邻的文本视图,你知道该怎么做吗?

如果我理解您的问题,您希望每一行看起来像这样:

-----
text1
text2   text3
-----
text1
text2   text3
-----

然后使用 TableLayout 您可以尝试:

<TableLayout ... >
    <TableRow ... >
        <TextView android:id="@+id/text1" .../>
    </TableRow>

    <TableRow ... >
        <TextView android:id="@+id/text2" .../>
        <TextView android:id="@+id/text3" .../>
    </TableRow>

</TableLayout>
于 2012-06-22T19:59:24.733 回答