ListView的使用
ListView的使用
公司主营业务:网站建设、成都网站制作、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。成都创新互联是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。成都创新互联推出乐陵免费做网站回馈大家。
ListView
是一个用来展示处于垂直的滚动列表中的items的View。这些Items来自与其相关联的ListAdapter。ListAdapter继承自Adapter
,它是一个链接ListView与 列表后面的数据的桥梁。Frequently that data comes from a Cursor, but that is not required.The ListView can display any data provided that it is wrapped in a ListAdapter.An Adapter object acts as a bridge between an AdapterView
and the underlying data for that view. The Adapter provides access to the dataitems. The Adapter is also responsible for making a View
for each item in the data set.
使用ListView
1.创建ListView
2.创建一个类,该类的对象包含了ListView中的子项中的数据。我们要创建的案例中,一个ListView中的子项包含了两个TextView,一个TextView是大标题,另一个TextView是对大标题的详细描述,如Memory标题下有一个4GB的描述。我们创建一个类,这个类有两个成员变量,分别是描述ListView子项大标题和详细描述的字符串。分别是title和content。如下所示
publicclass MyData { private String title; private String content; public MyData(String title, String content) { this.title = title; this.content = content; } public String getContent() { returncontent; } public String getTitle() { returntitle; } }
3.为ListView中的子项创建布局文件
4.创建自定义的Adapter类来加工自定义的类的对象
//创建适配器,使之能够加工ListView子项背后的数据成为ListView的子项 publicclass MyDataAdapter extendsArrayAdapter{ private int itemLayoutId; // ListView子项的布局文件ID public MyDataAdapter(Context context, intresource,List objects) { super(context, resource, objects); itemLayoutId = resource; } //当ListView中的子项滚动到屏幕中时会调用这个方法来产生ListView的子项view,然后ListView显示之 @Override public View getView(intposition, View convertView, ViewGroup parent) { // 根据position参数得到ListView子项后面的数据 MyData myData = getItem(position); View view; // ListView的子项view ViewHolder viewHolder; // 存储ListView子项view中的view if (convertView == null) // 如果没有缓存 { view = LayoutInflater.from(getContext()).inflate(itemLayoutId, null); viewHolder = new ViewHolder(); // 将得到的view的ID保存在viewHolder的域中,方便以后的使用 // 这种方法是为了优化ListView的显示而作的 viewHolder.tv_title = (TextView) view.findViewById(R.id.tv_title); viewHolder.tv_content = (TextView) view.findViewById(R.id.tv_content); view.setTag(viewHolder); } else { view = convertView; viewHolder = (ViewHolder) view.getTag(); } viewHolder.tv_title.setText(myData.getTitle()); viewHolder.tv_content.setText(myData.getContent()); returnview; } class ViewHolder { TextView tv_title; TextView tv_content; } }
5.在代码中显式ListView
import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ListView; import android.widget.Toast; public class MainActivity extends Activity { private ListView lv_test; private Listitems = new ArrayList (); // 初始化数据 private void init() { MyData item1 = new MyData("手机型号", "MyPhone"); items.add(item1); MyData item2 = new MyData("Android version", "4.4.4"); items.add(item2); MyData item3 = new MyData("Processor", "MSM8974 four cores"); items.add(item3); MyData item4 = new MyData("Memory", "4GB"); items.add(item4); MyData item5 = new MyData("version", "123456"); items.add(item5); MyData item6 = new MyData("Phone Number", "18523415974"); items.add(item6); MyData item7 = new MyData("Name", "Yanguifeng"); items.add(item7); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); lv_test = (ListView) findViewById(R.id.lv_test); MyDataAdapter adapter = new MyDataAdapter(this, R.layout.listview_item_layout, items); lv_test.setAdapter(adapter); lv_test.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView> parent, View view, int position, long id) { MyData myData = (MyData) parent.getItemAtPosition(position); Toast.makeText(view.getContext(), myData.getTitle(), Toast.LENGTH_LONG).show(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
2015-6-17日 创建
网站栏目:ListView的使用
当前地址:http://pwwzsj.com/article/gchghp.html