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>
请帮我得到这个程序的工作。 感谢
谢谢!不知道为什么我做了这么愚蠢的事情。 – Backspace
@Backspace不会让自己灰心丧气,它也会发生在很多职业球员身上,继续保持良好状态并编写代码 –
虽然你可以做到这一点'public Result(String subject,String grade)'并且使用'this.subject = subject '和'this.grade = grade' –