简介
SimpleAdapter的功能是能够为AbsListView提供复杂的数据,需要构造ListView
代码示例
package peng.liu.testview;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends Activity {
ListView myList = null;
String[] names = new String[]{
"hello","world", "hello","world"
};
String[] desc = new String[]{
"java","c#","python","swift"
};
private int[] imageIds = new int[]{R.drawable.ic_launcher,R.drawable.ic_launcher,
R.drawable.ic_launcher,R.drawable.ic_launcher};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List<Map<String,Object>> listItems = new ArrayList<Map<String, Object>>();
for (int i = 0;i<names.length;i++){
Map<String,Object> map = new HashMap<String, Object>();
map.put("header",imageIds[i]);
map.put("name",names[i]);
map.put("desc",desc[i]);
listItems.add(map);
}
SimpleAdapter adapter = new SimpleAdapter(this,listItems,R.layout.simple_item,new String[]{
"name","desc","header"
},new int[]{R.id.name,R.id.desc,R.id.header});
myList = (ListView) findViewById(R.id.myList);
myList.setAdapter(adapter);
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:id="@+id/header"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:textSize="20dp"
android:background="#f0f"
android:id="@+id/name"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:textSize="14dp"
android:id="@+id/desc"/>
</LinearLayout>
</LinearLayout>
监听事件
有两种方式:
1.OnItemClickListener()
2.OnItemSelectorListener()
这两种方式都是基于AdapterView提供的。
BaseAdapter简介
使用BaseAdapter可以获得对Adapter的控制权,创建多少个列表项以及每个列表项的组件都可以在继承的复写方法里面定义。
废话不多说,代码示例
package peng.liu.testview;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BaseAdapter adapter = new BaseAdapter() {
@Override
public int getCount() {
return 40;
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
LinearLayout line = new LinearLayout(MainActivity.this);
line.setOrientation(LinearLayout.VERTICAL);
ImageView image = new ImageView(MainActivity.this);
image.setImageResource(R.drawable.ic_launcher);
TextView text = new TextView(MainActivity.this);
text.setTextSize(20);
text.setTextColor(Color.RED);
text.setText("第"+i+"个");
line.addView(image);
line.addView(text);
return line;
}
};
ListView myList = (ListView) findViewById(R.id.myList);
myList.setAdapter(adapter);
}
}
时间: 2024-11-01 18:38:19