在我的 Android 应用程序中,我试图在列表视图中显示我的数据库表的内容,它运行良好.. 并给出这样的输出,
这是我从表中调用所有名称的方法,
public void getAllPartiesName()
{
try
{
db=new DBAdapter(PartyNamesActivity.this);
db.open();
c=db.getAllPartiesName();
partiesnamelist=new ArrayList<String>();
if(c.getCount()>0)
{
if (c.moveToFirst())
{
do
{
partiesnamelist.add(c.getString(0));
}
while (c.moveToNext());
}
}
else
{
Toast toast=Toast.makeText(getBaseContext(), "No Names Found!", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
c.close();
db.close();
}
catch(Exception e)
{
Toast.makeText(getApplicationContext(), "Exception: "+e.getMessage(), Toast.LENGTH_LONG).show();
}
ArrayAdapter<String> adapter=new ArrayAdapter<String>(PartyNamesActivity.this,android.R.layout.simple_list_item_1,partiesnamelist);
lvpartiesname.setAdapter(adapter);
}
但是当我尝试向 ListView 添加自定义布局时,它会给出错误,实际上我想在左侧显示一个带有每个 List View 名称的图像......所以,我使用了自定义布局......
那个自定义布局是这样的,
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/list_selector"
android:orientation="horizontal"
android:padding="5dip" >
<!-- ListRow Left sied Thumbnail image -->
<LinearLayout android:id="@+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:layout_alignParentLeft="true"
android:background="@drawable/image_bg"
android:layout_marginRight="5dip">
<ImageView
android:id="@+id/list_image"
android:layout_width="50dip"
android:layout_height="50dip"
android:src="@drawable/ic_launcher"/>
</LinearLayout>
<!-- Text of List-->
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/thumbnail"
android:layout_toRightOf="@+id/thumbnail"
android:text="List View Name"
android:textColor="#040404"
android:typeface="sans"
android:textSize="15dip"
android:textStyle="bold"/>
</RelativeLayout>
添加此布局后,我更改了Array Adapter
这样的代码,
ArrayAdapter<String> adapter=new ArrayAdapter<String>(PartyNamesActivity.this,R.layout.list_row,partiesnamelist);
lvpartiesname.setAdapter(adapter);
在这里我遇到了问题,(给出了这个错误)
我的日志猫,
11-01 10:01:43.624: E/AndroidRuntime(10968): FATAL EXCEPTION: main
11-01 10:01:43.624: E/AndroidRuntime(10968): java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
11-01 10:01:43.624: E/AndroidRuntime(10968): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:386)
11-01 10:01:43.624: E/AndroidRuntime(10968): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
11-01 10:01:43.624: E/AndroidRuntime(10968): at android.widget.AbsListView.obtainView(AbsListView.java:2159)
11-01 10:01:43.624: E/AndroidRuntime(10968): at android.widget.ListView.measureHeightOfChildren(ListView.java:1246)
11-01 10:01:43.624: E/AndroidRuntime(10968): at android.widget.ListView.onMeasure(ListView.java:1158)
11-01 10:01:43.624: E/AndroidRuntime(10968): at android.view.View.measure(View.java:15518)
11-01 10:01:43.624: E/AndroidRuntime(10968): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
11-01 10:01:43.624: E/AndroidRuntime(10968): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
11-01 10:01:43.624: E/AndroidRuntime(10968): at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
11-01 10:01:43.624: E/AndroidRuntime(10968): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
11-01 10:01:43.624: E/AndroidRuntime(10968): at android.view.View.measure(View.java:15518)
11-01 10:01:43.624: E/AndroidRuntime(10968): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
11-01 10:01:43.624: E/AndroidRuntime(10968): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
11-01 10:01:43.624: E/AndroidRuntime(10968): at android.view.View.measure(View.java:15518)
11-01 10:01:43.624: E/AndroidRuntime(10968): at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
11-01 10:01:43.624: E/AndroidRuntime(10968): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
11-01 10:01:43.624: E/AndroidRuntime(10968): at android.view.View.measure(View.java:15518)
11-01 10:01:43.624: E/AndroidRuntime(10968): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
11-01 10:01:43.624: E/AndroidRuntime(10968): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
11-01 10:01:43.624: E/AndroidRuntime(10968): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
11-01 10:01:43.624: E/AndroidRuntime(10968): at android.view.View.measure(View.java:15518)
11-01 10:01:43.624: E/AndroidRuntime(10968): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
11-01 10:01:43.624: E/AndroidRuntime(10968): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
11-01 10:01:43.624: E/AndroidRuntime(10968): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
11-01 10:01:43.624: E/AndroidRuntime(10968): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
11-01 10:01:43.624: E/AndroidRuntime(10968): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
11-01 10:01:43.624: E/AndroidRuntime(10968): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
11-01 10:01:43.624: E/AndroidRuntime(10968): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
11-01 10:01:43.624: E/AndroidRuntime(10968): at android.view.Choreographer.doFrame(Choreographer.java:532)
11-01 10:01:43.624: E/AndroidRuntime(10968): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
11-01 10:01:43.624: E/AndroidRuntime(10968): at android.os.Handler.handleCallback(Handler.java:725)
11-01 10:01:43.624: E/AndroidRuntime(10968): at android.os.Handler.dispatchMessage(Handler.java:92)
11-01 10:01:43.624: E/AndroidRuntime(10968): at android.os.Looper.loop(Looper.java:137)
11-01 10:01:43.624: E/AndroidRuntime(10968): at android.app.ActivityThread.main(ActivityThread.java:5041)
11-01 10:01:43.624: E/AndroidRuntime(10968): at java.lang.reflect.Method.invokeNative(Native Method)
11-01 10:01:43.624: E/AndroidRuntime(10968): at java.lang.reflect.Method.invoke(Method.java:511)
11-01 10:01:43.624: E/AndroidRuntime(10968): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
11-01 10:01:43.624: E/AndroidRuntime(10968): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-01 10:01:43.624: E/AndroidRuntime(10968): at dalvik.system.NativeStart.main(Native Method)
11-01 10:01:43.624: E/AndroidRuntime(10968): Caused by: java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TextView
11-01 10:01:43.624: E/AndroidRuntime(10968): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:379)
请帮我,
谢谢。