gpt4 book ai didi

java - 列表元素中的背景颜色

转载 作者:行者123 更新时间:2023-11-30 01:29:53 25 4
gpt4 key购买 nike

我有一个列表,每个元素都有一些字符串和图像,我如何更改每个元素的背景颜色 (LinearLayot)?

我的代码:

1)Activity.java中的数组

 ListView listview;
int[] list_photo = {R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d, R.drawable.e};
String[] list_name = {"A", "B", "C", "D", "E"};
String[] list_email = {"a@", "b@", "c@", "d@", "e@"};
String[] list_phone = {"12", "12", "12", "12", "12", "12"};
TrainersAdapter adapter;

并在 Actvity.java 的 OnCreate 中

listview = (ListView) findViewById(R.id.trainerslist);
adapter = new TrainersAdapter(getApplicationContext(), R.layout.list_trainers);
listview.setAdapter(adapter);
int i = 0;
for (i = 0; i < 5; i++) {
TrainersDataProvider dataProvider = new TrainersDataProvider(list_photo[i], list_name[i], list_email[i], list_phone[i]);
adapter.add(dataProvider);
}

2)然后我有适配器:

import android.content.Context;
import android.provider.ContactsContract;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import org.w3c.dom.Text;

import java.util.List;
import java.util.*;

/**
* Created by dd on 2016-03-06.
*/
public class TrainersAdapter extends ArrayAdapter {

List list = new ArrayList();

public TrainersAdapter(Context context, int resource) {
super(context, resource);
}

static class Datahandler{
ImageView photo;
TextView name;
TextView email;
TextView phone;
}

@Override
public void add(Object object) {
super.add(object);
list.add(object);
}

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

@Override
public Object getItem(int position) {
return this.list.get(position);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row;
row = convertView;
Datahandler handler;
if(convertView==null){
LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.list_trainers,parent,false);
handler = new Datahandler();
handler.photo=(ImageView) row.findViewById(R.id.trainerslist_photo);
handler.name=(TextView) row.findViewById(R.id.trainerslist_name);
handler.email=(TextView) row.findViewById(R.id.trainerslist_email);
handler.phone=(TextView) row.findViewById(R.id.trainerslist_phone);
row.setTag(handler);
}
else{
handler = (Datahandler) row.getTag();
}
TrainersDataProvider dataProvider;
dataProvider = (TrainersDataProvider) this.getItem(position);
handler.photo.setImageResource(dataProvider.getTrainer_photo());
handler.name.setText(dataProvider.getTrainer_name());
handler.email.setText(dataProvider.getTrainer_email());
handler.phone.setText(dataProvider.getTrainer_phone());

return row;
}
}

3) 和数据提供者

public class TrainersDataProvider {

private int trainer_photo;
private String trainer_name;
private String trainer_email;
private String trainer_phone;

public TrainersDataProvider(int trainer_photo,String trainer_name, String trainer_email, String trainer_phone){
this.setTrainer_photo(trainer_photo);
this.setTrainer_email(trainer_email);
this.setTrainer_name(trainer_name);
this.setTrainer_phone(trainer_phone);
}

public String getTrainer_email() {
return trainer_email;
}

public void setTrainer_email(String trainer_email) {
this.trainer_email = trainer_email;
}

public String getTrainer_name() {
return trainer_name;
}

public void setTrainer_name(String trainer_name) {
this.trainer_name = trainer_name;
}

public String getTrainer_phone() {
return trainer_phone;
}

public void setTrainer_phone(String trainer_phone) {
this.trainer_phone = trainer_phone;
}

public int getTrainer_photo() {
return trainer_photo;
}

public void setTrainer_photo(int trainer_photo) {
this.trainer_photo = trainer_photo;
}
}

我想在 Activity.java 中有一组颜色(如照片、姓名、电子邮件和电话)并将其放入 Dataprovider 构造函数,这种颜色应该是列表元素中线性布局的背景。

最佳答案

给线性布局一个 id,比如 android:id="@+id/background_layout"。然后在Datahandler中定义为LinearLayout背景,初始化handler.background=(LinearLayout) row.findViewById(R.id.background_layout);并执行 handler.backgorund.setBackgroundColor(来自您的 arrayList 的颜色);

关于java - 列表元素中的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35893873/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com