使用自定义适配器自定义 Android ListView
Posted
技术标签:
【中文标题】使用自定义适配器自定义 Android ListView【英文标题】:Customizing Android ListView with Custom Adapter 【发布时间】:2016-01-01 06:08:16 【问题描述】:我正在使用自定义适配器处理列表视图,但我的代码似乎无法正常工作。我不知道出了什么问题。有人能帮我吗。这是我做的代码:
student_record.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_
android:layout_ >
<LinearLayout android:layout_
android:layout_
android:orientation="vertical"
android:layout_above="@+id/footerlayout"
android:id="@+id/listviewlayout">
<TextView
android:layout_
android:layout_
android:layout_gravity="top|start"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingStart="10dp"
android:paddingRight="10dp"
android:paddingEnd="10dp"
android:background="@drawable/header"
android:text="@string/student_record"
android:textSize="15sp"
android:textColor="#ffffff" />
<ListView
android:id="@+id/listView1"
android:layout_
android:layout_ >
</ListView>
</LinearLayout>
<LinearLayout android:id="@+id/footerlayout"
android:layout_marginTop="3dip"
android:layout_
android:orientation="vertical"
android:layout_
android:gravity="center"
android:layout_alignParentBottom="true">
<Button
android:id="@+id/tableOfSpecificationButton"
android:layout_
android:layout_
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_marginRight="10dp"
android:layout_marginEnd="10dp"
android:background="@drawable/roundedbutton"
android:text="@string/table_of_specifications"
android:textColor="#ffffff"
android:textSize="15sp" />
<Button
android:id="@+id/itemAnalysisButton"
android:layout_
android:layout_
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_marginRight="10dp"
android:layout_marginEnd="10dp"
android:background="@drawable/roundedbutton"
android:text="@string/item_analysis"
android:textColor="#ffffff"
android:textSize="15sp" />
</LinearLayout>
</RelativeLayout>
student_row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_ >
<TextView
android:id="@+id/studentTextView"
android:layout_
android:layout_
android:layout_marginTop="10dp"
android:paddingBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:text="@string/hello_world"
android:textSize="17sp"
android:textStyle="bold" />
<TextView
android:id="@+id/scoreTextView"
android:layout_
android:layout_
android:layout_marginRight="15dp"
android:layout_marginEnd="15dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignBaseline="@+id/studentTextView"
android:text="@string/hello_world"
android:textSize="17sp"
android:textStyle="bold" />
</RelativeLayout>
StudentRecord.java
package com.checkaidev1;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
public class StudentRecord extends Activity
private ListView listView1;
String[] score = new String[] "10", "45", "34", "28",
"45", "30", "19", "33"
;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.student_record);
Student student_data[] = new Student[]
new Student("Ace"),
new Student("Brian"),
new Student("Cathy"),
new Student("Dave"),
new Student("Ethel")
;
StudentRecordCustomAdapter adapter = new StudentRecordCustomAdapter(this, R.layout.student_row, student_data, score);
listView1 = (ListView)findViewById(R.id.listView1);
listView1.setAdapter(adapter);
/**
* @param args
*/
public static void main(String[] args)
// TODO Auto-generated method stub
StudentRecordCustomAdapter.java
package com.checkaidev1;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class StudentRecordCustomAdapter extends ArrayAdapter<Student>
Context ctx;
int layoutResourceId;
Student data[] = null;
String[] score;
LayoutInflater inflater;
public StudentRecordCustomAdapter(Context context, int layoutResourceId, Student data[], String[] score)
super(context, layoutResourceId, data);
// TODO Auto-generated constructor stub
this.ctx = context;
this.layoutResourceId = layoutResourceId;
this.data = data;
this.score = score;
public View getView(int position, View convertView, ViewGroup parent)
View row = convertView;
ViewHolder holder = null;
if(row == null)
LayoutInflater inflater = ((Activity)ctx).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ViewHolder();
holder.studentTextView = (TextView) convertView.findViewById(R.id.studentTextView);
holder.scoreTextView = (TextView) convertView.findViewById(R.id.scoreTextView);
row.setTag(holder);
else
holder = (ViewHolder)row.getTag();
Student student = data[position];
holder.studentTextView.setText(student.name);
holder.scoreTextView = (TextView) convertView.findViewById(R.id.scoreTextView);
return row;
static class ViewHolder
TextView studentTextView;
TextView scoreTextView;
谢谢!
【问题讨论】:
我认为你没有复制适配器类。相反,您将 Activity 复制了两次! 你到底遇到了什么问题? 它只显示“不幸的是应用程序已停止。” 你能发布错误堆栈跟踪吗,因为没有它很难帮助 【参考方案1】:首先,您没有名为 Student 的模型类,即使您将它们声明为 Student。用字符串替换它们。 这样写
String student_data[] = new String[]
new String("Ace"),
new String("Brian"),
new String("Cathy"),
new String("Dave"),
new String("Ethel")
;
StudentRecordCustomAdapter adapter = new StudentRecordCustomAdapter(StudentRecord.this,R.layout.student_row,student_data,score);
然后像这样收到他们
public StudentRecordCustomAdapter(Context context, int layoutResourceId, String data[], String[] score)
super(context, layoutResourceId, data);
// TODO Auto-generated constructor stub
this.ctx = context;
this.layoutResourceId = layoutResourceId;
this.data = data;
this.score = score;
这是错误的..
String student = data[position];
您可以直接访问并将变量设置为 textView.. 喜欢
holder.studentTextView.setText(data[position]);
查看您的代码。更改它们然后再试一次你想做的事......
【讨论】:
【参考方案2】:我建议始终为您需要在应用程序中使用的每个自定义适配器扩展BaseAdapter
。你的代码看起来不错。
【讨论】:
【参考方案3】:你重复这行两次 holder.scoreTextView = (TextView) convertView.findViewById(R.id.scoreTextView);第二个你必须做 holder.scoreTextView.setText(data[position])
【讨论】:
以上是关于使用自定义适配器自定义 Android ListView的主要内容,如果未能解决你的问题,请参考以下文章
在 Android 中使用 SearchableSpinner 和自定义适配器
Android - 通过复选框使用自定义适配器从列表视图中获取项目