android listview的使用
程序员文章站
2024-03-24 15:30:46
...
/**
*
*
*
* @author fansxnet
*
*
* @deprecated pojo
*
*
*/
public class Item {
private int i;
private Drawable imge;
public int getI() {
return i;
}
public void setI(int i) {
this.i = i;
}
public Drawable getImge() {
return imge;
}
public void setImge(Drawable imge) {
this.imge = imge;
}
}
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class ListViewTestActivity extends Activity {
private ListView listview;
private List<Item> itemlist;
private Context context;
private ListViewAdapter listadapter;
private Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
itemlist = new ArrayList<Item>();
this.context = this;
button = (Button)findViewById(R.id.button);
button.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
itemlist = getData();
listadapter.addItem(itemlist);
// listadapter.notifyDataSetChanged();
}
});
listview = (ListView) findViewById(R.id.listView);
listview.setOnItemClickListener(new ListItemOnClickListener());
listadapter = new ListViewAdapter(context);
listview.setAdapter(listadapter);
}
private List<Item> getData(){
List<Item> templist = new ArrayList<Item>();
for(int i = 0;i<10;i++){
Item item = new Item();
item.setI(i);
item.setImge(getResources().getDrawable(R.drawable.ic_launcher));
templist.add(item);
}
return templist;
}
class ListViewAdapter extends BaseAdapter {
private List<Item> itemlist;
private Context mContext;
public ListViewAdapter(Context context) {
this.itemlist = new ArrayList<Item>();
this.mContext = context;
}
public void addItem(List<Item> addlist){
itemlist.addAll(addlist);
notifyDataSetChanged();
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return itemlist.size() + 1;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
if (position == this.getCount() - 1) {
return null;
} else {
return itemlist.get(position);
}
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
if (position != this.getCount() - 1) {
return position;
} else {
return -1;
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Item item = new Item();
if(position != this.getCount()-1){
item = itemlist.get(position);
}
if(position == this.getCount()-1){
LayoutInflater flater = (LayoutInflater) context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
convertView = flater.inflate(R.layout.morestatus, null);
return convertView;
}
convertView = LayoutInflater.from(mContext).inflate(R.layout.listviewitem, null);
ImageView imageview = (ImageView)convertView.findViewById(R.id.imageView);
TextView textview = (TextView)convertView.findViewById(R.id.textView);
imageview.setImageDrawable(item.getImge());
textview.setText(String.valueOf(item.getI()));
return convertView;
}
}
class ListItemOnClickListener implements OnItemClickListener{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
long id = arg0.getItemIdAtPosition(arg2);
if(id == -1){
itemlist = getData();
listadapter.addItem(itemlist);
listadapter.notifyDataSetChanged();
}else{
Toast.makeText(context, String.valueOf(id), 5000).show();
}
}
}
}
上一篇: 关于JQ在ajax请求数据前添加加载动画
下一篇: GestureDetector使用
推荐阅读
-
使用递归的思想解决汉诺塔问题
-
基于Android搭建tensorflow lite,实现官网的Demo以及运行自定义tensorflow模型(二)
-
android listview的使用
-
android回收AnimationDrawable动画的每一帧的图片资源,而释放内存
-
使用BezierPath实现简单的画板
-
Java模版引擎velocity的使用 博客分类: velocity velocity
-
dstat命令的使用
-
KVC、KVO探识(一)KVO和KVO的详细使用
-
使用Spring发送基于freemarker模板的邮件 博客分类: Spring SpringFreemarkerJavaMail模板邮件
-
iOS KVO实现原理和FBKVOController的使用