今天在写一个listview的时候,遇到一个问题,如下
package com.brookji.funlearn;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class ListViewActivity extends Activity {
private ListView listView;
private MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view);
listView = (ListView)findViewById(R.id.listviewlearn);
ArrayList<String> array = new ArrayList<>();
array.add("jilichuan");
array.add("xumeifang");
adapter = new MyAdapter(this, array);
listView.setAdapter(adapter);
/* TextView view = new TextView(this);
listView.addView(view);
view.setText("child");*/
}
public class MyAdapter extends BaseAdapter{
private ArrayList<String> items;
private Context context;
public MyAdapter(){
items = new ArrayList<>();
}
public MyAdapter(Context context, ArrayList<String> items){
this.context = context;
this.items = items;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return items.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return items.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
convertView = ((LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.listviewlearn_item_title, null);
TextView textView = (TextView)convertView.findViewById(R.id.listviewlearn_title);
textView.setText((String)getItem(position));
return convertView;
}
}
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class ListViewActivity extends Activity {
private ListView listView;
private MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view);
listView = (ListView)findViewById(R.id.listviewlearn);
ArrayList<String> array = new ArrayList<>();
array.add("jilichuan");
array.add("xumeifang");
adapter = new MyAdapter(this, array);
listView.setAdapter(adapter);
/* TextView view = new TextView(this);
listView.addView(view);
view.setText("child");*/
}
public class MyAdapter extends BaseAdapter{
private ArrayList<String> items;
private Context context;
public MyAdapter(){
items = new ArrayList<>();
}
public MyAdapter(Context context, ArrayList<String> items){
this.context = context;
this.items = items;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return items.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return items.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
convertView = ((LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.listviewlearn_item_title, null);
TextView textView = (TextView)convertView.findViewById(R.id.listviewlearn_title);
textView.setText((String)getItem(position));
return convertView;
}
}
}
这段代码是可以运行的,
但是将倒数的代码
convertView = ((LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.listviewlearn_item_title, null);
改为
convertView = ((LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.listviewlearn_item_title, parent);
就会出错,那么就会问:子节点不就是放在listview低下吗?不就是子节点吗?怎么不能这样写呢?
醉深层次的原因我还不知道,但是浅层次的原因是:
在viewgroup中,addview方法不能运用于adapterview,而listview就是一个adapterview,而inflate实现的效果适合add一样的,因此不可以这样,只能设为null。
oncreate方法的最后三行注释掉了,不注释掉,同样会出错!