您好,登錄后才能下訂單哦!
//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("測試數據1");
ls.add("測試數據2");
ls.add("測試數據1");
ls.add("測試數據2");
ls.add("測試數據1");
ls.add("測試數據2");
ls.add("測試數據1");
ls.add("測試數據2");
ls.add("測試數據1");
ls.add("測試數據2");
ls.add("測試數據1");
ls.add("測試數據2");
ArrayAdapter adapter =new ArrayAdapter(this,android.R.layout.simple_list_item_1,ls);
setListAdapter(adapter); //繼承ListActivity才有此函數
}
@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,關鍵是ArrayAdapter中布局和數據的適配。android.R.layout.simple_list_item_1系統自帶的樣式,可以根據自己的要求更改布局樣式。List<String>ls = new ArrayList<String>() 用于顯示的數據。
//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>
//關鍵代碼設置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);
//得到數據
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","價格:5800");
lm.add(value);
value =new HashMap<String,Object>();
value.put("title", "三星");
value.put("img",R.drawable.sphone);
value.put("price","價格:4800");
lm.add(value);
value =new HashMap<String,Object>();
value.put("title","華為");
value.put("img",R.drawable.hphone);
value.put("price","價格:6000");
lm.add(value);
return lm;
}
//SimpleCursorAdapter適配Listview 用于數據庫數據的顯示
//關鍵代碼
//獲得一個指向系統通訊錄數據庫的Cursor對象獲得數據來源
Cursor cursor = getContentResolver().query(Contacts.People.CONTENT_URI,null,null,null,null);
startManagingCursor(cursor);
//實例化列表適配器
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加讀取聯系人的權限。
<uses-permission android:name="android.permission.READ_CONTACTS"/>
//代碼下載
http://down.51cto.com/data/2119105
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。