Je veux créer un ListView
personnalisé comme ceci:
Je pense que je dois utiliser BaseAdapter
mais je n'en ai aucune idée.
main.xml:
<RelativeLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
xmlns:tools="http://schemas.Android.com/tools"
Android:layout_width="match_parent"
Android:layout_height="match_parent"
Android:paddingBottom="@dimen/activity_vertical_margin"
Android:paddingLeft="@dimen/activity_horizontal_margin"
Android:paddingRight="@dimen/activity_horizontal_margin"
Android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ListView
Android:id="@+id/list"
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:layout_alignParentLeft="true"
Android:layout_alignParentTop="true" >
</ListView>
</RelativeLayout>
custom.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:layout_width="match_parent"
Android:layout_height="wrap_content" >
<LinearLayout
Android:layout_width="255dp"
Android:layout_height="wrap_content"
Android:orientation="vertical" >
<LinearLayout
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:orientation="vertical" >
<TextView
Android:id="@+id/title"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:text="Video1"
Android:textAppearance="?android:attr/textAppearanceLarge"
Android:textColor="#339966"
Android:textStyle="bold" />
</LinearLayout>
<LinearLayout
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:orientation="vertical" >
<TextView
Android:id="@+id/detail"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:text="video1"
Android:textColor="#606060" />
</LinearLayout>
</LinearLayout>
<ImageView
Android:id="@+id/img"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:src="@drawable/ic_launcher" />
</LinearLayout>
</LinearLayout>
main.Java:
package com.example.sample;
import Android.app.Activity;
import Android.os.Bundle;
import Android.view.LayoutInflater;
import Android.view.View;
import Android.view.ViewGroup;
import Android.widget.BaseAdapter;
import Android.widget.ImageView;
import Android.widget.ListView;
import Android.widget.TextView;
public class MainActivity extends Activity {
ListView l1;
String[] t1={"video1","video2"};
String[] d1={"lesson1","lesson2"};
int[] i1 ={R.drawable.ic_launcher,R.drawable.ic_launcher};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
l1=(ListView)findViewById(R.id.list);
l1.setAdapter(new dataListAdapter(t1,d1,i1));
}
class dataListAdapter extends BaseAdapter {
String[] Title, Detail;
int[] imge;
dataListAdapter() {
Title = null;
Detail = null;
imge=null;
}
public dataListAdapter(String[] text, String[] text1,int[] text3) {
Title = text;
Detail = text1;
imge = text3;
}
public int getCount() {
// TODO Auto-generated method stub
return Title.length;
}
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row;
row = inflater.inflate(R.layout.custom, parent, false);
TextView title, detail;
ImageView i1;
title = (TextView) row.findViewById(R.id.title);
detail = (TextView) row.findViewById(R.id.detail);
i1=(ImageView)row.findViewById(R.id.img);
title.setText(Title[position]);
detail.setText(Detail[position]);
i1.setImageResource(imge[position]);
return (row);
}
}
}
Essaye ça.
public class ListElementAdapter extends BaseAdapter{
String[] data;
Context context;
LayoutInflater layoutInflater;
public ListElementAdapter(String[] data, Context context) {
super();
this.data = data;
this.context = context;
layoutInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return data.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView= layoutInflater.inflate(R.layout.item, null);
TextView txt=(TextView)convertView.findViewById(R.id.text);
txt.setText(data[position]);
return convertView;
}
}
Appelez simplement ListElementAdapter dans votre activité principale et définissez Adapter sur ListView.
Créez votre propre classe BaseAdapter et utilisez-la comme suit.
public class NotificationScreen extends Activity
{
@Override
protected void onCreate_Impl(Bundle savedInstanceState)
{
setContentView(R.layout.notification_screen);
ListView notificationList = (ListView) findViewById(R.id.notification_list);
NotiFicationListAdapter notiFicationListAdapter = new NotiFicationListAdapter();
notificationList.setAdapter(notiFicationListAdapter);
homeButton = (Button) findViewById(R.id.home_button);
}
}
Créez votre propre classe BaseAdapter et son fichier XML séparé.
public class NotiFicationListAdapter extends BaseAdapter
{
private ArrayList<HashMap<String, String>> data;
private LayoutInflater inflater=null;
public NotiFicationListAdapter(ArrayList data)
{
this.data=data;
inflater =(LayoutInflater)baseActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount()
{
return data.size();
}
public Object getItem(int position)
{
return position;
}
public long getItemId(int position)
{
return position;
}
public View getView(int position, View convertView, ViewGroup parent)
{
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.notification_list_item, null);
ImageView compleatImageView=(ImageView)vi.findViewById(R.id.complet_image);
TextView name = (TextView)vi.findViewById(R.id.game_name); // name
TextView email_id = (TextView)vi.findViewById(R.id.e_mail_id); // email ID
TextView notification_message = (TextView)vi.findViewById(R.id.notification_message); // notification message
compleatImageView.setBackgroundResource(R.id.address_book);
name.setText(data.getIndex(position));
email_id.setText(data.getIndex(position));
notification_message.setTextdata.getIndex(position));
return vi;
}
}
Fichier xml BaseAdapter.
<RelativeLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
Android:id="@+id/inner_layout"
Android:layout_width="fill_parent"
Android:layout_height="wrap_content"
Android:layout_marginBottom="5dp"
Android:layout_marginLeft="10dp"
Android:layout_weight="4"
Android:background="@drawable/list_view_frame"
Android:gravity="center_vertical"
Android:padding="5dp" >
<TextView
Android:id="@+id/game_name"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:text="Game name"
Android:textColor="#FFFFFF"
Android:textSize="15dip"
Android:textStyle="bold"
Android:typeface="sans" />
<TextView
Android:id="@+id/e_mail_id"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:layout_below="@id/game_name"
Android:layout_marginTop="1dip"
Android:text="E-Mail Id"
Android:textColor="#FFFFFF"
Android:textSize="10dip" />
<TextView
Android:id="@+id/notification_message"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:layout_below="@id/game_name"
Android:layout_toRightOf="@id/e_mail_id"
Android:paddingLeft="5dp"
Android:text="Notification message"
Android:textColor="#FFFFFF"
Android:textSize="10dip" />
<ImageView
Android:id="@+id/complet_image"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:layout_alignParentRight="true"
Android:layout_centerVertical="true"
Android:layout_marginBottom="30dp"
Android:layout_marginRight="10dp"
Android:src="@drawable/complete_tag"
Android:visibility="invisible" />
</RelativeLayout>
Changez-le en conséquence et utilisez-le.
Je suggère d'utiliser un adaptateur personnalisé, créez d'abord un fichier XML, par exemple layout/customlistview.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:Android="http://schemas.Android.com/apk/res/Android" Android:layout_width="fill_parent" Android:layout_height="wrap_content" >
<ImageView
Android:id="@+id/image"
Android:layout_alignParentRight="true"
Android:paddingRight="4dp" />
<TextView
Android:id="@+id/title"
Android:layout_toLeftOf="@id/image"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:textSize="23sp"
Android:maxLines="1" />
<TextView
Android:id="@+id/subtitle"
Android:layout_toLeftOf="@id/image" Android:layout_below="@id/title"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content" />
</RelativeLayout>
En supposant que vous ayez une classe personnalisée comme celle-ci
public class CustomClass {
private long id;
private String title, subtitle, picture;
public CustomClass () {
}
public CustomClass (long id, String title, String subtitle, String picture) {
this.id = id;
this.title= title;
this.subtitle= subtitle;
this.picture= picture;
}
//add getters and setters
}
Et un CustomAdapter.Java
utilise la mise en page XML
public class CustomAdapter extends ArrayAdapter {
private Context context;
private int resource;
private LayoutInflater inflater;
public CustomAdapter (Context context, List<CustomClass> values) { // or String[][] or whatever
super(context, R.layout.customlistviewitem, values);
this.context = context;
this.resource = R.layout.customlistview;
this.inflater = LayoutInflater.from(context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = (RelativeLayout) inflater.inflate(resource, null);
CustomClass item = (CustomClass) getItem(position);
TextView textviewTitle = (TextView) convertView.findViewById(R.id.title);
TextView textviewSubtitle = (TextView) convertView.findViewById(R.id.subtitle);
ImageView imageview = (ImageView) convertView.findViewById(R.id.image);
//fill the textviews and imageview with the values
textviewTitle = item.getTtile();
textviewSubtitle = item.getSubtitle();
if (item.getAfbeelding() != null) {
int imageResource = context.getResources().getIdentifier("drawable/" + item.getImage(), null, context.getPackageName());
Drawable image = context.getResources().getDrawable(imageResource);
}
imageview.setImageDrawable(image);
return convertView;
}
}
Avez-vous réussi à le faire? N'hésitez pas à demander si vous voulez plus d'informations sur quelque chose :)
EDIT : Modification de l'adaptateur pour l'adapter à une liste plutôt qu'à une simple liste
private class ObjectAdapter extends BaseAdapter {
private Context context;
private List<Object>objects;
public ObjectAdapter(Context context, List<Object> objects) {
this.context = context;
this.objects = objects;
}
@Override
public int getCount() {
return objects.size();
}
@Override
public Object getItem(int position) {
return objects.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView==null){
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(Android.R.layout.simple_list_item_1, parent, false);
holder.text = (TextView) convertView.findViewById(Android.R.id.text1);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.text.setText(getItem(position).toString()));
return convertView;
}
class ViewHolder {
TextView text;
}
}