Android中ListView设置静态数据的方法

有的时候我们需要为一个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软件编程有所帮助。

声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:notice#nhooo.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。