常见适配器的用法(在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);
}
}
按照这样的步骤,一个简单的适配器就做好了!
(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);
}
}
按照这样的步骤,一个简单的适配器就做好了!
相关文章推荐
- 全屏下ListView使Edittext长按复制失效,内容消失
- Android 重写EditText,失去焦点超出控件宽度部分三个点表示,获得焦点显示原来内容
- 根据EditText输入框的内容动态更改listView的数据
- ListView设置不同item(包涵radiobutton和EditText),并解决滑动listview后radiobutton和EditText内容被清空的情况
- 求教关于listview内edittext点击修改内容的问题
- ListView显示自定义新闻内容简单Adapter用法
- ListView的listitem为EditText无法输入内容问题
- 代码技巧2.listview的item只是简单的textview的时候适配器 怎么写?
- android listview 每一项都是edittext 导致的坑爹问题 内容的保存和焦点,光标位置的设置
- listview+EditText 实现过滤搜索listview中的内容
- 在edittext中输入内容,动态匹配更新 listview的数据(EditText中边输入,listView边更新)
- ListView中包含EditText,修改并保存EditText中内容的处理办法
- EditText + Listview 字符串匹配适配器
- edittext + listview 实现搜索listview中的内容
- ExpandableListView中包含EditText 时不能输入内容
- android edittext + listview 实现搜索listview中的内容
- Android 根据Edittext内容动态改变Listview显示的数据
- 学习笔记-Android之ListView随EditText输入内容动态改变
- edittext + listview 实现搜索listview中的内容方法(推荐)
- 获取listview中每个条目editText输入的内容