Java 使用自定义适配器自定义Android ListView
我正在使用自定义适配器处理列表视图,但我的代码似乎不起作用。我不知道怎么了。有人能帮我吗。以下是我所做的代码: student_record.xml:Java 使用自定义适配器自定义Android ListView,java,android,listview,android-listview,custom-adapter,Java,Android,Listview,Android Listview,Custom Adapter,我正在使用自定义适配器处理列表视图,但我的代码似乎不起作用。我不知道怎么了。有人能帮我吗。以下是我所做的代码: student_record.xml: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" androi
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_above="@+id/footerlayout"
android:id="@+id/listviewlayout">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
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_height="wrap_content"
android:layout_width="match_parent" >
</ListView>
</LinearLayout>
<LinearLayout android:id="@+id/footerlayout"
android:layout_marginTop="3dip"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_width="fill_parent"
android:gravity="center"
android:layout_alignParentBottom="true">
<Button
android:id="@+id/tableOfSpecificationButton"
android:layout_width="match_parent"
android:layout_height="35dp"
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_width="match_parent"
android:layout_height="35dp"
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>
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;
}
}
谢谢大家! 首先,您没有名为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]);
检查您的代码。更改它们并重试您想要执行的操作…我建议始终为您需要在应用程序中使用的每个自定义适配器扩展BaseAdapter。您的代码似乎很好。将这一行重复两次holder.scoreTextView=TextView convertView.findViewByIdR.id.scoreTextView;第二,您必须执行holder.scoreTextView.setTextdata[position]我想您还没有复制适配器类。相反,您复制了两次活动!你到底遇到了什么问题?它只是不幸地显示应用程序已经停止。你能发布错误stacktrace吗,因为没有它很难帮助你
String student = data[position];
holder.studentTextView.setText(data[position]);