您的位置:首页 > 移动开发 > Android开发

Android基本控件ListView的使用总结

2015-11-05 12:57 465 查看
ListView的简单用法:

首先我们需要创建一个UsingListView项目,Eclipse会自动生成相应XML的布局文件。代码所下所示:


<RelativeLayout 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"
tools:context="com.example.uselistview.MainActivity" >

<ListView
android:id="@+id/lv"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />

</RelativeLayout>


然后我们修改MainActivity.java中的代码:

public class MainActivity extends Activity {
private ListView lv;
String[] data=new String[]{"小明","婧婧","瑞瑞"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter<String>adapter=new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1,data);
lv=(ListView) findViewById(R.id.lv);
lv.setAdapter(adapter);
}


以上是相对简单的使用ArrayAdapter为ListView传递相对简单的文本数据。ListView在使用适配器经常使用ArrayAdapter、BaseAdapter、SimpleAdapter。以下描述SimpleAdapter,它的扩展性最好,可以定义各种各样的布局。使用simpleAdapter一般都是用数组列表ArrayList,而ArrayList一般使用HashMap构成。

如下所示为使用simpleAdapter传递数据给ListView:

自定义List_cell.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"
android:orientation="horizontal" >

<ImageView
android:id="@+id/Image"
android:layout_width="60dp"
android:layout_height="60dp"
/>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >

<TextView
android:id="@+id/tv1"
android:layout_width="fill_parent"
android:layout_height="40dp" />

<TextView
android:id="@+id/tv2"
android:layout_width="fill_parent"
android:layout_height="20dp" />
</LinearLayout>

</LinearLayout>


MainActivity.java:

public class MainActivity extends Activity {
private ListView lv;
//准备数据
int ImgId[]=new int[]{R.drawable.img1,R.drawable.img2,R.drawable.img3};
String name[]=new String[]{"Img1","Img2","Img3"};
String dec[]=new String[]{"Img1_dec","Img2_dec","Img3_dec"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<HashMap<String, Object>> alist=new ArrayList<HashMap<String, Object>>();
for(int i=0;i<ImgId.length;i++){
HashMap<String, Object> map=new HashMap<String, Object>();
map.put("Image", ImgId[i]);
map.put("tv_1", name[i]);
map.put("tv_2", dec[i]);
alist.add(map);
}
lv=(ListView) findViewById(R.id.lv);
SimpleAdapter adapter=new SimpleAdapter(MainActivity.this, alist, R.layout.list_cell,
new String[]{"Image","tv_1","tv_2"},
new int[]{R.id.Image,R.id.tv1,R.id.tv2}
);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View   view,
int position, long id) {
HashMap<String, Object>clkmap=( HashMap<String, Object>)parent.getItemAtPosition(position);
Toast.makeText(MainActivity.this, clkmap.get("tv_1").toString(), Toast.LENGTH_SHORT).show();
}
}


以下我们使用BaseAdapter来实现以上效果,BaseAdapter与其他Adapter有些不一样,其他的Adapter可以直接在其构造函数中配置数据,但是BaseAdapter中需要重写4个基本方法。

布局文件和以上相同。

自定义ListAdapte继承至BaseAdapter,如下所示:

public class ListAdapter extends BaseAdapter {
private Context context=null;
public ListAdapter(Context context){
this.context=context;
}
public Context getContext() {
return context;
}

private ListcellData data[]=new ListcellData[]{
new ListcellData("婧婧", "是个美丽的好女孩", R.drawable.img1),
new ListcellData("瑞瑞", "是个阳光好男孩", R.drawable.img2),
new ListcellData("楼上", "说的很有道理", R.drawable.img3)
};
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout ll=null;
if(convertView!=null){
ll=(LinearLayout)convertView;
}else{
ll=(LinearLayout) LayoutInflater.from(getContext()).inflate(R.layout.list_cell, null);
}
ListcellData data=getItem(position);
//重新赋值子对象
ImageView icon=(ImageView) ll.findViewById(R.id.Image);
TextView tv_1=(TextView)ll.findViewById(R.id.tv1);
TextView tv_2=(TextView) ll.findViewById(R.id.tv2);
icon.setImageResource(data.ImgId);
tv_1.setText(data.name);
tv_2.setText(data.dec);
return ll;
}
@Override
public long getItemId(int position) {

return position;
}
@Override
public ListcellData getItem(int position) {

return data[position];
}

@Override
public int getCount() {

return data.length;
}

}


创建一个类ListcellData.java:

public class ListcellData {
public String name;
public String dec;
public int ImgId;
public ListcellData(String name,String dec,int ImgId){
this.name=name;
this.dec=dec;
this.ImgId=ImgId;
}
}


主逻辑实现:

public class MainActivity extends Activity {
private ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv=(ListView) findViewById(R.id.lv);
lv.setAdapter(new ListAdapter(this));
}


……………………………………………………………………………………………………………………………………………………………………………………………..可以利用android系统提供的ListActivity简单的实现以上效果:具体如下:

首先我们创建一个ListViewActivity继承自ListActivity,并重写onCreate()方法:

public class ListViewActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.listview_activity);
setListAdapter(new ListAdapter(this));
}
}


此时我们需要修改AndroidManifest.xml中的:

<activity
android:name="com.example.uselistview.ListViewActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>


这样我就能实现了以上功能,也可以自定义相应的布局。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: