2017-03-04 98 views
1

我最近開始爲android編程。我正在一個應用程序中編寫一個簡單的customadapter類。問題在於適配器在列表視圖中不顯示任何文本。 logcat沒有顯示錯誤消息,但該應用程序仍然沒有按預期工作。在列表視圖中不顯示文本的自定義適配器

我包括參比

Reportcard.java

package com.example.jsk.myreportcard; 

import android.os.Bundle; 
import android.support.annotation.Nullable; 
import android.support.v7.app.AppCompatActivity; 
import android.widget.ListView; 

import java.util.ArrayList; 


public class Reportcard extends AppCompatActivity { 
    @Override 
    protected void onCreate(@Nullable Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.reportcard);  

     ArrayList<Result>s=new ArrayList<Result>();  

     s.add(new Result("Science","A")); s.add(new Result("Maths","A"));  

     ReportAdapter adapter=new ReportAdapter(this, s); 
     ListView l=(ListView)findViewById(R.id.list); 
     l.setAdapter(adapter); 
    } 
} 

Result.java

package com.example.jsk.myreportcard; 


public class Result { 

    private String subject,grade; 

    public Result(String subjec, String grad) 
    { 
     subjec=subject; 
     grad=grade; 
    } 

    public String getSubject() 
    { 
     return subject; 
    } 
    public String getGrade() 
    { 
     return grade; 
    } 

} 

ReportAdapter.java我的代碼文件片斷現在

package com.example.jsk.myreportcard; 

import android.app.Activity; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.TextView; 

import java.util.ArrayList; 



public class ReportAdapter extends ArrayAdapter<Result> { 

    public ReportAdapter(Activity context, ArrayList<Result>w) { 
     super(context, 0, w); 

    } 
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 

     View v=convertView; 
     if(v==null) 
     { 
      v= LayoutInflater.from(getContext()).inflate(R.layout.lay_text,parent,false); 
     } 

     Result res=getItem(position); 
     TextView sub=(TextView)v.findViewById(R.id.t1); 
     sub.setText(res.getSubject()); 
     TextView grad=(TextView)v.findViewById(R.id.t2); 
     grad.setText(res.getGrade()); 

//  return super.getView(position, convertView, parent); 
    return v; 
    } 


} 

Ť他佈局文件 -

reportcard.xml

<?xml version="1.0" encoding="utf-8"?> 
<ListView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/list" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

</ListView> 

lay_text.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:padding="16dp" 
    android:id="@+id/lay"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/t1" 
     tools:text="hello"/> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/t2" 
     tools:text="World"/> 

</LinearLayout> 

請幫我得到這個程序的工作。 感謝

回答

2

在構造函數時,局部變量subjecgrad分配的Stingsnull默認值,你是不是分配值

public Result(String subjec, String grad) 
{ 
    subjec=subject; // yourvalue = null 
    grad=grade;  // yourvalue = null 
} 

應該

public Result(String subjec, String grad) 
{ 
    subject=subjec; // datafield = yourvalue 
    grade=grad;  // datafield = yourvalue 
} 
+0

謝謝!不知道爲什麼我做了這麼愚蠢的事情。 – Backspace

+0

@Backspace不會讓自己灰心喪氣,它也會發生在很多職業球員身上,繼續保持良好狀態並編寫代碼 –

+0

雖然你可以做到這一點'public Result(String subject,String grade)'並且使用'this.subject = subject '和'this.grade = grade' –

相關問題