简体   繁体   English

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

[英]Customizing Android ListView with Custom Adapter

I'm working on a List View with Custom Adapter but it seems that my code isn't working. 我正在使用“自定义适配器”处理列表视图,但似乎我的代码无法正常工作。 I don't know what is wrong. 我不知道怎么了 Can someone help me. 有人能帮我吗。 Here's the code that I did: 这是我做的代码:

student_record.xml: 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 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 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 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;
        }

}

Thank you! 谢谢!

First of all you don't have model class named Student Even though you are declaring them as Student. 首先,即使您将它们声明为Student,也没有名为Student的模型类。 Replace them with String. 将它们替换为String。 Write in this way 用这种方式写

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

And Recieve them like this 像这样接受他们

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

This is wrong.. 错了

String student = data[position];

You can access and set your variable To textView Directly.. Like 您可以访问并将变量直接设置为TextView。

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

Review your code. 查看您的代码。 Change them And try again what you want to do... 更改它们,然后重试您想做什么...

I suggest to always extend BaseAdapter for every single custom Adapter that you need to use in your App. 我建议始终为需要在App中使用的每个自定义适配器扩展BaseAdapter Your code seems fine. 您的代码似乎很好。

You repeat this line twice holder.scoreTextView = (TextView) convertView.findViewById(R.id.scoreTextView); 您重复此行两次holder.scoreTextView =(TextView)convertView.findViewById(R.id.scoreTextView); second tine you must do holder.scoreTextView.setText(data[position]) 您必须执行的第二个tiner.scoreTextView.setText(data [position])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM