作者:mobiledu2502861763 | 来源:互联网 | 2023-08-24 14:56
基于BaseAdapter实现ListView注意扩展BaseAdapter需要重写以下四个方法getCount();getItem();getItemId();getView();程序清单package xiaoc
基于BaseAdapter 实现ListView
注意扩展BaseAdapter 需要重写以下四个方法
getCount();
getItem();
getItemId();
getView();
程序清单
package xiaocool.net.listviewtest;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
private ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview2);
list=(ListView)this.findViewById(R.id.list1);
/*//定义一个数组
String arr[]={"校酷网络1","校酷网络2","校酷网络3","校酷网络4","校酷网络5"};
//将数组包装成ArrayAdapter
ArrayAdapter adapter=new ArrayAdapter(this,R.layout.array_item,R.id.textView,arr);
list.setAdapter(adapter);*/
BaseAdapter adapter=new BaseAdapter() {
//返回总的列表项数
@Override
public int getCount() {
return 20;
}
@Override
public Object getItem(int position) {
return null;
}
//返回列表项的id
@Override
public long getItemId(int position) {
return 0;
}
//返回每个具体item的样子
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//创建一个LinearLayout,并向其中添加两个控件
LinearLayout layout=new LinearLayout(MainActivity.this);
layout.setOrientation(0);
ImageView p_w_picpathView=new ImageView(MainActivity.this);
p_w_picpathView.setImageResource(R.drawable.syudio);
TextView textView=new TextView(MainActivity.this);
textView.setText("第"+(position+1)+"项");
textView.setTextColor(Color.BLUE);
textView.setTextSize(20);
layout.addView(p_w_picpathView);
layout.addView(textView);
return layout;
}
};
list.setAdapter(adapter);
}
}