package com.example.com.exmple.android_listview;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.content.ClipData.Item;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
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, OnScrollListener{
private ListView listview;
private ArrayAdapter<String>arr_adapter;
private SimpleAdapter simp_adapter;
private List<Map<String, Object>>data_list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview=(ListView) findViewById(R.id.listView1);
String[]arr_data={"慕课网1","慕课网2","慕课网3","慕课网4","慕课网5","慕课网6"};
arr_adapter= new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,arr_data);
data_list=new ArrayList<Map<String,Object>>();
/**
* context:上下文
* data:数据源(List<? extends Map<String,?>> data)一个Map所组成的LIST集合
* 每一个Map都会对应ListView列表中的一行
* 每一个Map(键-值)中的键必须包含所有在from中所指定的键
* resource:列表项的布局文件ID
* from:Map中的键名
* to:绑定数据视图中的ID,与from成对应关系
*/
simp_adapter=new SimpleAdapter(this, getdata(), R.layout.item, new String[]{"pic","text"},
new int[]{R.id.image,R.id.text});
listview.setAdapter(simp_adapter);
}
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.ic_launcher);
map.put("text","慕课网"+i);
data_list.add(map);
}
return data_list;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
String text=listview.getItemIdAtPosition(arg2)+"";
//这里toast不出来
Toast.makeText(MainActivity.this, "sdfasdfasdfasd", Toast.LENGTH_SHORT).show();
Log.i("toast",String.valueOf(arg2));
}
@Override
public void onScroll(AbsListView arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void onScrollStateChanged(AbsListView arg0, int arg1) {
// TODO Auto-generated method stub
switch (arg1) {
case SCROLL_STATE_FLING:
Log.i("main", "离开前用力划了一下屏幕");
break;
case SCROLL_STATE_TOUCH_SCROLL:
Log.i("main", "还在滑动");
break;
case SCROLL_STATE_IDLE:
Log.i("main", "停止滑动");
break;
}
}
}