Android的listview问题
其他问答
1
package com.example.listview;
import android.app.Activity;
import android.content.Context;
import android.database.DataSetObserver;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity {
private Context mContext;
private TextView textview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//1找到listview
ListView listview=(ListView)findViewById(R.id.listview);
//2创建一个adapter对象,以为下面已经申明了一个类
mylistadpater mylistadpater1=new mylistadpater();
//3将adapter设置个listview
listview.setAdapter(mylistadpater1);
}
class mylistadpater extends BaseAdapter{//default base simple
@Override
public int getCount() {
// TODO Auto-generated method stub
//集合里面又多少条目,告诉listview要显示多少条目
return 10;
}
//根据position获取listview上面的bean书局,该方法不影响数据的展示,可以先不实现
@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) {
textview=(TextView)findViewById(R.id.TextView);
textview.setText("position:"+position);
return textview;
}
}
}
-
你这个适配器写的,你那个getView()返回的是一个item项,你这个返回一个textview是个什么鬼。因为写
convertview =LayoutInflater.from(MainActivity.this).inflate(R.layout.list_item, null); tv = (TextView) convertView.findViewById(R.id.tv); return convertView;
-
getView( )方法中 写的有问题, 一般需要引入一个listv的布局item
View view=View..inflate(this,R.layout.list_item, null); textview=view.findViewById(R.id.TextView);
然后在设置值;
发表回复