【问题标题】:Customized ListView with TextView, TextView, RadioGroup in AndroidAndroid中带有TextView、TextView、RadioGroup的自定义ListView
【发布时间】:2013-04-08 15:30:25
【问题描述】:

我正在使用ListView 开发一个应用程序,用于创建学生标记列表。

在此应用程序中,列表有 10 名学生。为这些学生进行的考试提供了四个等级。一个学生只能适应四个年级中的一个。

老师会给ListView中的学生评分。

我的xml文件Studentlist.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" >
<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</ListView>
</LinearLayout>

我的 row.xml 文件如下:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="500dp"
android:layout_height="fill_parent"
android:padding="10dp" >

<TableRow>

    <TextView
        android:id="@+id/SNo"
        style="@style/text"
        android:layout_width="40dp"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/StudNo"
        style="@style/text"
        android:layout_width="80dp"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/StudName"
        style="@style/text"
        android:layout_width="180dp"
        android:layout_height="wrap_content" />

    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="160dp"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <RadioButton
            android:id="@+id/radio0"
            android:layout_width="40dp"
            android:layout_height="wrap_content"
            android:checked="true" />

        <RadioButton
            android:id="@+id/radio1"
            android:layout_width="40dp"
            android:layout_height="wrap_content" />

        <RadioButton
            android:id="@+id/radio2"
            android:layout_width="40dp"
            android:layout_height="wrap_content" />

        <RadioButton
            android:id="@+id/radio3"
            android:layout_width="40dp"
            android:layout_height="wrap_content" />
    </RadioGroup>
</TableRow>

现在我正在尝试以下形式的输出:

1 0001 AAAA <RadioButton1> <RadioButton2> <RadioButton3> <RadioButton4>
2 0002 BBBB <RadioButton1> <RadioButton2> <RadioButton3> <RadioButton4>

如何使用适配器功能?

ArrayAdapter&lt;String,String,String,RadiGroup&gt; studList=new ArrayAdapter&lt;String,String,String,RadiGroup&gt;();

我可以这样使用吗?如何为ListView开发定制的Adapter?

建议我最好的解决方案!

【问题讨论】:

  • 为什么要为行使用 TableLayout?常规的 LinearLayout 或 RelativeLayout 也适合。
  • 您需要为此目的定义一个自定义适配器。stackoverflow.com/questions/15832335/…
  • 两者是一样的,对吧.. 我们将列表视图中的列表项替换为自定义布局。这里 LinearLayout 和 TableLayout 做的一样,我们可以使用任何东西。告诉我一个解决方案的建议 LinearLayout..
  • 检查我上面评论中的链接每行将显示两个 textviews 。一个标题和列表项的另一个位置。您可以根据自己的目的自定义相同的内容。我可以在这里发布答案,但这将是多余的。androidhive.info/2012/02/…
  • 是的,我必须使用 CustomAdapter,我是 android 新手。在这个过程中我无法清楚地了解 CustomAdapter 的概念..

标签: android listview android-arrayadapter custom-adapter


【解决方案1】:

这是一个用于列表视图的自定义适配器的示例。根据需要修改它: XML 布局:

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

    <LinearLayout
        android:id="@+id/header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/black" >

        <TextView
            android:id="@+id/item2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:height="30dip"
            android:text="Latest News - Your Healing Place"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#FFFFFF"
            android:width="100dip" />

        <TextView
            android:id="@+id/item1"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:height="30dip"
            android:width="20dip" />

    </LinearLayout>

    <View android:layout_width="fill_parent"
        android:layout_height="1dip"
        android:background="?android:attr/listDivider" />

    <LinearLayout android:id="@+id/layout"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent">

        <ListView
            android:id="@+id/listview"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@color/black" >

        </ListView>

    </LinearLayout>
</LinearLayout>

news.java:

List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
String[] from = new String[] {"details", "date"};
int[] to = new int[] { R.id.item1, R.id.item2};
ListView lv= (ListView)findViewById(R.id.listview);
HashMap<String, String> map = new HashMap<String, String>();
map.put("details", "This is a sample message");
map.put("date", "24-07-2003");
fillMaps.add(map);

SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.grid_item, from, to);
lv.setAdapter(adapter);

【讨论】:

    【解决方案2】:

    您可以使用继承自 ArrayAdapter 的类,并覆盖其 getView() 方法。

    public class StudentAdapter extends ArrayAdapter<Student> {
    
    protected LayoutInflater inflater;
    
        public StudentAdapter(final Context context) {
            super(context, 0);
            inflater = (LayoutInflater) ((Context) context)
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }
    
        public View getView(int position, View convertView, ViewGroup parent) {
    
            // Note: You should optimize here with re-using convertView
    
            View rowView = inflater.inflate(R.layout.user_address_row_layout,
                    parent, false);
            TextView sNo = (TextView) rowView.findViewById(R.id.sNo);
            sNo.setText(getItem(position).number);
                // same for every field of the row
                // ...
    
            return rowView;
        }
    
     }
    

    【讨论】:

    • 同意,但是你应该更有效地检查 convertView 是否已经存在,在这种情况下你只需要更新 View。这里有一个使用 ViewHolder 模式的示例:github.com/Jachu5/RssFeeder_sherlock/blob/master/src/com/…
    • 这就是我在评论中指定“您应该在此处优化 [等]”的原因。我没有使用它来保持示例足够简单。
    猜你喜欢
    • 1970-01-01
    • 2012-05-05
    • 2019-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-16
    • 1970-01-01
    相关资源
    最近更新 更多