Linking

Capturing Life & Tech

  • 主页
  • 随笔
  • 关于我
所有文章 外链

Linking

Capturing Life & Tech

  • 主页
  • 随笔
  • 关于我

Adapter初识

阅读数:次 2016-12-04
字数统计: 1.1k字   |   阅读时长≈ 5分

Adapter 类控件

Adapter基础讲解

概念:帮助填充数据的中间桥梁,将数据以合适的形式显示到view上,供用户查看。

1.ArrayAdapter:

1
2
3
4
5
6
7
8
9
10
11
12
//data to show
//String[] datas = {"神奇", "没事吗", "不会吧"};
//also can setdata like this
List<String> datas = new ArrayList<String>();
datas.add("hehe1");
datas.add("hehe2");
datas.add("hehe3");
//create ArrayAdapter
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, datas);
//listview set adapter
ListView lv = (ListView)findViewById(R.id.listview_adapter1);
lv.setAdapter(adapter);

ArrayAdapter第二参数:
qiniu截借的别人的blog

2.SimpleAdapter
常用,listview,item,List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
from是塞的数据list中的标识string[]{},to是item中的id的int[]{R.id.xx}数组,resource是item布局,context几个adapter参数;
add at 20161201

ListView简单使用

1.自定义BaseAdapter,绑定数据
展示样式在item中设计,适应adapter中做数据的set工作,只做一个适配器,而具体的数据在mainactivity中插入,一般会用到list,以及范型,list<entrity>,实体类中写属性的存取方法,最后给listview设置适配器,listview.setAdapter(mAdapter)。下面是代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
//itme_layout
<?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"
android:padding="10dp">

<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher" />

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:orientation="vertical"
android:padding="5dp">

<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="wenzi1" />

<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:text="wenzi1" />
</LinearLayout>
</LinearLayout>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#entity
package com.example.linking.adapters.Model;
/**
* Created by linking on 16/12/4.
*/
public class Animals {
private String aName;
private String aSpeak;
private int aIcon;
public Animals(String aName, String aSpeak, int aIcon) {
this.aName = aName;
this.aSpeak = aSpeak;
this.aIcon = aIcon;
}
public Animals() {
}
public String getaName() {
return aName;
}
public void setaName(String aName) {
this.aName = aName;
}
public String getaSpeak() {
return aSpeak;
}
public void setaSpeak(String aSpeak) {
this.aSpeak = aSpeak;
}
public int getaIcon() {
return aIcon;
}
public void setaIcon(int aIcon) {
this.aIcon = aIcon;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
##AnimalAdapter
package com.example.linking.adapters.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.linking.adapters.Model.Animals;
import com.example.linking.adapters.R;

import java.util.LinkedList;

/**
* Created by linking on 16/12/4.
*/

public class AnimalAdapter extends BaseAdapter {

private LinkedList<Animals> mData;
private Context mContext;

public AnimalAdapter(LinkedList<Animals> mData, Context mContext) {
this.mData = mData;
this.mContext = mContext;
}

@Override
public int getCount() {
return mData.size();
}

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

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View view, ViewGroup viewGroup) {
view = LayoutInflater.from(mContext).inflate(R.layout.item_listview_baseadapter,viewGroup,false);
ImageView img_icon = (ImageView) view.findViewById(R.id.img);
TextView tv1 = (TextView) view.findViewById(R.id.tv1);
TextView tv2 = (TextView) view.findViewById(R.id.tv2);
img_icon.setBackgroundResource(mData.get(position).getaIcon());
tv1.setText(mData.get(position).getaName());
tv2.setText(mData.get(position).getaSpeak());
return view;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#Activity
package com.example.linking.adapters.Activity;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;

import com.example.linking.adapters.Adapter.AnimalAdapter;
import com.example.linking.adapters.Model.Animals;
import com.example.linking.adapters.R;

import java.util.LinkedList;
import java.util.List;

public class ListViewBaseAdapterActivity extends AppCompatActivity {

private List<Animals> mData = null;
private Context mContext;
private AnimalAdapter mAdapter = null;
private ListView list_animal;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view_base_adapter);

mContext = ListViewBaseAdapterActivity.this;
list_animal = (ListView) findViewById(R.id.listview1);
mData = new LinkedList<Animals>();
mData.add(new Animals("狗说","你是够吗",R.mipmap.ic_launcher));
mData.add(new Animals("mao说","你是mao吗",R.mipmap.ic_launcher));
mData.add(new Animals("hooo说","你是hooo吗",R.mipmap.ic_launcher));
mAdapter = new AnimalAdapter((LinkedList < Animals > )mData,mContext);
list_animal.setAdapter(mAdapter);
}
}

2.其他设置
2.1表头表尾设置分割线
只能在Java中写代码进行设置了,可供我们调用的方法如下:

  • addHeaderView(View v):添加headView(表头),括号中的参数是一个View对象
  • addFooterView(View v):添加footerView(表尾),括号中的参数是一个View对象
  • addHeaderView(headView, null, false):和前面的区别:设置Header是否可以被选中
  • addFooterView(View,view,false):同上

对了,使用这个addHeaderView方法必须放在listview.setAdapter前面,否则会报错。
注意问题:
添加表头表尾后,我们发现positon是从表头开始算的,就是你添加的第一个数据本来的postion是
0,但是此时却变成了1,因为表头也算!!

2.2列表从底部开始显示:stackFromBottom=”true”
2.3设置点击颜色cacheColorHint
2.4隐藏滑动条

1
2
3
android:scrollbars=”none” 
or
setVerticalScrollBarEnabled(true);
  • 本文作者: Linking
  • 本文链接: https://linking.fun/2016/12/04/Adapter初识/
  • 版权声明: 版权所有,转载请注明出处!

扫一扫,分享到微信

《利用Python进行数据分析》读书笔记_1_附录
安卓onClick事件监听的实现方式
  1. 1. Adapter 类控件
    1. 1.1. Adapter基础讲解
    2. 1.2. ListView简单使用
© 2015-2026 Linking
GitHub:hexo-theme-yilia-plus by Litten
本站总访问量次 | 本站访客数人
  • 所有文章
  • 外链

tag:

  • weather
  • 需求
  • essay
  • basketball
  • olympic
  • nginx
  • APPScan
  • SQl盲注
  • xss
  • Ajax
  • ajax
  • ai
  • agent
  • openclaw
  • ccf
  • Nginx
  • HTML5
  • html5
  • hmtl5
  • sse
  • JavaScriptCore
  • Oracle
  • operation
  • Linux
  • deploy
  • Mac Office
  • markdown
  • ListView
  • GridView
  • MySQL
  • 慢查询
  • mongodb
  • 转置
  • thought
  • network
  • ubuntu
  • NetworkManager
  • RFKill
  • Netplan
  • avatar
  • cocoa
  • blog
  • Gitalk
  • container
  • macvlan
  • docker
  • oracle
  • cookie
  • patch
  • gitea
  • git
  • iOS
  • https
  • 多线程
  • bundle
  • 兼容性
  • HTTP
  • 绘图
  • cs
  • java
  • 效率
  • 快捷键
  • route
  • nodejs
  • pip
  • arcgis
  • arcgis 建模
  • 标识
  • redis
  • read
  • bookList
  • running
  • showdoc
  • disk
  • unit-test
  • D.Wade
  • thoughts
  • duoduo
  • Python
  • python
  • tomcat
  • 读书节
  • session
  • jdk
  • war
  • 加班
  • Android onclick事件监听
  • 正则
  • 手机品牌匹配
  • ntp
  • OpenLayers
  • Geoserver
  • wechat
  • 微信公众号
  • 爬虫
  • WeChat
  • 张靓颖
  • 动漫
  • vpn
  • PPT
  • MarkDown
  • plan
  • 朱赟
  • 极客时间专栏
  • 极客邦
  • 模块化
  • MVC
  • excel
  • NBA
  • kobe
  • team
  • crawler
  • 进度条
  • ssl
  • book
  • anti-stealing-link
  • Agentic Engineering
  • Vibe Coding
  • Software 3.0
  • Andrej Karpathy
  • LLM
  • Programming
  • complain

    缺失模块。
    1、请确保node版本大于6.2
    2、在博客根目录(注意不是yilia-plus根目录)执行以下命令:
    npm i hexo-generator-json-content --save

    3、在根目录_config.yml里添加配置:

      jsonContent:
        meta: false
        pages: false
        posts:
          title: true
          date: true
          path: true
          text: false
          raw: false
          content: false
          slug: false
          updated: false
          comments: false
          link: false
          permalink: false
          excerpt: false
          categories: false
          tags: true
    

  • GitHub Trending
  • OpenAI ChatGPT
  • Gitee码云
  • 简书
  • CSDN