Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android中ListView設置靜態數據的方法

Android中ListView設置靜態數據的方法

編輯:關於Android編程

有的時候我們需要為一個listview設置固定的數據,下邊就是如何設置靜態的數據,之前先給大家看一看效果圖:

布局文件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>

然後關鍵的是如何設置靜態數據:
這界面的控制類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);

    // 設置適配器的圖片資源
    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 };

    // 設置標題
    String[] title = new String[] { "相機", "定位", "畫筆", "視頻", "聲音", "聊天" };
    List<Map<String, Object>> listitem = new ArrayList<Map<String, Object>>();

    // 將上述資源轉化為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;
  }
}

希望本文所述對大家學習Android軟件編程有所幫助。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved