package com.example.zeroking.demolistview;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
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 AppCompatActivity implements AdapterView.OnItemClickListener,AbsListView.OnScrollListener
{
private ListView listView;
private ArrayAdapter arrayAdapter;
private SimpleAdapter simpleAdapter;
private List<Map<String,Object>> dataList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView=(ListView)findViewById(R.id.LV);
String[] arr_data={"慕课1","慕课2","慕课3","慕课4","慕课5","慕课6"};
//新建一个适配器
//new ArrayAdapter(上下问,当前ListView加载每项的布局,数据源)
arrayAdapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1,arr_data);
//listView.setAdapter(arrayAdapter);
dataList=new ArrayList<Map<String,Object>>();
simpleAdapter=new SimpleAdapter(this,getData(),R.layout.simplelayout,new String[]{"pic","text"},new int[]{R.id.pic,R.id.text});
listView.setAdapter(simpleAdapter);
listView.setOnItemClickListener(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.drawable.gets);
map.put("text","慕课网"+i);
dataList.add(map);
}
/*
Map<String,Object>map1=new HashMap<String,Object>();
map1.put("pic",R.drawable.gets);
map1.put("text","慕课网"+1);
dataList.add(map1);
Map<String,Object>map2=new HashMap<String,Object>();
map2.put("pic",R.drawable.gets);
map2.put("text","哈哈哈哈");
dataList.add(map2);
*/
return dataList;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String text2=listView.getItemAtPosition(position)+"";
Toast.makeText(this,"position="+position+" text="+text2,Toast.LENGTH_SHORT).show();
Log.i("toast",String.valueOf(position));
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
}
}
蜂之谷
相关分类