Android中ListView設(shè)置靜態(tài)數(shù)據(jù)的方法
有的時(shí)候我們需要為一個(gè)listview設(shè)置固定的數(shù)據(jù),下邊就是如何設(shè)置靜態(tài)的數(shù)據(jù),之前先給大家看一看效果圖:

布局文件listview 的主頁(yè)面
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
</LinearLayout>
然后的一個(gè)布局文件為每一個(gè)listview的item,listview_item.xml有圖片和文字
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/listitem_iv"
android:layout_width="74dp"
android:layout_height="74dp"
android:src="@drawable/about_brand" />
<TextView
android:id="@+id/listitem_tv"
android:layout_width="match_parent"
android:layout_height="74dp"
android:text="TextView"
android:textAlignment="center"
android:textSize="55dp" />
</LinearLayout>
然后關(guān)鍵的是如何設(shè)置靜態(tài)數(shù)據(jù):
這界面的控制類ListViewUseAdapter.java
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
public class ListViewUseAdapter extends Activity {
private ListView listview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview_test);
listview = (ListView) this.findViewById(R.id.listview);
// 設(shè)置適配器的圖片資源
int[] imageId = new int[] { R.drawable.chat_tool_camera,
R.drawable.chat_tool_location, R.drawable.chat_tool_paint,
R.drawable.chat_tool_video, R.drawable.chat_tool_voice,
R.drawable.about_brand };
// 設(shè)置標(biāo)題
String[] title = new String[] { "相機(jī)", "定位", "畫筆", "視頻", "聲音", "聊天" };
List<Map<String, Object>> listitem = new ArrayList<Map<String, Object>>();
// 將上述資源轉(zhuǎn)化為list集合
for (int i = 0; i < title.length; i++) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("image", imageId[i]);
map.put("title", title[i]);
listitem.add(map);
}
ListViewAdapter adapter = new ListViewAdapter(this, listitem);
listview.setAdapter(adapter);
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(ListViewUseAdapter.this, "haha", Toast.LENGTH_SHORT).show();
}
});
}
}
然后需要的適配器如下:
import java.util.List;
import java.util.Map;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class ListViewAdapter extends BaseAdapter {
private Context context;
private List<Map<String, Object>> listitem;
public ListViewAdapter(Context context, List<Map<String, Object>> listitem) {
this.context = context;
this.listitem = listitem;
}
@Override
public int getCount() {
return listitem.size();
}
@Override
public Object getItem(int position) {
return listitem.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.listview_item, null);
}
ImageView imageView = (ImageView) convertView.findViewById(R.id.listitem_iv);
TextView textView = (TextView) convertView.findViewById(R.id.listitem_tv);
Map<String, Object> map = listitem.get(position);
imageView.setImageResource((Integer) map.get("image"));
textView.setText(map.get("title") + "");
return convertView;
}
}
希望本文所述對(duì)大家學(xué)習(xí)Android軟件編程有所幫助。
- Android編程使用ListView實(shí)現(xiàn)數(shù)據(jù)列表顯示的方法
- Android編程使用緩存優(yōu)化ListView的方法
- Android中ListView如何分頁(yè)加載數(shù)據(jù)
- Android實(shí)現(xiàn)ListView分頁(yè)自動(dòng)加載數(shù)據(jù)的方法
- Android checkbox的listView具體操作方法
- Android ListView優(yōu)化之提高android應(yīng)用效率
- Android ListView詳解
- Android編程記錄ListView標(biāo)記行狀態(tài)的方法
- Android編程開發(fā)中ListView的常見用法分析
- Android中ListView Item布局優(yōu)化技巧
- Android開發(fā)之ListView實(shí)現(xiàn)Item局部刷新
- android開發(fā)之listView組件用法實(shí)例簡(jiǎn)析
相關(guān)文章
Android 仿小米鎖屏實(shí)現(xiàn)九宮格解鎖功能(無需圖片資源)
最近公司要求做個(gè)九宮格解鎖,本人用的是小米手機(jī),看著他那個(gè)設(shè)置鎖屏九宮格很好看,就做了該組件,不使用圖片資源,純代碼實(shí)現(xiàn),感興趣的朋友參考下吧2016-12-12
Android Studio開發(fā)之 JNI 篇的簡(jiǎn)單示例
本篇文章主要介紹了Android Studio開發(fā)之 JNI 篇的簡(jiǎn)單示例,它提供了若干的API實(shí)現(xiàn)了Java和其他語(yǔ)言的通信,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
Android View源碼解讀 DecorView與ViewRootImpl淺談
這篇文章主要解讀了Android View源碼,為大家詳細(xì)介紹DecorView與ViewRootImpl,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02
Android動(dòng)態(tài)添加碎片代碼實(shí)例
這篇文章主要介紹了Android動(dòng)態(tài)添加碎片代碼實(shí)例,2019-06-06
解決Android Studio一直停留在MyApplication:syncing的問題
這篇文章主要介紹了Android Studio一直停留在MyApplication:syncing的完美解決方案,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10
Android動(dòng)畫之逐幀動(dòng)畫(Frame Animation)基礎(chǔ)學(xué)習(xí)
大家都知道逐幀動(dòng)畫是一種常見的動(dòng)畫形式,其原理是在“連續(xù)的關(guān)鍵幀”中分解動(dòng)畫動(dòng)作,也就是在時(shí)間軸的每幀上逐幀繪制不同的內(nèi)容,使其連續(xù)播放而成動(dòng)畫。下面我們就來學(xué)習(xí)下Android中逐幀動(dòng)畫的基礎(chǔ)知識(shí),有需要的可以參考借鑒。2016-09-09
詳解Android中Handler的內(nèi)部實(shí)現(xiàn)原理
這篇文章主要介紹了Android中Handler的內(nèi)部實(shí)現(xiàn)原理,對(duì)Handler和消息循環(huán)的實(shí)現(xiàn)原理進(jìn)行源碼分析,需要的朋友可以參考下2015-12-12
Android開發(fā)教程之shape和selector的結(jié)合使用
shape和selector是Android UI設(shè)計(jì)中經(jīng)常用到的,比如我們要自定義一個(gè)圓角Button,點(diǎn)擊Button有些效果的變化,就要用到shape和selector,接下來通過本文給大家介紹Android開發(fā)教程之shape和selector的結(jié)合使用,感興趣的朋友一起學(xué)習(xí)吧2016-01-01

