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

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);
    }
}


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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值