android-BaseAdapter实现ListView增删改



★ListView其实是显示多少条目就即时加载,并不是加载所有。BaseAdapter的getView函数第一个参数为position,就是因为传入需要加载的位置,到了什么位置需要显示时就加载
超过频幕位置的条目都被回收


public class MainActivity extends Activity{
int p = -1; //用于存储被点击Item ID
ListView list;
MyAdapter adapter;
Button button1;
Button button2;
Button button3;
Button button4;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button1 = (Button)findViewById(R.id.button1);
        button2 = (Button)findViewById(R.id.button2);
        button3 = (Button)findViewById(R.id.button3);
        button4 = (Button)findViewById(R.id.button4);
        list = (ListView)findViewById(R.id.list);
        adapter = new MyAdapter(this);
        list.setAdapter(adapter);
        button1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
adapter.addText("Koma"); //加入一行文字
}
});
        button2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
adapter.addImage(R.drawable.ic_launcher); //加入一个图片项
}
});
        list.setOnItemClickListener(new OnItemClickListener() {


public void onItemClick(AdapterView arg0, View arg1, int position, long arg3) {
MainActivity.this.p = position;//储存被点击条目的ID
}
       
});
        button3.setOnClickListener(new OnClickListener() {
public void onClick(View v) {//根据ID删除
adapter.remove(p);
p = -1;
}
});
        button4.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
adapter.removeAll();
}
});
    }


} //布局中有4个button,一个ListView,ListView的android:listSelector属性可设置被点击项的样式
BaseAdapter类:




★其他方法可以都不实现,getView和getCount必须写对
public class MyAdapter extends BaseAdapter {
private List textIdList = new ArrayList(); //图片和文字资源的集合
private Context context; //context对象,用于getService

public MyAdapter(Context context){this.context = context;}

public void addText(String text){//添加文字项
textIdList.add(text); //加入集合
notifyDataSetChanged(); //通知adapter数据已发生变化,系统再调用getView更新界面
}
public void addImage(int id){ //添加图片项
textIdList.add(id); //同上
notifyDataSetChanged();
}

public void remove(int index){ //删除指定项
if(index < 0)return;
textIdList.remove(index);
notifyDataSetChanged(); //更新
}

public void removeAll(){
textIdList.clear();
notifyDataSetChanged();
}
@Override
public int getCount() { //返回数据个数,没有正确返回,在更新视图时可能出现异常
return textIdList.size();
}


@Override
public Object getItem(int arg0) {
return textIdList.get(arg0);
}


@Override
public long getItemId(int arg0) {
return arg0;
}


@Override
//在这里建立,返回每个Item的布局,写简单点就返回个TextView都可以
public View getView(int position, View view, ViewGroup parent) {//position为当前操作Item的索引,也可以在这个函数内部为每个Item设置监听器
//装载布局文件
String inflater = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater li = (LayoutInflater)context.getSystemService(inflater);
LinearLayout ll = null;
if(textIdList.get(position) instanceof String)//如果新加入的是文字
{
ll = (LinearLayout)li.inflate(R.layout.text, null);
TextView text = (TextView)ll.findViewById(R.id.text);
text.setText(String.valueOf(textIdList.get(position)));
}
else if(textIdList.get(position) instanceof Integer)//新加入的是图片资源ID
{
ll = (LinearLayout)li.inflate(R.layout.image, null);
ImageView image = (ImageView)ll.findViewById(R.id.image);
image.setImageResource(Integer.parseInt(String.valueOf(textIdList.get(position))));
}
return ll;
}
//text.xml中有一个id为text的TextView,image.xml中有一个id为image的ImageView

你可能感兴趣的:(android-BaseAdapter实现ListView增删改)