您好,登錄后才能下訂單哦!
//layout 布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@android:id/list"
/>
</RelativeLayout>
//代碼
package com.example.test.testlistview;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class ListViewArrayadapter extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view_arrayadapter);
List<String>ls = new ArrayList<String>();
ls.add("測試數(shù)據(jù)1");
ls.add("測試數(shù)據(jù)2");
ls.add("測試數(shù)據(jù)1");
ls.add("測試數(shù)據(jù)2");
ls.add("測試數(shù)據(jù)1");
ls.add("測試數(shù)據(jù)2");
ls.add("測試數(shù)據(jù)1");
ls.add("測試數(shù)據(jù)2");
ls.add("測試數(shù)據(jù)1");
ls.add("測試數(shù)據(jù)2");
ls.add("測試數(shù)據(jù)1");
ls.add("測試數(shù)據(jù)2");
ArrayAdapter adapter =new ArrayAdapter(this,android.R.layout.simple_list_item_1,ls);
setListAdapter(adapter); //繼承ListActivity才有此函數(shù)
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
String s =((TextView)v).getText().toString();
Toast.makeText(this,"提示"+position+s,Toast.LENGTH_LONG).show();
super.onListItemClick(l, v, position, id);
}
}
//效果圖
簡單的listview,關(guān)鍵是ArrayAdapter中布局和數(shù)據(jù)的適配。android.R.layout.simple_list_item_1系統(tǒng)自帶的樣式,可以根據(jù)自己的要求更改布局樣式。List<String>ls = new ArrayList<String>() 用于顯示的數(shù)據(jù)。
//SimpleAdapter 適配Listview
//layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/img2"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/title2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
//關(guān)鍵代碼設(shè)置listview
SimpleAdapter adapter1 =new SimpleAdapter(this,lm,R.layout.simple2, new String[]{"title","img","price"},new int[]{R.id.title2,R.id.img2,R.id.price});
setListAdapter(adapter1);
//得到數(shù)據(jù)
public List<Map<String, Object>> getData2()
{
Map<String,Object>value =new HashMap<String,Object>();
List<Map<String,Object>>lm =new ArrayList<Map<String,Object>>();
value.put("title","蘋果");
value.put("img",R.drawable.aphone);
value.put("price","價(jià)格:5800");
lm.add(value);
value =new HashMap<String,Object>();
value.put("title", "三星");
value.put("img",R.drawable.sphone);
value.put("price","價(jià)格:4800");
lm.add(value);
value =new HashMap<String,Object>();
value.put("title","華為");
value.put("img",R.drawable.hphone);
value.put("price","價(jià)格:6000");
lm.add(value);
return lm;
}
//SimpleCursorAdapter適配Listview 用于數(shù)據(jù)庫數(shù)據(jù)的顯示
//關(guān)鍵代碼
//獲得一個指向系統(tǒng)通訊錄數(shù)據(jù)庫的Cursor對象獲得數(shù)據(jù)來源
Cursor cursor = getContentResolver().query(Contacts.People.CONTENT_URI,null,null,null,null);
startManagingCursor(cursor);
//實(shí)例化列表適配器
ListAdapter la = new SimpleCursorAdapter(this,android.R.layout.simple_list_item_2,cursor,new String[]{Contacts.People.NAME,Contacts.People.NUMBER},new int[]{android.R.id.text1,android.R.id.text2});
setListAdapter(la);
//一定要在AndroidManifest.xml加讀取聯(lián)系人的權(quán)限。
<uses-permission android:name="android.permission.READ_CONTACTS"/>
//代碼下載
http://down.51cto.com/data/2119105
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。