Android的BaseAdapter重写以及ListView问题!
我想在ListView中放一个CheckBox以及两个TextView,所以重写了BaseAdapter,但总是报错,求指教!!!!下面是部分源码:publicvoidd...
我想在ListView中放一个CheckBox以及两个TextView,所以重写了BaseAdapter,但总是报错,求指教!!!!
下面是部分源码:
public void deleteWords(){
Log.e("deleteword", "normal in first line");
button3=(Button) findViewById(R.id.button3);
button4=(Button) findViewById(R.id.button4);
mListView=(ListView) findViewById(R.id.listView1);
mListItem=getData();
Log.e("deleteword", "normal in x line");
MyAdapter adapter = new MyAdapter(this);
try{
mListView.setAdapter(adapter);}
catch(Exception e){
e.printStackTrace();
}
setContentView(R.layout.listview1);
button3.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v){
}
});
}
//define a new class ViewHolder
public final class ViewHolder{
TextView english,chinese;
CheckBox checkbox;
}
//define a new adapter
public class MyAdapter extends BaseAdapter{
private final LayoutInflater mInflater;
private MyAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return mListItem.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView == null) {
viewHolder =new ViewHolder();
//指定资源文件
convertView = mInflater.inflate(R.layout.listelement, null);
//映射数据
viewHolder.english = (TextView)convertView.findViewById(R.id.textView1);
viewHolder.chinese = (TextView)convertView.findViewById(R.id.textView2);
viewHolder.checkbox=(CheckBox)convertView.findViewById(R.id.checkBox1);
convertView.setTag(viewHolder);
}else {
viewHolder = (ViewHolder)convertView.getTag();
}
//绑定数据
viewHolder.english.setText((String)mListItem.get(position).get("english"));
viewHolder.chinese.setText((String)mListItem.get(position).get("chinese"));
return convertView;
}
} 展开
下面是部分源码:
public void deleteWords(){
Log.e("deleteword", "normal in first line");
button3=(Button) findViewById(R.id.button3);
button4=(Button) findViewById(R.id.button4);
mListView=(ListView) findViewById(R.id.listView1);
mListItem=getData();
Log.e("deleteword", "normal in x line");
MyAdapter adapter = new MyAdapter(this);
try{
mListView.setAdapter(adapter);}
catch(Exception e){
e.printStackTrace();
}
setContentView(R.layout.listview1);
button3.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v){
}
});
}
//define a new class ViewHolder
public final class ViewHolder{
TextView english,chinese;
CheckBox checkbox;
}
//define a new adapter
public class MyAdapter extends BaseAdapter{
private final LayoutInflater mInflater;
private MyAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return mListItem.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView == null) {
viewHolder =new ViewHolder();
//指定资源文件
convertView = mInflater.inflate(R.layout.listelement, null);
//映射数据
viewHolder.english = (TextView)convertView.findViewById(R.id.textView1);
viewHolder.chinese = (TextView)convertView.findViewById(R.id.textView2);
viewHolder.checkbox=(CheckBox)convertView.findViewById(R.id.checkBox1);
convertView.setTag(viewHolder);
}else {
viewHolder = (ViewHolder)convertView.getTag();
}
//绑定数据
viewHolder.english.setText((String)mListItem.get(position).get("english"));
viewHolder.chinese.setText((String)mListItem.get(position).get("chinese"));
return convertView;
}
} 展开
3个回答
展开全部
我觉得你是不是搞的太麻烦了啊。几行代码就超不多啊。
listView.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item, R.id.textView1, new String[] { "A", "B", "C" }) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View view = super.getView(position, convertView, parent);
TextView textView = (TextView) view.findViewById(R.id.textView2);
textView.setText("position" + position);
return view;
}
});
R.layout.list_item item布局文件,
R.id.textView1这个是主显示的textview。如果要拿其他的控件只要view.finViewById就可以了,不过一定要是这个item layout里的控件啊,别不再这个文件里拿肯定是拿不到了。
listView.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item, R.id.textView1, new String[] { "A", "B", "C" }) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View view = super.getView(position, convertView, parent);
TextView textView = (TextView) view.findViewById(R.id.textView2);
textView.setText("position" + position);
return view;
}
});
R.layout.list_item item布局文件,
R.id.textView1这个是主显示的textview。如果要拿其他的控件只要view.finViewById就可以了,不过一定要是这个item layout里的控件啊,别不再这个文件里拿肯定是拿不到了。
追问
谢谢。。。不过问题在于要对ListView里面的组件有事件响应,所以不得不重写BaseAdapter,所以我主要是想问我这种重写方法到底哪错了,不好意思,是我没说清楚......
报的错误我看不太懂,第一个是“NullPointerException”,不过真心不知道哪有空指针。。。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
错误信息是什么?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询