日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java适配器各三种_适配器三种

發布時間:2025/1/21 编程问答 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java适配器各三种_适配器三种 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

適配器有:

作用:用于將數據綁定到組件上

過程:寫的內容----->適配器

控件------------------->通過適配器----->listView的布局

1,ArrayAdapter列表適配器

2,SimpleCursorAdapter簡單標游適配器

3,BaseAdapter基礎適配器

(2,3都可以用復雜的Lietview)

1,ArrayAdapter列表適配器(只能寫TextView體系的控件)

java代碼寫:

package com.example.layou_text;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import android.R.string;

import android.app.Activity;

import android.os.Bundle;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.view.ViewGroup;

import android.widget.ArrayAdapter;

import android.widget.BaseAdapter;

import android.widget.ImageView;

import android.widget.ListView;

import android.widget.SimpleAdapter;

import android.widget.TextView;

public class CopyOfdi1Activity extends Activity {

ListView lv_man;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.lv_man);//加載(是布局不是id!)

lv_man=(ListView)findViewById(R.id.lv_man);//獲取操作

// 注意寫法不能寫R.layout.獲取布局

//準備數據

String [] data={"a","b","c","d","e","f","g","h"};

//構造arrayaadapter適配(上下文,加載另一個布局id,傳入數據(數據會被傳入布局中))

ArrayAdapter adapter = new ArrayAdapter(CopyOfdi1Activity.this,

R.layout.item_array_adapter,data);

// 加載textview體系的單個布局

//給lv_man設置適配器

lv_man.setAdapter(adapter);

}}

xml:(定義一個listview布局)id+lv_man.xml

android:id="@+id/lv_man"

android:layout_width="match_parent"

android:layout_height="match_parent" >

再寫一個xml(定義listView里面的內容textview)

android:id="@+id/textview"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:textSize="20sp"

android:gravity="start"

>

ArrayAdapter效果圖

image.png

2,SimpleAdapter簡單的適配器

java寫:

package com.example.layou_text;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import android.R.string;

import android.app.Activity;

import android.os.Bundle;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.view.ViewGroup;

import android.widget.ArrayAdapter;

import android.widget.BaseAdapter;

import android.widget.ImageView;

import android.widget.ListView;

import android.widget.SimpleAdapter;

import android.widget.TextView;

public class CopyOfdi2Activity extends Activity {

ListView lv_man;

List data;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.lv_man);//加載(是布局不是id!)

lv_man=(ListView) findViewById(R.id.lv_man);//獲取操作

ArrayList> arrayList = new ArrayList>();

//new了一個Arraylist數組包含map集合

HashMap hashMap = new HashMap();

//new了一個map集合編寫key value

hashMap=new HashMap();

//簡寫用上面hashMap的對象再新建一個map集合,賦值,添加到arrayList集合中

hashMap.put("icom", R.drawable.f2);

hashMap.put("name","美食--2");

hashMap.put("content","內容");

arrayList.add(hashMap);

hashMap=new HashMap();

hashMap.put("icom", R.drawable.f3);

hashMap.put("name","美食--3");

hashMap.put("content","內容");

arrayList.add(hashMap);

hashMap=new HashMap();

hashMap.put("icom", R.drawable.f4);

hashMap.put("name","美食--4");

hashMap.put("content","內容");

arrayList.add(hashMap);

hashMap=new HashMap();

hashMap.put("icom", R.drawable.f5);

hashMap.put("name","美食--5");

hashMap.put("content","內容");

arrayList.add(hashMap);

hashMap=new HashMap();

hashMap.put("icom", R.drawable.f6);

hashMap.put("name","美食--6");

hashMap.put("content","內容");

arrayList.add(hashMap);

hashMap=new HashMap();

hashMap.put("icom", R.drawable.f7);

hashMap.put("name","美食--7");

hashMap.put("content","內容");

arrayList.add(hashMap);

hashMap=new HashMap();

hashMap.put("icom", R.drawable.f8);

hashMap.put("name","美食--8");

hashMap.put("content","內容");

arrayList.add(hashMap);

hashMap=new HashMap();

hashMap.put("icom", R.drawable.f9);

hashMap.put("name","美食--9");

hashMap.put("content","內容");

arrayList.add(hashMap);

hashMap=new HashMap();

hashMap.put("icom", R.drawable.f10);

hashMap.put("name","美食--10");

hashMap.put("content","內容");

arrayList.add(hashMap);

//arrayList集合里面有許多個map集合,map集合中有許多的數據

// Map對象中的key的數組,用于得到對應的value

String from[]={"icom","name","content"};//定義from代表map里面的key值

// item布局文件中的子View的id的數組

int [] to ={R.id.imageView1,R.id.tv_item_name,R.id.tv_item_content,};

//定義to代表布局控件

// 準備SimpleA

SimpleAdapter simpleAdapter = new SimpleAdapter(this, arrayList,

R.layout.activity_chatn, from, to);

//加載包含to的控件視圖(不是視圖id),from是通過key獲取了value值,

// to是把value賦給了布局中的控件

lv_man.setAdapter(simpleAdapter);

// 設置適配器

}

}

lv_man.xml布局寫

android:id="@+id/lv_man"

android:layout_width="match_parent"

android:layout_height="match_parent" >

再寫一個xml(定義listView里面的內容控件)

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="horizontal" >

android:id="@+id/imageView1"

android:layout_width="80dp"

android:layout_height="80dp"

android:gravity="center_vertical"

android:src="@drawable/f1" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:orientation="vertical"

android:gravity="center_vertical"

android:layout_marginLeft="10dp"

>

android:id="@+id/tv_item_name"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

/>

android:id="@+id/tv_item_content"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_margin="10dp"

/>

效果圖

image.png

3,BaseAdapter基礎適配器(需要繼承是抽象類)

java寫

package com.example.layou_text;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import android.R.string;

import android.app.Activity;

import android.os.Bundle;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.view.ViewGroup;

import android.widget.ArrayAdapter;

import android.widget.BaseAdapter;

import android.widget.ImageView;

import android.widget.ListView;

import android.widget.SimpleAdapter;

import android.widget.TextView;

public class MainActivity extends Activity {

ListView lv_man;//列表視圖

List data;//不用map用了shoinfo類來儲存數據集合

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.lv_man);//加載(是布局不是id!)

lv_man=(ListView) findViewById(R.id.lv_man);//獲取操作

// 準備集合數據

data =new ArrayList();

data.add(new shoinfo(R.drawable.f1,"美食名--1","美食內容--1"));

data.add(new shoinfo(R.drawable.f2,"美食名--2","美食內容--2"));

data.add(new shoinfo(R.drawable.f3,"美食名--3","美食內容--3"));

data.add(new shoinfo(R.drawable.f4,"美食名--4","美食內容--4"));

data.add(new shoinfo(R.drawable.f5,"美食名--5","美食內容--5"));

data.add(new shoinfo(R.drawable.f6,"美食名--6","美食內容--6"));

data.add(new shoinfo(R.drawable.f7,"美食名--7","美食內容--7"));

data.add(new shoinfo(R.drawable.f8,"美食名--8","美食內容--8"));

data.add(new shoinfo(R.drawable.f9,"美食名--9","美食內容--9"));

data.add(new shoinfo(R.drawable.f10,"美食名--10","美食內容--10"));

// 準備Baseadapter對象

Myadapter myadapter = new Myadapter();

//設置Adapter顯示列表

lv_man.setAdapter(myadapter);

}

class Myadapter extends BaseAdapter{//定義一個局部內部類繼承BaseAdapter

//返回集合的總數

@Override

public int getCount() {

return data.size();

}

//返回指定下標對應的數據對象

@Override

public Object getItem(int position) {

return data.get(position);

}

//返回每個條目的ID

@Override

public long getItemId(int position) {

// TODO Auto-generated method stub

return position;

}

//返回指定下標所對應的Item的View對象(試圖和總數)

// position下標

// convertView可以復用的緩存Item視圖對象, 前n+1個為null

// parent listView對象

@Override

public View getView(int position, View convertView, ViewGroup parent) {

// 1.創建或得到對應當前行的一個Viewholder對象

ViewHolder hole=null;

if(convertView==null){

// 加載布局Item的布局,得到View對象

convertView = View.inflate(MainActivity.this, R.layout.activity_chatn, null);

hole=new ViewHolder();

// 獲取到convertView的子view的控件

hole.findViewById = (ImageView) convertView.findViewById(R.id.imageView1);

hole.view = (TextView) convertView.findViewById(R.id.tv_item_name);

hole.view1 = (TextView) convertView.findViewById(R.id.tv_item_content);

// 將holder對象保存到converterView上

convertView.setTag(hole);

}else{

hole=(ViewHolder)convertView.getTag();

}

// 根據position設置對應的數據

// 得到當行的數據對象

shoinfo shoinfo = data.get(position);

// 得到子View對象

// 給ViewHolder對象的視圖設置數據

hole.findViewById.setImageResource(shoinfo.getIncon());

hole.view.setText(shoinfo.getName());

hole.view1.setText(shoinfo.getContent());

return convertView;

}

//數據類

class ViewHolder{

public ImageView findViewById;

public TextView view;

public TextView view1;

}

}

}

java寫一個存放數據類

package com.example.layou_text;

public class shoinfo {

public int incon;

public String name;

public String content;

@Override

public String toString() {

return "shoinfo [incon=" + incon + ", name=" + name + ", content="

+ content + "]";

}

public int getIncon() {

return incon;

}

public void setIncon(int incon) {

this.incon = incon;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getContent() {

return content;

}

public void setContent(String content) {

this.content = content;

}

public shoinfo(int incon, String name, String content) {

super();

this.incon = incon;

this.name = name;

this.content = content;

}

}

定義一個listview(id+lv_man)

android:id="@+id/lv_man"

android:layout_width="match_parent"

android:layout_height="match_parent" >

再寫一個xml(定義listView里面的內容控件)

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="horizontal" >

android:id="@+id/imageView1"

android:layout_width="80dp"

android:layout_height="80dp"

android:src="@drawable/f1" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:orientation="vertical"

android:gravity="center_vertical"

android:layout_marginLeft="10dp"

>

android:id="@+id/tv_item_name"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

/>

android:id="@+id/tv_item_content"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_margin="10dp"

/>

效果圖

image.png

總結

以上是生活随笔為你收集整理的java适配器各三种_适配器三种的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。