У меня есть три вида сетки в соответствии с ответом API. Я получаю несколько временных интервалов в соответствии с утренним вечерним днем. Я хочу реализовать одноразовое изменение цвета фона элемента одним щелчком мыши. У меня есть отдельный адаптер из трех сеток, и вот мой код:
private void getTimingSlot() {
ProgressDialog pd = new ProgressDialog(BookingTimeList.this);
pd.setMessage("Please wait..");
pd.show();
ApiInterface apiInterface = RetrofitManager.getInstance().create(ApiInterface.class);
Call<JsonObject> call = apiInterface.getTimeSlot(doctorId);
Log.d("@CallApiBegin", "beginCallApi");
call.enqueue(new Callback<JsonObject>() {
@Override
public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
if (response.isSuccessful()) {
pd.dismiss();
try {
jsonObject = new JSONObject(response.body().toString());
isSuccess = jsonObject.getBoolean("success");
String message = jsonObject.getString("message");
JSONObject doctorTimeSlots = jsonObject.getJSONObject("doctorTimeSlots");
if (isSuccess) {
JSONArray jsonArrayMorning = doctorTimeSlots.getJSONArray("morning");
for (int i = 0; i < jsonArrayMorning.length(); i++) {
maplist.add(jsonArrayMorning.getString(i));
}
GridAdapter adapter = new GridAdapter(BookingTimeList.this);
gridview1.setAdapter(adapter);
JSONArray jsonArrayAfterNoon = doctorTimeSlots.getJSONArray("afternoon");
for (int i = 0; i < jsonArrayAfterNoon.length(); i++) {
maplist2.add(jsonArrayAfterNoon.getString(i));
}
GridAdapterAfter adapterAfter = new GridAdapterAfter(BookingTimeList.this);
gridview2.setAdapter(adapterAfter);
JSONArray jsonArrayEvening = doctorTimeSlots.getJSONArray("evening");
for (int i = 0; i < jsonArrayEvening.length(); i++) {
maplist3.add(jsonArrayEvening.getString(i));
}
GridAdapterEvening adapterEvening = new GridAdapterEvening(BookingTimeList.this);
gridview3.setAdapter(adapterEvening);
} else {
Toast.makeText(BookingTimeList.this, message, Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
@Override
public void onFailure(Call<JsonObject> call, Throwable t) {
}
});
}
вот мои классы адаптера, которые я использую при прослушивании щелчков:
public class GridAdapter extends BaseAdapter {
private Context mContext;
public GridAdapter(Context c) {
mContext = c;
}
public int getCount() {
return maplist.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 v = convertView;
LayoutInflater _inflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (v == null) {
v = _inflater.inflate(R.layout.time_item, null);
}
TextView tvTimeM = v.findViewById(R.id.tvTime);
tvTimeM.setText(maplist.get(position));
tvTimeM.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
selectTime = maplist.get(position);
Log.d("@@selctTime",selectTime );
row_index = position;
notifyDataSetChanged();
}
});
if (row_index == position) {
tvTimeM.setBackgroundResource(R.drawable.btn_shape);
tvTimeM.setTextColor(Color.parseColor("#ffffff"));
} else {
tvTimeM.setBackgroundResource(R.drawable.edt_text_back);
tvTimeM.setTextColor(Color.parseColor("#000000"));
}
return v;
}
}
public class GridAdapterAfter extends BaseAdapter {
// int row_index;
private Context mContext;
public GridAdapterAfter(Context c) {
mContext = c;
}
public int getCount() {
return maplist.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 v = convertView;
LayoutInflater _inflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (v == null) {
v = _inflater.inflate(R.layout.time_item, null);
}
TextView tvTime = v.findViewById(R.id.tvTime);
tvTime.setText(maplist.get(position));
tvTime.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
selectTime = maplist.get(position);
Log.d("@@selctTime",selectTime );
row_index = position;
notifyDataSetChanged();
}
});
if (row_index == position) {
tvTime.setBackgroundResource(R.drawable.btn_shape);
tvTime.setTextColor(Color.parseColor("#ffffff"));
} else {
tvTime.setBackgroundResource(R.drawable.edt_text_back);
tvTime.setTextColor(Color.parseColor("#000000"));
}
return v;
}
}
public class GridAdapterEvening extends BaseAdapter {
private Context mContext;
public GridAdapterEvening(Context c) {
mContext = c;
}
public int getCount() {
return maplist.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 v = convertView;
LayoutInflater _inflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (v == null) {
v = _inflater.inflate(R.layout.time_item, null);
}
TextView tvTime = v.findViewById(R.id.tvTime);
tvTime.setText(maplist.get(position));
tvTime.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
selectTime = maplist.get(position);
Log.d("@@selctTime",selectTime );
row_index = position;
notifyDataSetChanged();
}
});
if (row_index == position) {
tvTime.setBackgroundResource(R.drawable.btn_shape);
tvTime.setTextColor(Color.parseColor("#ffffff"));
} else {
tvTime.setBackgroundResource(R.drawable.edt_text_back);
tvTime.setTextColor(Color.parseColor("#000000"));
}
return v;
}
}
вот мое основное занятие, в котором я определил три списка для элемента сетки и индекса строки:
public class BookingTimeList extends AppCompatActivity {
private GridView gridview1, gridview2, gridview3;
private ArrayList<String> maplist = new ArrayList<>();
private ArrayList<String> maplist2 = new ArrayList<>();
private ArrayList<String> maplist3 = new ArrayList<>();
private JSONObject jsonObject;
private boolean isSuccess;
private int doctorId, healthid;
String selectedDate, visitType, selectTime;
int row_index;
private Button btnSubmit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_booking_time_list);
healthid = WeLoveCareApplication.ReadIntPreferences(SharedPrefData.PREF_HEALTH_ID);
doctorId = getIntent().getIntExtra("doctorId", 0);
visitType = getIntent().getStringExtra("visitType");
selectedDate = getIntent().getStringExtra("selectedDate");
Log.d("@@doctorId", String.valueOf(doctorId));
gridview1 = findViewById(R.id.mainGridView1);
gridview2 = findViewById(R.id.mainGridView2);
gridview3 = findViewById(R.id.mainGridView3);
btnSubmit = findViewById(R.id.btnSubmit);
getTimingSlot();
btnSubmit.setOnClickListener(v -> {
bookAppointment(selectTime);
});
}
Я хочу, чтобы изменение фона элемента одним щелчком мыши было выполнено при щелчке по этому конкретному элементу. помогите мне, я буду признателен за каждый ответ, заранее спасибо