我正在尝试在每个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)
答案 0 :(得分: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>
如果您要创建列表视图,则不需要表格标记。这是一个很棒的教程,我用它来帮助我查看列表视图。它有图像和文字,但我相信你可以把它变成你自己的:
答案 1 :(得分: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>