1

在我的 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)

请帮我,

谢谢。

4

1 回答 1

0

创建自定义适配器。CutomAdapter 扩展了 ArrayAdapter。在getView您膨胀自定义布局并返回视图。使用 ViewHolder 模式实现平滑滚动和性能。

http://developer.android.com/training/improving-layouts/smooth-scrolling.html

将活动上下文和数组列表传递给自定义适配器的构造函数并在那里使用它们。

public class CustomAdapter extends ArrayAdapter<String>
{

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return names.size();
    }
    LayoutInflater inflater;
    ArrayList<String> naems;
    public CustomAdapter(Context context, ArrayList<String> partiesnamelist) {
        super(context, 0);
        // TODO Auto-generated constructor stub
        inflater = LayoutInflater.from(context);
        names= partiesnamelist;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder holder;
        if(convertView==null)
        {
        convertView = inflater.inflate(R.layout.list_item, parent,false);
            // inflate layout 
        holder = new ViewHolder();
        holder.tv = (TextView) convertView.findViewById(R.id.textView1);
        // use the view object to initialize views
        holder.iv = (ImageView) convertView.findViewById(R.id.list_image);
        convertView.setTag(holder); 
    } else { 
        holder = (ViewHolder) convertView.getTag(); 
    } 
        holder.tv.setText(names.get(position)); 
            // set text to textview usinf position adn arraylist names
            // set image to imageview using position
        return convertView;
    }
static class ViewHolder
{
    TextView tv;
    ImageView iv;
}
}

然后在 Activtiy

CustomAdapter cus = new CustomAdapter(ActivityName.this,partiesnamelist);
lvpartiesname.setAdapter(cus);

list_item.xml // 单一的相对布局就足够了

<?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:orientation="horizontal"
android:padding="5dip" >
<ImageView
        android:id="@+id/list_image"
        android:layout_width="50dip"
        android:layout_height="50dip"
        android:src="@drawable/ic_launcher"/>

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="List View Name"
    android:textColor="#040404"
    android:textSize="15dip"
    android:textStyle="bold"
    android:typeface="sans" />

</RelativeLayout>
于 2013-11-01T11:01:41.947 回答