嘿,所以目標是當單擊串列項的按鈕取消時,該專案的狀態必須顯示為“已取消”并且按鈕變為黑色。但是,當按下按鈕時,按鈕會變黑,但串列項不會改變狀態。相反,視圖串列的最后一個按鈕會更改狀態。你能幫我嗎?
配接器類:
package com.example.myappfinal.Adapter;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.graphics.drawable.DrawableCompat;
import com.example.myappfinal.Logic.Appointment;
import com.example.myappfinal.R;
import java.util.ArrayList;
public class AppointmentListAdapter extends ArrayAdapter<Appointment> {
private String state,date,time;
private TextView stateT,dateT,timeT;
private Context mContext;
int mRecource;
public AppointmentListAdapter(@NonNull Context context, int resource, @NonNull ArrayList<Appointment> objects) {
super(context, resource, objects);
mContext=context;
mRecource=resource;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
state=getItem(position).getState();
String service=getItem(position).getService().getName();
date = getItem(position).getDate();
time = getItem(position).getStartTime();
LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(mRecource,parent,false);
stateT = (TextView) convertView.findViewById(R.id.state);
TextView serviceT = (TextView) convertView.findViewById(R.id.service);
TextView dateT = (TextView) convertView.findViewById(R.id.date);
TextView timeT = (TextView) convertView.findViewById(R.id.time);
stateT.setText(state);
serviceT.setText(service);
dateT.setText(date);
timeT.setText(time);
Button button =(Button)convertView.findViewById(R.id.button);
if(state.equals("Canceled")){
Drawable buttonDrawable = button.getBackground();
buttonDrawable = DrawableCompat.wrap(buttonDrawable);
DrawableCompat.setTint(buttonDrawable, Color.BLACK);
button.setBackground(buttonDrawable);
}
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//do something
if(state.equals("Active")) {
getItem(position).cancel();
state = getItem(position).getState();
stateT.setText(state);
Drawable buttonDrawable = button.getBackground();
buttonDrawable = DrawableCompat.wrap(buttonDrawable);
DrawableCompat.setTint(buttonDrawable, Color.BLACK);
button.setBackground(buttonDrawable);
}
}
});
return convertView;
}
uj5u.com熱心網友回復:
問題是stateT配接器中的一個類成員,您為每一行的getView呼叫覆寫它,因此在填充串列后,它總是參考最后一行。您不應該將視圖(例如stateT)保存為類成員。每次呼叫都getView將覆寫那里保存的先前值。
對于您的情況,您可能應該做這樣的事情(如果沒有完整的代碼,很難確定)
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
// Don't re-inflate unless necessary
if( convertView == null ) {
LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(mRecource,parent,false);
}
// Get the state of the current row
Appointment appt = getItem(position);
String state = appt.getState();
String service = appt.getService().getName();
String date = appt.getDate();
String time = appt.getStartTime();
// These should NOT be class members
TextView stateT = convertView.findViewById(R.id.state);
TextView serviceT = convertView.findViewById(R.id.service);
TextView dateT = convertView.findViewById(R.id.date);
TextView timeT = convertView.findViewById(R.id.time);
// Set the row's values
stateT.setText(state);
serviceT.setText(service);
dateT.setText(date);
timeT.setText(time);
Button button = convertView.findViewById(R.id.button);
if(state.equals("Canceled")){
Drawable buttonDrawable = button.getBackground();
buttonDrawable = DrawableCompat.wrap(buttonDrawable);
DrawableCompat.setTint(buttonDrawable, Color.BLACK);
button.setBackground(buttonDrawable);
// may also want to disable the button
button.setEnabled(false);
}
else {
button.setEnabled(true);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// get the current row state - since it may have been
// updated since the listener was created
String rowState = appt.getState();
if(rowState.equals("Active")) {
appt.cancel();
String newState = appt.getState();
// stateT here refers to just this row's TextView now
stateT.setText(newState);
Drawable buttonDrawable = button.getBackground();
buttonDrawable = DrawableCompat.wrap(buttonDrawable);
DrawableCompat.setTint(buttonDrawable, Color.BLACK);
button.setBackground(buttonDrawable);
// may also want to disable the button
button.setEnabled(false);
}
}
});
}
return convertView;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/487465.html
