2015-10-04 115 views
0

我正在使用自定義適配器的列表視圖,但似乎我的代碼不起作用。我不知道什麼是錯的。有人能幫我嗎。下面是我做的代碼:使用自定義適配器自定義Android ListView

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

} 

謝謝!

+1

我覺得你還沒有複製Adapter類。相反,你複製活動兩次! – Shriram

+0

你遇到什麼問題? –

+0

它只是顯示「不幸的是,應用程序已停止。」 – KatrinaP

回答

0

首先,您沒有名爲Student的模型類即使您將它們聲明爲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]; 

您可以訪問並設置變量直接TextView的。像

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

查看您的代碼。改變它們然後再試一次你想要做的事情...

0

我建議你爲你的應用程序中需要使用的每一個自定義適配器總是延長BaseAdapter。你的代碼看起來很好。

0

重複此行兩次holder.scoreTextView =(TextView)convertView.findViewById(R.id.scoreTextView);第二個齒你必須做holder.scoreTextView.setText(data [position])