Android中ListView設(shè)置靜態(tài)數(shù)據(jù)的方法
有的時候我們需要為一個listview設(shè)置固定的數(shù)據(jù),下邊就是如何設(shè)置靜態(tài)的數(shù)據(jù),之前先給大家看一看效果圖:
布局文件listview 的主頁面
<?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>
然后的一個布局文件為每一個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; } }
希望本文所述對大家學(xué)習(xí)Android軟件編程有所幫助。
- Android編程使用ListView實現(xiàn)數(shù)據(jù)列表顯示的方法
- Android編程使用緩存優(yōu)化ListView的方法
- Android中ListView如何分頁加載數(shù)據(jù)
- Android實現(xiàn)ListView分頁自動加載數(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實現(xiàn)Item局部刷新
- android開發(fā)之listView組件用法實例簡析
相關(guān)文章
Android利用RecyclerView實現(xiàn)列表倒計時效果
這篇文章主要為大家詳細(xì)介紹了Android利用RecyclerView實現(xiàn)列表倒計時效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-09-09Android系統(tǒng)關(guān)機(jī)的全流程解析
這篇文章主要介紹了Android系統(tǒng)關(guān)機(jī)的全流程解析,從上層空間一直深入到內(nèi)核全面講解,非常推薦!需要的朋友可以參考下2016-02-02Android編程單選項框RadioGroup綜合應(yīng)用示例
這篇文章主要介紹了Android編程單選項框RadioGroup用法,結(jié)合實例形式分析了Android單選按鈕組RadioGroup的定義與具體使用技巧,需要的朋友可以參考下2016-10-10Android利用Espresso進(jìn)行UI自動化測試的方法詳解
因為我是搞android開發(fā)的,所以被分到了自動化測試小組,所以了解了一些UI自動化測試。下面這篇文章主要給大家介紹了關(guān)于Android利用Espresso進(jìn)行UI自動化測試的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧。2017-12-12