安卓开发之ListAdapter(一)

      Adapter常用来管理数据,是连接后端数据和前端显示的适配器接口,是数据和UI(view)之间一个重要的纽带。再常见的view(listview、gridview)等地方都需要用到adapter,下面我们来讲讲SimpleAdapter:

•SimpleAdapter类:一个使静态数据和在xml中定义的Views对应起来的简单adapter。
•构造方法:

      SimpleAdapter adapter = new SimpleAdapter(context, List<?extends Map<String,?>>  data, resource, from, to);

      context:上下文,指这个适配器所运行的视图,一般是指在那个activity(界面)

      data:一个泛型的List,适配器的数据源(一般使用ArrayList封装Map、HashMap对象)

       resource:适配器xml视图的引用路径

      from:参数是一个数组对象,主要是将Map的键映射到列名,一一对应

      to:将“from”的值一一对应显示,是一个R文件的数组,一般是你所要加载数据的控件的ID数组.

   list.setAdapter(adapter);

代码示例:

java代码:

package com.sumzom.arrayadp;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.example.com.sumzom.lv.R;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class ArrayAdpActivity extends Activity{

 private ListView ary_list = null;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.arrayadp);
  List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
  HashMap<String, String> map = new HashMap<String, String>();
  map.put("name","xoap");
  list.add(map);
  HashMap<String, String> map1 = new HashMap<String, String>();
  map1.put("name","fnsjf");
  list.add(map1);
  SimpleAdapter adapter = new SimpleAdapter(
    getApplicationContext(), list, R.layout.lv_item,
    new String[]{"name"}, new int[]{R.id.tv} );
  ary_list = (ListView) findViewById(R.id.ary_list);
  ary_list.setAdapter(adapter);
  }

}

xml代码:

activity绑定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="vertical" >
   
    <ListView
        android:id="@+id/ary_list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"></ListView>
   
</LinearLayout>

list item的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="vertical" >
   
    <LinearLayout
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
       
        <ImageView
            android:id="@+id/img"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher"
        />
    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我的第一个自定义适配器"
        android:textColor="#ff00ff"/>
       
    </LinearLayout>

</LinearLayout>

运行结果:

 

转载于:https://www.cnblogs.com/sunzan/p/4860063.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个使用Kivy开发记账本的基本步骤: 1. 创建一个Kivy应用程序 ```python import kivy kivy.require('1.11.1') from kivy.app import App from kivy.uix.gridlayout import GridLayout class MyGrid(GridLayout): pass class MyApp(App): def build(self): return MyGrid() if __name__ == '__main__': MyApp().run() ``` 2. 创建GUI界面 在Kivy中,可以使用kv文件来定义GUI界面。我们可以在kv文件中创建一个GridLayout,用于显示以下元素: - 日期选择器 - 消费类型选择器 - 消费金额输入框 - 添加按钮 - 记账记录列表 ```python <MyGrid>: cols: 1 GridLayout: cols: 2 Label: text: '日期:' DatePicker: id: date_picker Label: text: '消费类型:' Spinner: id: category_spinner values: ['餐饮', '交通', '住宿', '娱乐'] Label: text: '金额:' TextInput: id: amount_input input_type: 'number' input_filter: 'float' Button: text: '添加' on_press: app.add_record() ListView: id: record_list adapter: ListAdapter(data=[], cls=ListItemButton) <ListItemButton>: text: self.text font_size: '20sp' size_hint_y: None height: self.texture_size[1] + 20 on_press: root.parent.parent.parent.parent.remove_record(self) ``` 3. 添加记账记录 在应用程序中,我们需要一个函数来添加新的记账记录。这个函数会从GUI界面中获取日期、消费类型和金额,并将它们添加到一条新的记录中。然后,将新的记录添加到记录列表中。 ```python class MyGrid(GridLayout): def add_record(self): date = self.ids.date_picker.text category = self.ids.category_spinner.text amount = self.ids.amount_input.text if date and category and amount: record = {'date': date, 'category': category, 'amount': amount} self.ids.record_list.adapter.data.extend([record]) self.ids.record_list._trigger_reset_populate() self.ids.date_picker.text = '' self.ids.amount_input.text = '' ``` 4. 删除记账记录 我们还需要一个函数来删除已添加的记账记录。当用户点击列表中的删除按钮时,这个函数将从记录列表中删除该记录。 ```python class MyGrid(GridLayout): def remove_record(self, item): record = item.text self.ids.record_list.adapter.data.remove(record) self.ids.record_list._trigger_reset_populate() ``` 5. 运行应用程序 最后,我们可以运行应用程序并查看记账本的效果。 ```python if __name__ == '__main__': MyApp().run() ``` 完整代码如下所示: ```python import kivy kivy.require('1.11.1') from kivy.app import App from kivy.uix.gridlayout import GridLayout from kivy.adapters.listadapter import ListAdapter from kivy.uix.listview import ListItemButton class MyGrid(GridLayout): def add_record(self): date = self.ids.date_picker.text category = self.ids.category_spinner.text amount = self.ids.amount_input.text if date and category and amount: record = {'date': date, 'category': category, 'amount': amount} self.ids.record_list.adapter.data.extend([record]) self.ids.record_list._trigger_reset_populate() self.ids.date_picker.text = '' self.ids.amount_input.text = '' def remove_record(self, item): record = item.text self.ids.record_list.adapter.data.remove(record) self.ids.record_list._trigger_reset_populate() class MyApp(App): def build(self): return MyGrid() if __name__ == '__main__': MyApp().run() ``` 注意:为了运行这个应用程序,我们还需要安装以下依赖项:kivy、kivy-garden、kivy-garden-datepicker。可以使用以下命令来安装它们: ```bash pip install kivy pip install kivy-garden garden install datepicker ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值