我最近开始学习android并遇到了这个问题: 点击按钮后,我将新的TextView显示到我的ListView - 它是3个人之一的新付款:Lukasz / Marcelina / Karolina - 我想让每个Lukasz'付款显示为RED,每个Marcelinas BLUE和Karolinas - GREEN
到目前为止我尝试过的是在ONCLICK方法的每个CASE中为一个标志(1,2或3)赋值并添加if语句
if(flag==1){
holder.osoba.setTextColor(Color.RED);
}
else if(flag==2){
holder.osoba.setTextColor(Color.BLUE);
}else if (flag == 3) {
holder.osoba.setTextColor(Color.GREEN);
}
但事实证明我没有为我列表中的特定项目设置颜色,而是为所有项目设置颜色(例如,当我按下lukas'它会给我RED Lukasz&#39的付款,但是然后,当我做一个Marcelina的新付款时,应该是BLUE(而Lukasz应该是RED),它会使列表中的每笔付款都变为蓝色。任何想法如何设置每个List元素的不同颜色?
我的主要活动如下:
公共类MainActivity扩展ListActivity实现了View.OnClickListener {
private int flag = 0;
private TextView sum;
private Button addButton;
private EditText addPrice;
private TextView sumPerPerson;
private EditText description;
private ListaWplat listaWplat;
private RadioGroup czlonek;
private RadioButton lukaszRadioButton;
private RadioButton marcelinaRadioButton;
private RadioButton karolinaRadioButton;
private TextView sumaLukasz;
private TextView sumaMarcelina;
private TextView sumaKarolina;
private float tempLukasz = 0;
private float tempMarcelina = 0;
private float tempKarolina = 0;
public float getTempMarcelina() {
return tempMarcelina;
}
public float getTempLukasz() {
return tempLukasz;
}
public float getTempKarolina() {
return tempKarolina;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sum = (TextView) findViewById(R.id.suma);
addButton = (Button) findViewById(R.id.addButton);
addPrice = (EditText) findViewById(R.id.dodajCene);
sumPerPerson = (TextView) findViewById(R.id.sumaNaOsobe);
description = (EditText) findViewById(R.id.description);
sumaLukasz = (TextView) findViewById(R.id.sumaLukasz);
sumaMarcelina = (TextView) findViewById(R.id.sumaMarcelina);
sumaKarolina = (TextView) findViewById(R.id.sumaKarolina);
czlonek = (RadioGroup) findViewById(R.id.czlonek);
lukaszRadioButton = (RadioButton) findViewById(R.id.lukaszRadioButton);
marcelinaRadioButton = (RadioButton) findViewById(R.id.marcelinaRadioButton);
karolinaRadioButton = (RadioButton) findViewById(R.id.karolinaRadioButton);
addButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
int checkedRadioButtonId = czlonek.getCheckedRadioButtonId();
switch (checkedRadioButtonId) {
case R.id.lukaszRadioButton:
if (lukaszRadioButton.isChecked()) {
try {
tempLukasz += valueOf(addPrice.getText().toString());
} catch (NumberFormatException e) {
}
// dodanie $ do sumy lukasza
String sumaLukasza = getString(R.string.sumaLukasz);
sumaLukasza = String.format(sumaLukasza, getTempLukasz());
sumaLukasz.setText(sumaLukasza + " zl");
flag = 1;
NowaWplata nowaWplata = new NowaWplata(lukaszRadioButton.getText().toString(), addPrice.getText().toString(), description.getText().toString());
listaWplat.setWplaty(nowaWplata);
setListAdapter(new NewPaymentAdapter(this, R.layout.nowa_wplata, listaWplat.getWplaty()));
}
break;
case R.id.marcelinaRadioButton:
if (marcelinaRadioButton.isChecked()) {
try {
tempMarcelina += valueOf(addPrice.getText().toString());
} catch (NumberFormatException e) {
}
// dodanie $ do sumy marceliny
String sumaMarceliny = getString(R.string.sumaMarcelina);
sumaMarceliny = String.format(sumaMarceliny, getTempMarcelina());
sumaMarcelina.setText(sumaMarceliny + " zl");
flag=2;
NowaWplata nowaWplata = new NowaWplata(marcelinaRadioButton.getText().toString(), addPrice.getText().toString(), description.getText().toString());
listaWplat.setWplaty(nowaWplata);
setListAdapter(new NewPaymentAdapter(this, R.layout.nowa_wplata, listaWplat.getWplaty()));
}
break;
case R.id.karolinaRadioButton:
if (karolinaRadioButton.isChecked()) {
try {
tempKarolina += valueOf(addPrice.getText().toString());
} catch (NumberFormatException e) {
}
// dodanie $ do sumy karoliny
String sumaKaroliny = getString(R.string.sumaKarolina);
sumaKaroliny = String.format(sumaKaroliny, getTempKarolina());
sumaKarolina.setText(sumaKaroliny + " zl");
flag=3;
NowaWplata nowaWplata = new NowaWplata(karolinaRadioButton.getText().toString(), addPrice.getText().toString(), description.getText().toString());
ListaWplat.setWplaty(nowaWplata);
NewPaymentAdapter npa = new NewPaymentAdapter(this, R.layout.nowa_wplata, listaWplat.getWplaty());
setListAdapter(npa);
}
break;
}
String sumaCalkowita = getString(R.string.sumaCalkowita);
sumaCalkowita = String.format(sumaCalkowita, getTempKarolina() + getTempLukasz() + getTempMarcelina());
sum.setText(sumaCalkowita);
String sumaSrednia = getString(R.string.sumaSrednio);
sumaSrednia = String.format(sumaSrednia, (getTempKarolina() + getTempLukasz() + getTempMarcelina()) / 3);
sumPerPerson.setText(sumaSrednia);
}
class NewPaymentAdapter extends ArrayAdapter<NowaWplata> {
public LayoutInflater layoutInflater;
public NewPaymentAdapter(Context context, int textViewResourceId, List<NowaWplata> wplaty) {
super(context, textViewResourceId, wplaty);
layoutInflater = LayoutInflater.from(context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
NowaWplata nowaWplata = getItem(position);
Holder holder = null;
if(view == null){
view = layoutInflater.inflate(R.layout.nowa_wplata, null);
TextView osobyWplata = (TextView) view.findViewById(R.id.osobaWplata);
TextView kwotaWplaty = (TextView) view.findViewById(R.id.kwotaWplata);
TextView opisWplaty = (TextView) view.findViewById(R.id.opisWplata);
holder = new Holder(osobyWplata, kwotaWplaty, opisWplaty);
view.setTag(holder);
}else{
holder = (Holder) view.getTag();
}
SpannableString spannableString = new SpannableString(nowaWplata.getOsoba().toString());
holder.osoba.setText("[" + nowaWplata.getDate() + "] " + spannableString);
holder.kwota.setText("- " + nowaWplata.getWplata() + "zł");
holder.opis.setText(nowaWplata.getOpis());
return view;
}
}
static class Holder{
public TextView osoba;
public TextView kwota;
public TextView opis;
public Holder(TextView osoba, TextView kwota, TextView opis) {
this.osoba = osoba;
this.opis = opis;
this.kwota = kwota;
}
}
}
答案 0 :(得分:0)
将此标志添加到NowaWplata类:
class NowaWplata {
int flag;
public NowaWplata (String par1, String par2, String par3, int flag) {
this.flag = flag;
...
}
}
然后以下一种方式创建实例:
flag=2;
NowaWplata nowaWplata = new NowaWplata(marcelinaRadioButton.getText().toString(), addPrice.getText().toString(), description.getText().toString(), flag);
并更新您的NewPaymentAdapter.getView()方法
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
NowaWplata nowaWplata = getItem(position);
Holder holder = null;
if(view == null){
view = layoutInflater.inflate(R.layout.nowa_wplata, null);
TextView osobyWplata = (TextView) view.findViewById(R.id.osobaWplata);
TextView kwotaWplaty = (TextView) view.findViewById(R.id.kwotaWplata);
TextView opisWplaty = (TextView) view.findViewById(R.id.opisWplata);
holder = new Holder(osobyWplata, kwotaWplaty, opisWplaty);
view.setTag(holder);
}else{
holder = (Holder) view.getTag();
}
SpannableString spannableString = new SpannableString(nowaWplata.getOsoba().toString());
holder.osoba.setText("[" + nowaWplata.getDate() + "] " + spannableString);
holder.kwota.setText("- " + nowaWplata.getWplata() + "zł");
holder.opis.setText(nowaWplata.getOpis());
if(nowaWplata.flag==1){
holder.osoba.setTextColor(Color.RED);
}
else if(nowaWplata.flag==2){
holder.osoba.setTextColor(Color.BLUE);
}else if (nowaWplata.flag == 3) {
holder.osoba.setTextColor(Color.GREEN);
}
return view;
}