Get the App
SLTechnology News&Howtos  ›  Development  › 

How android uses ListView to display data

Shulou Source: shulou.com Published: 2022-06-02 18:44:49 10月30日 Update

This article mainly introduces android how to use ListView to display data, the article is very detailed, has a certain reference value, interested friends must read it!

Effect picture

1. Define item first

2. Bind the data to listview through the adapter, and click on a row of data to display the number.

Method 1:

List data=new ArrayList (); for (Person person: persons) {HashMap hashMap=new HashMap (); hashMap.put ("id", person.getPersonId ()); hashMap.put ("name", person.getName ()); hashMap.put ("amount", person.getAmount ()); data.add (hashMap) } SimpleAdapter adapter=new SimpleAdapter (this,data,R.layout.item,new String [] {"id", "name", "amount"}, new int [] {R.id.personiddyR.id.nametheR.id.amount}); listView.setAdapter (adapter); listView.setOnItemClickListener (new OnItemClickListener () {@ Override public void onItemClick (AdapterView parent, View view, int position, long id) {ListView myListView= (ListView) parent) HashMap item= (HashMap) myListView.getItemAtPosition (position); Toast.makeText (DBActivity.this, item.get ("id"). ToString (), Toast.LENGTH_SHORT). Show ();}})

Method 2: use cursors

Cursor cursor=personService.getCursor (0,15); / requires that the primary key for designing a table with _ id in the field is _ id SimpleCursorAdapter cursorAdapter=new SimpleCursorAdapter (this,R.layout.item,cursor,new String [] {"_ id", "name", "amount"}, new int [] {R.id.personiddR.id.amount}); listView.setAdapter (cursorAdapter) ListView.setOnItemClickListener (new OnItemClickListener () {/ * parent currently clicked listview object * view current clicked entry * / @ Override public void onItemClick (AdapterView parent, View view, int position, long id) {ListView myListView= (ListView) parent; Cursor data= (Cursor) myListView.getItemAtPosition (position); / / move cursor int m=data.getInt (data.getColumnIndex ("_ id") based on position) Toast.makeText (DBActivity.this, String.valueOf (m), Toast.LENGTH_SHORT). Show ();}}); this is all the content of the article "how android uses ListView to display data". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Tags: Data mode content cursors articles antecedents lines values locations interests fields objects guys buddies effects effects times more items knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple NVidia Shulou Tech Info macOS Shulou Information