您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關Android中ListView如何使用,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
Android手機操作系統中,有許多應用比較復雜,對于初學者來說還需要在實踐中去加深對此的理解。在Android ListView的使用較為復雜一點,也就是配置其Adapter,Adapter有幾種,有ArrayAdapter,SimpleAdapter等,首先要生成一個ListView(當然可以使用ListActivity,此Activity整合了ListView)然后用Adapter來設定ListView的顯示數據及布局方式,然后再來響應OnItemClick 事件,或者在ListActivity改寫onListItemClick 響應事件函數。看如下代碼演示了使用ListActivity:
import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import android.app.ListActivity; import android.os.Bundle; import android.view.View; import android.widget.ListView; import android.widget.SimpleAdapter;
public class Test extends ListActivity {
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
this.setTitle(this.mModelData.get(position).get("type").toString());
}
SimpleAdapter adapter = null;
private ArrayList<Map<String, Object>> mModelData = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initModelData();
adapter = new SimpleAdapter
(this, mModelData, android.R.layout.two_line_list_item,
new String[]{"name"}, new int[]{android.R.id.text1});this.setListAdapter(adapter);
//setContentView(R.layout.main);
}
public void initModelData()
{
mModelData = new ArrayList<Map<String, Object>>();
Map<String, Object> item = new HashMap<String,Object>();
item.put("name", "Linux");item.put("type", "OS");
mModelData.add(item);
Map<String, Object> item2 = new HashMap<String,Object>();
item2.put("name", "Android");item2.put("type", "Platform");
mModelData.add(item2);
Map<String, Object> item3 = new HashMap<String,Object>();
item3.put("name", "Tomato");item3.put("type", "Fruit");
mModelData.add(item3);
}
}
下面代碼顯示了,使用Android ListView + Activity:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
public class TestStringList extends Activity implements
OnItemClickListener {@Override
public void onItemClick(AdapterView
> arg0, View arg1, int arg2, long arg3) { this.setTitle("You Click Item:" + String.valueOf(arg2)); } private ListView mListView = null; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); mListView = new ListView(this); mListView.setOnItemClickListener(this); ArrayAdapter<String> adapter = new ArrayAdapter<String> (this,android.R.layout.simple_list_item_checked, new String[]{"ItemA", "ItemB", "ItemC"}); mListView.setAdapter(adapter); this.setContentView(mListView); } }
以上就是Android中ListView如何使用,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。