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利用RecyclerView實(shí)現(xiàn)列表倒計(jì)時(shí)效果
這篇文章主要為大家詳細(xì)介紹了Android利用RecyclerView實(shí)現(xiàn)列表倒計(jì)時(shí)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09
Android Compose 屬性動(dòng)畫使用探索詳解
這篇文章主要為大家介紹了Android Compose 屬性動(dòng)畫使用探索詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
Android系統(tǒng)關(guān)機(jī)的全流程解析
這篇文章主要介紹了Android系統(tǒng)關(guān)機(jī)的全流程解析,從上層空間一直深入到內(nèi)核全面講解,非常推薦!需要的朋友可以參考下2016-02-02
Android編程單選項(xiàng)框RadioGroup綜合應(yīng)用示例
這篇文章主要介紹了Android編程單選項(xiàng)框RadioGroup用法,結(jié)合實(shí)例形式分析了Android單選按鈕組RadioGroup的定義與具體使用技巧,需要的朋友可以參考下2016-10-10
Android利用Espresso進(jìn)行UI自動(dòng)化測(cè)試的方法詳解
因?yàn)槲沂歉鉧ndroid開發(fā)的,所以被分到了自動(dòng)化測(cè)試小組,所以了解了一些UI自動(dòng)化測(cè)試。下面這篇文章主要給大家介紹了關(guān)于Android利用Espresso進(jìn)行UI自動(dòng)化測(cè)試的相關(guān)資料,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-12-12
常見的8個(gè)Android內(nèi)存泄漏問題及解決方法
在Android開發(fā)中,內(nèi)存泄漏是一個(gè)常見的問題,這個(gè)問題可能會(huì)導(dǎo)致應(yīng)用程序變慢、崩潰或者消耗大量的內(nèi)存,最終導(dǎo)致設(shè)備性能下降,本文就給大家總結(jié)一下最常見的8個(gè)Android內(nèi)存泄漏問題及解決方法,需要的朋友可以參考下2023-07-07
Android實(shí)現(xiàn)文字滾動(dòng)播放效果的代碼
這篇文章主要介紹了Android實(shí)現(xiàn)文字滾動(dòng)播放效果,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07
ViewFlipper實(shí)現(xiàn)上下翻滾輪播效果
這篇文章主要為大家詳細(xì)介紹了ViewFlipper實(shí)現(xiàn)上下翻滾輪播效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-08-08

