扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
小编给大家分享一下Android编程如何实现带有图标的ListView,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!
成都创新互联公司是一家专注于成都网站设计、成都网站建设与策划设计,枞阳网站建设哪家好?成都创新互联公司做网站,专注于网站建设十载,网设计领域的专业建站公司;建站业务涵盖:枞阳等地区。枞阳做网站价格咨询:18980820575
具体如下:
MainActivity.Java
package com.zxl; import java.util.ArrayList; import java.util.List; import org.w3c.dom.ls.LSException; import android.app.Activity; import android.os.Bundle; import android.view.ContextMenu; import android.view.ContextMenu.ContextMenuInfo; import android.view.MenuItem; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.AdapterContextMenuInfo; import android.widget.ListView; import android.widget.AdapterView.OnItemClickListener; import android.widget.Toast; public class MainActivity extends Activity implements OnItemClickListener { /** Called when the activity is first created. */ ListView lv; Listitems; CustomListViewAdapter adapter; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); lv=(ListView)findViewById(R.id.listView1); items=new ArrayList (); items.add(new ListViewItem(){{ ThumbnailResource=R.drawable.ic_launcher; title="Item1"; SubTitle="Item1 Description"; }}); items.add(new ListViewItem(){{ ThumbnailResource=R.drawable.ic_launcher; title="Item2"; SubTitle="Item2 Description"; }}); items.add(new ListViewItem(){{ ThumbnailResource=R.drawable.ic_launcher; title="Item3"; SubTitle="Item3 Description"; }}); items.add(new ListViewItem(){{ ThumbnailResource=R.drawable.ic_launcher; title="Item4"; SubTitle="Item4 Description"; }}); adapter=new CustomListViewAdapter(this, items); lv.setAdapter(adapter); registerForContextMenu(lv); lv.setOnItemClickListener(this); } class ListViewItem{ public int ThumbnailResource; public String title; public String SubTitle; } public void onItemClick(AdapterView> parent, View view, int position, long id) { // TODO Auto-generated method stub /* ListViewItem item=items.get(position); //删除 items.remove(item); adapter=new CustomListViewAdapter(this, items); lv.setAdapter(adapter);*/ ListViewItem item=items.get(position); Toast.makeText(MainActivity.this, "this is :"+item, Toast.LENGTH_LONG).show(); } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { // TODO Auto-generated method stub super.onCreateContextMenu(menu, v, menuInfo); menu.add("add"); menu.add("update"); menu.add("delete"); } @Override public boolean onContextItemSelected(MenuItem item) { // TODO Auto-generated method stub super.onContextItemSelected(item); if(item.getTitle()=="add"){ Toast.makeText(MainActivity.this, "add", Toast.LENGTH_LONG).show(); items.add(new ListViewItem(){{ ThumbnailResource=R.drawable.ic_launcher; title="Item new"; SubTitle="Item new Description"; }}); adapter=new CustomListViewAdapter(this, items); lv.setAdapter(adapter); } else if(item.getTitle()=="update"){ //do somthing } else if(item.getTitle()=="delete"){ ContextMenuInfo info = item.getMenuInfo(); AdapterView.AdapterContextMenuInfo contextMenuInfo = (AdapterContextMenuInfo) info; // 获取选中行位置 int position = contextMenuInfo.position; ListViewItem itema=items.get(position); items.remove(itema); adapter=new CustomListViewAdapter(this, items); lv.setAdapter(adapter); } return true; } }
CustomListViewAdapter.java
package com.zxl; import java.util.List; import com.zxl.MainActivity.ListViewItem; import android.app.Activity; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; public class CustomListViewAdapter extends BaseAdapter { LayoutInflater inflater; Listitems; public CustomListViewAdapter(Activity context, List items) { super(); this.items = items; this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } public int getCount() { // TODO Auto-generated method stub return items.size(); } public Object getItem(int position) { // TODO Auto-generated method stub return null; } public long getItemId(int position) { // TODO Auto-generated method stub return 0; } public View getView(final int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub //items ListViewItem item=items.get(position); View vi=convertView; if(convertView==null) vi = inflater.inflate(R.layout.item_row, null); ImageView imgThumbnail=(ImageView) vi.findViewById(R.id.imgThumbnail); TextView txtTitle =(TextView) vi.findViewById(R.id.txtTitle); TextView txtSubTitle=(TextView) vi.findViewById(R.id.txtSubTitle); imgThumbnail.setImageResource(item.ThumbnailResource); txtTitle.setText(item.title); txtSubTitle.setText(item.SubTitle); return vi; } }
main.xml
item_row.xml
文件结构:
看完了这篇文章,相信你对“Android编程如何实现带有图标的ListView”有了一定的了解,如果想了解更多相关知识,欢迎关注创新互联行业资讯频道,感谢各位的阅读!
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流