simpleApater自定义列表(程序)
2021/7/11 12:06:18
本文主要是介绍simpleApater自定义列表(程序),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
1、新创建一个XML文件,存放列表
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/simpleAdaperbtnzdydy"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>
</LinearLayout>
2、新创建一个一个XML文件存放列表内容
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imgeviewzdy"
android:layout_width="60dp"
android:layout_height="60dp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="10dp"
>
<TextView
android:id="@+id/simpletextonezdy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/red"
android:text="title"
android:textSize="18dp"
/>
<TextView
android:id="@+id/simpletexttwozdy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="info"
/>
</LinearLayout>
</LinearLayout>
3、创建一个java源文件
package com.example.jj;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import org.w3c.dom.Text;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Administrator on 14-11-24.
*/
public class SimpleApaterZdy extends Activity {
private ListView listViewzdy;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simpleadaper2);
//获取XML文件中存放列表listview 中id名
listViewzdy = (ListView) findViewById(R.id.simpleAdaperbtnzdydy);
//获取MyBaseAdapter的数据,MyBaseAdapter是新建另一个java文件
MyBaseAdapter adapter = new MyBaseAdapter(this,getData());
listViewzdy.setAdapter(adapter);
}
------------------------------------------------------------------------------------------------------------------------------------------------------
Massage外部类
package com.example.jj;
/**
* Created by Administrator on 14-11-24.
*/
public class Massages {
private int id;
private String title;
private String content;
private int headerIcon;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public int getHeaderIcon() {
return headerIcon;
}
public void setHeaderIcon(int headerIcon) {
this.headerIcon = headerIcon;
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------
//获取Massges外部类(也可以内部类实现)
public List<Massages> getData() {
List<Massages> list = new ArrayList<Massages>();
//new一个massage,装要在XML中展示的内容
Massages msg = new Massages();
msg.setId(10);//获取ID
msg.setTitle("发vbwqrfvqfvqv去");
msg.setHeaderIcon(R.drawable.tubiao);//获取图标
msg.setContent("contet111111");获取内容
Massages msg1 = new Massages();
msg1.setId(11);
msg1.setTitle("发vbwqrfvqfvqv去1111");
msg1.setHeaderIcon(R.drawable.a);
msg.setContent("conte222");
Massages msg2 = new Massages();
msg2.setId(12);
msg2.setTitle("发vbwqrfvqfvqv去222221");
msg2.setHeaderIcon(R.drawable.c);
msg.setContent("contet23333");
list.add(msg);
list.add(msg1);
list.add(msg2);
return list;
}
}
4、新建一个java文件实现它的四种方法,对XML中的插件做初始化处理
package com.example.jj;
import android.content.Context;
import android.os.Message;
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 java.util.ArrayList;
import java.util.List;
/**
* Created by Administrator on 14-11-24.
*/
public class MyBaseAdapter extends BaseAdapter {
private List<Massages> mList=new ArrayList<Massages>();
private LayoutInflater mInflater;
private Context mcontext;
public MyBaseAdapter(Context context, List<Massages> list) {
mcontext=context;
mInflater = LayoutInflater.from(mcontext);
mList = list;
}
//实现方法
@Override
public int getCount() {
return mList.size();
}
@Override
public Object getItem(int position) {
return mList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
//初始化XML文件里各插件的值
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = mInflater.inflate(R.layout.simpleapaterzdy, null);
ImageView iconImg = (ImageView) view.findViewById(R.id.imgeviewzdy);
TextView titleTxt = (TextView) view.findViewById(R.id.simpletextonezdy);
TextView infoTxt = (TextView) view.findViewById(R.id.simpletexttwozdy);
Massages msg = (Massages) getItem(position);
iconImg.setImageResource(msg.getHeaderIcon());
titleTxt.setText(msg.getTitle());
infoTxt.setText(msg.getContent());
return view;
}
}
这篇关于simpleApater自定义列表(程序)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-09-07面向对象编程基础:初学者指南
- 2024-09-07数据结构入门:了解基础概念与简单应用
- 2024-09-07面向对象编程入门:掌握基本概念与实践
- 2024-09-07数据结构入门:从基础概念到基本操作
- 2024-09-07从零开始学数据库:入门级指南与实操技巧
- 2024-09-07综合设备故障分析与预防:实例代码与优化策略
- 2024-09-07数据库入门:轻松掌握数据管理基础
- 2024-09-07面向对象入门:快速掌握对象编程基础
- 2024-09-07数据库教程:入门与实践指南
- 2024-09-07面向对象开发教程 - 从零基础到实践应用的进阶之路