您好我一直在尝试使用片段和列表。 出于某种原因,当应用程序打开时,列表不会显示。 如果你能帮助我找到原因并修复它,我将不胜感激! 我在许多网站上寻找答案,但仍然找不到合适的答案。
package androidarena.tab.Tabs;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.BaseAdapter;
import java.util.ArrayList;
import android.util.Log;
import androidarena.tab.R;
public class Assignments extends Fragment {
ListView list;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View assignmentsV = inflater.inflate(R.layout.assignments_frag, container, false);
ArrayList<AssignmentOBJ> items=new ArrayList<AssignmentOBJ>();
for (int i =0; i<10; i++) {
AssignmentOBJ item=new AssignmentOBJ("A "+i, "A "+i, "A "+i);
items.add(item);
Log.d("loop", "date: "+item.getDate());
}
AssignmentsListAdapter adapter=new AssignmentsListAdapter(getActivity(), items);
list=(ListView) assignmentsV.findViewById(R.id.listView123);
list.setAdapter(adapter);
return assignmentsV;
}
public class AssignmentsListAdapter extends ArrayAdapter<AssignmentOBJ> {
ArrayList<AssignmentOBJ> items;
LayoutInflater layoutInflater;
Activity context;
public AssignmentsListAdapter(Activity context, ArrayList<AssignmentOBJ> items) {
super(context, R.layout.assignments_row);
this.items=items;
this.context=context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView= layoutInflater.inflate(R.layout.assignments_row, null, true);
TextView txtTitle1 = (TextView) convertView.findViewById(R.id.textView2);
TextView txtTitle2 = (TextView) convertView.findViewById(R.id.textView3);
TextView txtTitle3 = (TextView) convertView.findViewById(R.id.textView4);
txtTitle1.setText(items.get(position).getDate());
txtTitle1.setText(items.get(position).getTopic());
txtTitle1.setText(items.get(position).getSubject());
return convertView;
}
@Override
public long getItemId(int position) {
return 0;
}
}
private class AssignmentOBJ {
String date;
String topic;
String subject;
public AssignmentOBJ(String date, String topic, String subject) {
this.date = date;
topic = topic;
subject = subject;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
subject = subject;
}
public String getTopic() {
return topic;
}
public void setTopic(String topic) {
topic = topic;
}
}
}
答案 0 :(得分:1)
问题在这里:
public AssignmentsListAdapter(Activity context, ArrayList<AssignmentOBJ> items) {
super(context, R.layout.assignments_row);
this.items=items;
this.context=context;
}
您忘了将items
传递给超级构造函数
只需更改为:super(context, R.layout.assignments_row, items);