使用自定义适配器自定义Android ListView

时间:2015-10-04 13:42:14

标签: 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"
        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>

student_row.xml

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >


        <TextView
            android:id="@+id/studentTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            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_width="wrap_content"
            android:layout_height="wrap_content"
            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;
        }

}

谢谢!

3 个答案:

答案 0 :(得分:0)

首先,你没有名为Student的模特课,即使你宣称他们是学生。用String替换它们。 用这种方式写

 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];

您可以直接访问和设置变量To textView ..赞

holder.studentTextView.setText(data[position]);

查看您的代码。改变他们再试一次你想做的事情......

答案 1 :(得分:0)

我建议始终为您需要在应用中使用的每个自定义适配器扩展BaseAdapter。你的代码似乎很好。

答案 2 :(得分:0)

你重复这一行两次holder.scoreTextView =(TextView)convertView.findViewById(R.id.scoreTextView);第二个你必须做holder.scoreTextView.setText(data [position])