自定义适配器不在listview中显示文本

时间:2017-03-04 14:39:29

标签: java android xml listview

我最近开始为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>

请帮助我让这款应用运行起来。 感谢

1 个答案:

答案 0 :(得分:2)

您没有在构造函数中分配值,而是将Stings的默认值,即null分配给局部变量subjecgrad

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
}