怎样使用ListView?
ArrayAdapter的使用
package com.mengmeng.android_listview;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class MainActivity extends Activity implements OnItemClickListener {
private ListView mListView;
private ArrayAdapter<String> mArray_Adapter;
private SimpleAdapter mSimpleAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = (ListView) findViewById(R.id.listView);
//载入数据源
String[] str = {"大眼萌1","大眼萌2","大眼萌3","大眼萌4","大眼萌5"};
mArray_Adapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, str);
mListView.setAdapter(mArray_Adapter);
mListView.setOnItemClickListener(this);
}
//监听
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
String text = mListView.getItemAtPosition(arg2)+"";
Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
}
}
simpleAdapter的使用
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = (ListView) findViewById(R.id.listView);
getData();
mSimpleAdapter = new SimpleAdapter
(this, list, R.layout.list_item, new String[] {"text","image"}, new int[] {R.id.text,R.id.image});
mListView.setAdapter(mSimpleAdapter);
}
private List<Map<String,Object>> getData(){
list = new ArrayList<Map<String,Object>>();
Map<String,Object> map1 = new HashMap<String, Object>();
map1.put("text", "java");
map1.put("image", R.drawable.ic_launcher);
Map<String,Object> map2 = new HashMap<String, Object>();
map2.put("text", "javaScript");
map2.put("image", R.drawable.ic_launcher);
Map<String,Object> map3 = new HashMap<String, Object>();
map3.put("text", "C++");
map3.put("image", R.drawable.ic_launcher);
Map<String,Object> map4 = new HashMap<String, Object>();
map4.put("text", "Android");
map4.put("image", R.drawable.ic_launcher);
Map<String,Object> map5 = new HashMap<String, Object>();
map5.put("text", "web");
map5.put("image", R.drawable.ic_launcher);
list.add(map1);
list.add(map2);
list.add(map3);
list.add(map4);
list.add(map5);
return list;
}
<h3>使用OnScollListener() notifyDataSetChanged()</h3>@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if(scrollState == SCROLL_STATE_FLING){//用力划一下
Map<String, Object> map = new HashMap<String, Object>();
map.put("text", "正在加入"+i++);
map.put("image", R.drawable.ic_launcher);
list.add(map);
<strong>mSimpleAdapter.notifyDataSetChanged();</strong>
Toast.makeText(this, "正在滚动...", Toast.LENGTH_SHORT).show();
}else if(scrollState == SCROLL_STATE_IDLE){//停止滚动
}else if(scrollState == SCROLL_STATE_TOUCH_SCROLL){//正在滚动
}