/**
*
* ClassName:基本的ListView例子 Function: TODO ADD FUNCTION Reason: TODO ADD REASON
*
* @author Administrator
* @version
* @since Ver 1.1
* @Date 2015 2015-1-23 上午10:19:40
*
* @see
*/
public class MainActivity extends Activity {
List<InfoDto> datalist = new ArrayList<InfoDto>();
Myadapter myadapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dataInit();
ListView lv = (ListView) findViewById(R.id.listView1);
myadapter = new Myadapter();
lv.setAdapter(myadapter);
lv.setOnItemClickListener(new MyOnItemClick());
lv.setOnItemLongClickListener(new MyOnItmeLongClick());
}
// 短按监听器
public class MyOnItemClick implements OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Toast.makeText(MainActivity.this, "短按监听", Toast.LENGTH_SHORT)
.show();
// TODO Auto-generated method stub
}
}
// 长按监听
public class MyOnItmeLongClick implements OnItemLongClickListener {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
Toast.makeText(MainActivity.this, "长按监听", Toast.LENGTH_SHORT)
.show();
if (datalist.size() != 0) {
datalist.remove(arg2);
myadapter.notifyDataSetChanged();
}
// TODO Auto-generated method stub
return true;
}
}
// 数据初始化
public void dataInit() {
/*
* datalist.add("孙悟饭"); datalist.add("阿克蒙德"); datalist.add("吉安娜");
* datalist.add("伊利丹.怒风"); datalist.add("麦维.影歌");
* datalist.add("泰兰德·语风"); datalist.add("雷克萨"); datalist.add("凯尔萨斯");
* datalist.add("阿尔萨斯"); datalist.add("凯莉根"); datalist.add("马格纳斯");
* datalist.add("巨牙海民"); datalist.add("神灵武士"); datalist.add("巨魔战将");
* datalist.add("混沌骑士"); datalist.add("痛苦之源"); datalist.add("死亡法师");
* datalist.add("小小"); datalist.add("末日使者"); datalist.add("陈");
* datalist.add("潮汐领主"); datalist.add("娜迦海妖"); datalist.add("昆卡");
* datalist.add("戴泽"); datalist.add("剃刀"); datalist.add("圣堂刺客");
* datalist.add("力丸"); datalist.add("米波");
*/
InfoDto inf_1 = new InfoDto();
inf_1.setImgres(R.drawable.juyahaimin);
inf_1.setName("juyahaimin");
inf_1.setWork("Dota三号位英雄");
inf_1.setShortintro("海象神拳,寒冰碎片,冰封魔印");
datalist.add(inf_1);
InfoDto inf_2 = new InfoDto();
inf_2.setImgres(R.drawable.chuanzhang);
inf_2.setName("kunka");
inf_2.setWork("Dota四号位英雄");
inf_2.setShortintro("潮汐使者,洪流,幽灵船");
datalist.add(inf_2);
InfoDto inf_3 = new InfoDto();
inf_3.setImgres(R.drawable.siling);
inf_3.setName("wenyifashi");
inf_3.setWork("Dota一号位英雄");
inf_3.setShortintro("死亡脉冲,竭心光环,施虐之心,死神镰刀");
datalist.add(inf_3);
}
public class Myadapter extends BaseAdapter {
@Override
// 返回数量
public int getCount() {
// TODO Auto-generated method stub
return datalist.size();
}
@Override
// 每一条的对象
public Object getItem(int position) {
// TODO Auto-generated method stub
return datalist.get(position);
}
@Override
// 每一条的ID值
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
// 每一条显示的样子
public View getView(int position, View convertView, ViewGroup parent) {
// 布局引用
LayoutInflater inflater = getLayoutInflater();
convertView = inflater.inflate(R.layout.listitem_base, null);
ImageView im = (ImageView) convertView.findViewById(R.id.touxiang);
TextView tv_name = (TextView) convertView.findViewById(R.id.mingzi);
TextView tv_work = (TextView) convertView.findViewById(R.id.zhiye);
TextView tv_Shortintro = (TextView) convertView
.findViewById(R.id.jianjie);
/* tv_name.setText(datalist.get(position)); */
tv_name.setText(datalist.get(position).getName());
tv_work.setText(datalist.get(position).getWork());
tv_Shortintro.setText(datalist.get(position).getShortintro());
im.setBackgroundResource(datalist.get(position).getImgres());
return convertView;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
SearchView sv = (SearchView) menu.findItem(R.id.search).getActionView();
// 查询文本监听器
sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
// TODO Auto-generated method stub
return false;
}
public boolean onQueryTextChange(String newText) {
searchItem(newText.toString());
// TODO Auto-generated method stub
return false;
}
});
// 还原数据的点击事件
sv.setOnCloseListener(new SearchView.OnCloseListener() {
@Override
public boolean onClose() {
datalist.clear();
dataInit();
myadapter.notifyDataSetChanged();
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
public void searchItem(String name) {
// 临时数据源
ArrayList<InfoDto> mSearchList = new ArrayList<InfoDto>();
for (int i = 0; i < datalist.size(); i++) {
int index = datalist.get(i).getName().indexOf(name);
// 存在匹配的数据
if (index != -1) {
mSearchList.add(datalist.get(i));
}
}
// 那临时数据源赋值给全局数据源
datalist = mSearchList;
// 适配器的 数据源发生改变,导致listview刷新
myadapter.notifyDataSetChanged();
}
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:// 新增
InfoDto newinfo = new InfoDto();
newinfo.setImgres(R.drawable.gangbeishou);
newinfo.setName("gangbeishou");
newinfo.setWork("近战(挨打)");
newinfo.setShortintro("外号:菊花猪,弱点:不能打脸");
// 数据源新增一条数据
datalist.add(newinfo);
myadapter.notifyDataSetChanged();
break;
case R.id.modify:// 修改
datalist.get(0).setName("魅惑魔女");
myadapter.notifyDataSetChanged();
default:
break;
}
// TODO Auto-generated method stub
return super.onMenuItemSelected(featureId, item);
}
}
*
* ClassName:基本的ListView例子 Function: TODO ADD FUNCTION Reason: TODO ADD REASON
*
* @author Administrator
* @version
* @since Ver 1.1
* @Date 2015 2015-1-23 上午10:19:40
*
* @see
*/
public class MainActivity extends Activity {
List<InfoDto> datalist = new ArrayList<InfoDto>();
Myadapter myadapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dataInit();
ListView lv = (ListView) findViewById(R.id.listView1);
myadapter = new Myadapter();
lv.setAdapter(myadapter);
lv.setOnItemClickListener(new MyOnItemClick());
lv.setOnItemLongClickListener(new MyOnItmeLongClick());
}
// 短按监听器
public class MyOnItemClick implements OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Toast.makeText(MainActivity.this, "短按监听", Toast.LENGTH_SHORT)
.show();
// TODO Auto-generated method stub
}
}
// 长按监听
public class MyOnItmeLongClick implements OnItemLongClickListener {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
Toast.makeText(MainActivity.this, "长按监听", Toast.LENGTH_SHORT)
.show();
if (datalist.size() != 0) {
datalist.remove(arg2);
myadapter.notifyDataSetChanged();
}
// TODO Auto-generated method stub
return true;
}
}
// 数据初始化
public void dataInit() {
/*
* datalist.add("孙悟饭"); datalist.add("阿克蒙德"); datalist.add("吉安娜");
* datalist.add("伊利丹.怒风"); datalist.add("麦维.影歌");
* datalist.add("泰兰德·语风"); datalist.add("雷克萨"); datalist.add("凯尔萨斯");
* datalist.add("阿尔萨斯"); datalist.add("凯莉根"); datalist.add("马格纳斯");
* datalist.add("巨牙海民"); datalist.add("神灵武士"); datalist.add("巨魔战将");
* datalist.add("混沌骑士"); datalist.add("痛苦之源"); datalist.add("死亡法师");
* datalist.add("小小"); datalist.add("末日使者"); datalist.add("陈");
* datalist.add("潮汐领主"); datalist.add("娜迦海妖"); datalist.add("昆卡");
* datalist.add("戴泽"); datalist.add("剃刀"); datalist.add("圣堂刺客");
* datalist.add("力丸"); datalist.add("米波");
*/
InfoDto inf_1 = new InfoDto();
inf_1.setImgres(R.drawable.juyahaimin);
inf_1.setName("juyahaimin");
inf_1.setWork("Dota三号位英雄");
inf_1.setShortintro("海象神拳,寒冰碎片,冰封魔印");
datalist.add(inf_1);
InfoDto inf_2 = new InfoDto();
inf_2.setImgres(R.drawable.chuanzhang);
inf_2.setName("kunka");
inf_2.setWork("Dota四号位英雄");
inf_2.setShortintro("潮汐使者,洪流,幽灵船");
datalist.add(inf_2);
InfoDto inf_3 = new InfoDto();
inf_3.setImgres(R.drawable.siling);
inf_3.setName("wenyifashi");
inf_3.setWork("Dota一号位英雄");
inf_3.setShortintro("死亡脉冲,竭心光环,施虐之心,死神镰刀");
datalist.add(inf_3);
}
public class Myadapter extends BaseAdapter {
@Override
// 返回数量
public int getCount() {
// TODO Auto-generated method stub
return datalist.size();
}
@Override
// 每一条的对象
public Object getItem(int position) {
// TODO Auto-generated method stub
return datalist.get(position);
}
@Override
// 每一条的ID值
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
// 每一条显示的样子
public View getView(int position, View convertView, ViewGroup parent) {
// 布局引用
LayoutInflater inflater = getLayoutInflater();
convertView = inflater.inflate(R.layout.listitem_base, null);
ImageView im = (ImageView) convertView.findViewById(R.id.touxiang);
TextView tv_name = (TextView) convertView.findViewById(R.id.mingzi);
TextView tv_work = (TextView) convertView.findViewById(R.id.zhiye);
TextView tv_Shortintro = (TextView) convertView
.findViewById(R.id.jianjie);
/* tv_name.setText(datalist.get(position)); */
tv_name.setText(datalist.get(position).getName());
tv_work.setText(datalist.get(position).getWork());
tv_Shortintro.setText(datalist.get(position).getShortintro());
im.setBackgroundResource(datalist.get(position).getImgres());
return convertView;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
SearchView sv = (SearchView) menu.findItem(R.id.search).getActionView();
// 查询文本监听器
sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
// TODO Auto-generated method stub
return false;
}
public boolean onQueryTextChange(String newText) {
searchItem(newText.toString());
// TODO Auto-generated method stub
return false;
}
});
// 还原数据的点击事件
sv.setOnCloseListener(new SearchView.OnCloseListener() {
@Override
public boolean onClose() {
datalist.clear();
dataInit();
myadapter.notifyDataSetChanged();
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
public void searchItem(String name) {
// 临时数据源
ArrayList<InfoDto> mSearchList = new ArrayList<InfoDto>();
for (int i = 0; i < datalist.size(); i++) {
int index = datalist.get(i).getName().indexOf(name);
// 存在匹配的数据
if (index != -1) {
mSearchList.add(datalist.get(i));
}
}
// 那临时数据源赋值给全局数据源
datalist = mSearchList;
// 适配器的 数据源发生改变,导致listview刷新
myadapter.notifyDataSetChanged();
}
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:// 新增
InfoDto newinfo = new InfoDto();
newinfo.setImgres(R.drawable.gangbeishou);
newinfo.setName("gangbeishou");
newinfo.setWork("近战(挨打)");
newinfo.setShortintro("外号:菊花猪,弱点:不能打脸");
// 数据源新增一条数据
datalist.add(newinfo);
myadapter.notifyDataSetChanged();
break;
case R.id.modify:// 修改
datalist.get(0).setName("魅惑魔女");
myadapter.notifyDataSetChanged();
default:
break;
}
// TODO Auto-generated method stub
return super.onMenuItemSelected(featureId, item);
}
}