CustomListAdapter类:
它是问题的例子;
public class CustomList extends ArrayAdapter<String>{
private final Activity context;
private final List<FixtureData> list;
public CustomList(Activity context,List<FixtureData>list) {
super(context, R.layout.list_single);
this.context = context;
this.list = list;
}
@Override
public View getView(final int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.list_single, null, true);
TextView txt_date = (TextView) rowView.findViewById(R.id.txt_date);
TextView txt_team1 = (TextView) rowView.findViewById(R.id.txt_team2);
TextView txt_team2 = (TextView) rowView.findViewById(R.id.txt_team1);
txt_date.setText(list.get(position).date.toString());
txt_team1.setText(list.get(position).team1.toString());
txt_team2.setText(list.get(position).team2.toString());
return rowView;
}
}
MainActivity Class :
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
public List<FixtureData> fixtureArray = new ArrayList<FixtureData>();
class FixtureData extends Object{
public String date=null;
public String team1=null;
public String team2=null;
}
FixtureData fixture = new FixtureData();
fixture.date="1990";
fixture.team1="Manchester";
fixture.team2="Barcelona";
fixtureArray.add(fixture);
final CustomList adapter2 = new CustomList(MainActivity.this, fixtureArray);
liste=(ListView)findViewById(R.id.list1));
liste.setAdapter(adapter2);
liste.setItemsCanFocus(true);
liste.setFocusable(false);
liste.setFocusableInTouchMode(false);
liste.setClickable(false);
}}
它只是问题的例子,不要检查意图。
当我使用single ArrayList
我的代码工作时;
final List<String> date = new ArrayList<String>();
final List<Integer> team1= new ArrayList<String>();
final List<Integer> team2= new ArrayList<String>();
但是当我尝试custom ArrayList
这样的时候,它不起作用
public List<FixtureData> fixtureArray = new ArrayList<FixtureData>();
class FixtureData extends Object{
public String date=null;
public String team1=null;
public String team2=null;
}
FixtureData fixture = new FixtureData();
fixture.date="1990";
fixture.team1="Manchester";
fixture.team2="Barcelona";
fixtureArray.add(fixture);
使用此代码给我null ListView, 我怎样才能解决我的问题?
答案 0 :(得分:1)
覆盖getCount
CustomList
方法
@Override
public int getCount()
{
return list.size();
}
OR
变化
super(context, R.layout.list_single);
到
super(context, R.layout.list_single,list);
答案 1 :(得分:0)
您的第一行计划显示
public class CustomList extends ArrayAdapter&lt;字符串&gt; {
应该是:
public class CustomList extends ArrayAdapter&lt; FixtureData&gt; {
您的ListAdapter类应该知道它要处理的Object类型
答案 2 :(得分:0)
看起来代码中有很多内容,但除了Brijesh提到的将ArrayAdapter更改为ArrayAdapter之外,还有一件事似乎是你从未将列表设置为适配器。您发送列表,并拨打超级(上下文,R.layout.list_single),但您没有在该电话中包含您的列表,也不会在以后单独设置。此外,虽然这不是处理此问题的唯一方法,但由于您使用的是具有多个文本视图的自定义列表项视图,因此可能更容易将0作为布局资源的占位符发送 - 这样做&#39 ; s好吧,你稍后在getView()中定义它。 ArrayAdapter的超级构造函数需要一个带有一个文本视图的布局。您可能想查看ArrayAdapter http://developer.android.com/reference/android/widget/ArrayAdapter.html的android开发人员参考页面,这样您就可以看到您的选项是什么以及期望的是什么,但是一个选项就是调用super(context,0,list)。顺便说一句 - 如果你想使用this.list,请确保在调用super之前定义它,而不是在你现在拥有它之后定义它。