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-12-26大厂数据结构与算法教程:入门级详解
- 2024-12-26大厂算法与数据结构教程:新手入门指南
- 2024-12-26Python编程入门指南
- 2024-12-26数据结构高级教程:新手入门及初级提升指南
- 2024-12-26并查集入门教程:从零开始学会并查集
- 2024-12-26大厂数据结构与算法入门指南
- 2024-12-26大厂算法与数据结构入门教程
- 2024-12-26二叉树入门教程:轻松掌握基础概念与操作
- 2024-12-26初学者指南:轻松掌握链表
- 2024-12-26平衡树入门教程:轻松理解与应用