您的位置:首页 > 其它

常见适配器的用法(在listview中 ,把三个edittext内容放在简单适配器中)

2013-11-19 20:07 417 查看
List作为数据源,填充SimpleAdapter
(1)在listview中 ,把三个edittext内容放在简单适配器中

先定义布局文件avtivity_main.xml

<?xml version ="1.0" encoding ="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>

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

</LinearLayout>

 

(2)在定义布局文件item.xml,设置edittext 的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:layout_width="120dip"
android:layout_height="match_parent"
android:id="@+id/name"

/>
<TextView
android:layout_width="150dip"
android:layout_height="match_parent"
android:id="@+id/phone"

/>
<TextView
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:id="@+id/amount"

/>

</LinearLayout>

(3)下面的方式就是把item.xml中的三个edittext,放在listview中,需要借助于SimpleAdapter,和list,

1)首相定义listview,设置监听;

2)接下来定义list,用put方法往list中写入数据;

3)最后定义适配器,需要找到edittext对应的id,布局里对应的内容放在list中再把它放在适配器里,

下面是在MainActivity.java代码实现过程

import java.util.ArrayList;
import java.util.HashMap;

import android.R.string;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class MainActivity extends Activity
{
private ListView listview;
protected voidonCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

listview=(ListView)findViewById(R.id.listView);
ArrayList<HashMap<String,String>> aList= new ArrayList<HashMap<String,String>>();

for (int i = 0; i < 30; i++)
{
HashMap<String,String> map = newHashMap<String, String>();
map.put("name","1");
map.put("phone","2");
map.put("amount","3");
aList.add(map);
}

SimpleAdapter saAdapter= newSimpleAdapter(this,
aList, R.layout.item, new String []{
"name","phone","amount"},
new int []{R.id.name,R.id.phone,R.id.amount}
);
listview.setAdapter(saAdapter);
}
}

按照这样的步骤,一个简单的适配器就做好了!

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: