TestAdapter
//简单适配器
public class TestAdapter extends AppCompatActivity {
private ListView listView;
private SimpleAdapter adapter;
private List<Map<String,Object>> list;
private int[] image = new int[]{R.drawable.dianhua,R.drawable.dingwei,
R.drawable.fabu,R.drawable.fenxiang,R.drawable.hezuo,R.drawable.shouji,
R.drawable.sousuo};
private String[] strings = new String[]{"电话","定位","发布","分享","合作","手机","搜索"};
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_adapter);
listView = (ListView) findViewById(R.id.listView);
list = new ArrayList<>();
for(int i = 0 ; i < strings.length; i ++){
Map<String,Object> map = new HashMap<>();
map.put("img",image[i]);
map.put("str",strings[i]);
list.add(map);
}
adapter = new SimpleAdapter(TestAdapter.this,list,R.layout.adapter_item,new String[]{"img","str"},new int[]{R.id.imageView,R.id.textView});
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.i("TEST","字符:"+view+","+position+","+id);
Toast.makeText(TestAdapter.this,strings[position],Toast.LENGTH_SHORT).show();
}
});
}
}
test_adapter.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
adapter_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="58dp"
android:gravity="center_vertical">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:src="@drawable/dianhua"
/>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="item"/>
</LinearLayout>