ListView列表的简单案例
在android开发中ListView它以列表的形式展示具体内容,并且能够根据数据的长度自适应显示。抽空把对ListView的使用做了整理,并写了个小例子
列表示例图:
BaseActivity
package com.shaoxin.mylistactivity; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; /** * Created by shaoxin on 2016/11/21. */ public abstract class BaseActivity extends AppCompatActivity { public abstract void init(); public abstract void setListener(); @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); init(); setListener(); } }
MainActivity
package com.shaoxin.mylistactivity; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.BaseAdapter; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.TextView; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class MainActivity extends BaseActivity { private ListView list1; private ArrayAdapter arrayAdapter; private String[] strName = {"南昌", "上海", "北京"}; private ListView list2; private SimpleAdapter simpleAdapter; private String[] strWeither = {"晴天", "下雨", "刮风"}; private List<Map<String, String>> list; private MyAdapter myAdapter; private ListView list3; @Override public void init() { setContentView(R.layout.activity_main); list1 = (ListView) findViewById(R.id.list_1); list2 = (ListView) findViewById(R.id.list_2); list3 = (ListView) findViewById(R.id.list_3); } @Override public void setListener() { arrayAdapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1, strName); list1.setAdapter(arrayAdapter); list = new ArrayList<>(); for (int i = 0; i < strName.length; i++) { Map map = new HashMap(); map.put("txt1", strName[i]); map.put("txt2", strWeither[i]); list.add(map); } simpleAdapter = new SimpleAdapter(MainActivity.this, list, android.R.layout.simple_list_item_2, new String[]{"txt1", "txt2"}, new int[]{android.R.id.text1, android.R.id.text2}); list2.setAdapter(simpleAdapter); myAdapter = new MyAdapter(); list3.setAdapter(myAdapter); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } public class MyAdapter extends BaseAdapter { @Override public int getCount() { return strName.length; } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { convertView = View.inflate(MainActivity.this, android.R.layout.simple_list_item_2, null); TextView text1 = (TextView) convertView.findViewById(android.R.id.text1); TextView text2 = (TextView) convertView.findViewById(android.R.id.text2); text1.setText(strName[position]); text2.setText(strWeither[position]); return convertView; } } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.shaoxin.mylistactivity.MainActivity"> <ListView android:id="@+id/list_3" android:layout_width="match_parent" android:layout_height="wrap_content" android:divider="#ff0000" android:dividerHeight="1dp" /> <ListView android:id="@+id/list_1" android:layout_width="match_parent" android:layout_height="wrap_content" android:divider="#ff0000" android:dividerHeight="1dp" /> <ListView android:id="@+id/list_2" android:layout_width="match_parent" android:layout_height="wrap_content" android:divider="#ff0000" android:dividerHeight="1dp" /> </LinearLayout>