package com.example.administrator.android_listview;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends Activity implements AdapterView.OnItemClickListener,AbsListView.OnScrollListener{
private ListView listView;
private List<Map<String, Object>>dataList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listView=(ListView)findViewById(R.id.listView);
dataList = new ArrayList<Map<String, Object>>();
SimpleAdapter simAdapter=new SimpleAdapter(this,getData(),R.layout.item,
new String[]{"pic","text"},
new int[]{R.id.pic, R.id.text});
listView.setAdapter(simAdapter);
listView.setOnItemClickListener(this);
// listView.setOnScrollListener(this);
}
private List<Map<String, Object>> getData(){
for(int i=0; i<20; i++) {
Map<String, Object>map=new HashMap<String, Object>();
map.put("pic", R.mipmap.ic_launcher);
map.put("text", String.valueOf(i));
dataList.add(map);
}
return dataList;
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
int totalItemCount){
}
@Override
public void onItemClick(AdapterView<?>parent, View view, int position, long id) {
String text=listView.getItemAtPosition(position).toString()+"";
Toast.makeText(this, "position="+position+" text="+text, Toast.LENGTH_LONG).show();
}
}