在下面,我发布了我的json,其中我将获得数天的数组。 如果数组包含[5,6,7],则更改3个文本视图的颜色即。卡片视图中的Th,Fr,Sa。
[
{
"id":112,
"patient":"sandeepsingh@innotical.com",
"salt":"sdfdsf",
"doctor":"Dr",
"amount":null,
"unit":null,
"method":null,
"date":"2006-04-02",
"note":null,
"frequency":null,
"reminder":[
]
},
{
"id":114,
"patient":"sandeepsingh@innotical.com",
"salt":"Zaleplon",
"doctor":"ufuffu",
"amount":"13",
"unit":1,
"method":1,
"date":"2016-06-02",
"note":"chchjf",
"frequency":null,
"reminder":[
{
"id":1,
"time":" ",
"days":[
1,
2,
3,
4,
5,
6,
7
]
},
{
"id":2,
"time":" ",
"days":[
5,
6,
7
]
}
]
}
]
并且在这里我发布了我想要更改颜色位置的cardview图像。我想要提前完成一个完整的解决方案。
我的解析代码如下。
@Override
public void success(List<SavedMedicationPojo> savedMedicationPojo, Response response) {
savedMedicationPojo1.addAll(savedMedicationPojo);
// medSetter.setMedicationPojo(savedMedicationPojo1);
if (savedMedicationPojo.size()>0){
for (int i = 0; i < savedMedicationPojo.size(); i++) {
// SavedMedicationPojo medication = new SavedMedicationPojo();
mSaltIdList.add(savedMedicationPojo.get(i).getId());
mSaltList.add(savedMedicationPojo.get(i).getSalt());
mSaltAmountList.add(savedMedicationPojo.get(i).getAmount());
mSaltUnitList.add(savedMedicationPojo.get(i).getUnit());
mSaltMethodList.add(savedMedicationPojo.get(i).getMethod());
mSaltDateList.add(savedMedicationPojo.get(i).getDate());
mSaltNoteList.add(savedMedicationPojo.get(i).getNote());
mSaltDoctorList.add(savedMedicationPojo.get(i).getDoctor());
mSaltFrequencyList.add(savedMedicationPojo.get(i).getFrequency());
// savedMedicationPojo.get(i).getReminder().get(i).
int msize = savedMedicationPojo.get(i).getReminder().size();
/*if(msize==0)
{ArrayList<Integer> mDaysList= new ArrayList<Integer>();
mReminderDaysHashMap.put(i, mDaysList);
}*/
for (int j = 0;j< savedMedicationPojo.get(i).getReminder().size();j++){
mReminderId.add(savedMedicationPojo.get(i).getReminder().get(j).getId());
mReminderTime.add(savedMedicationPojo.get(i).getReminder().get(j).getTime());
ArrayList<Integer> mDaysList= new ArrayList<Integer>();
for (int k = 0; k < savedMedicationPojo.get(i).getReminder().get(j).getDays().size(); k++) {
mDaysList.add(savedMedicationPojo.get(i).getReminder().get(j).getDays().get(k));
}
mReminderDaysHashMap.put(j, mDaysList);
// }
}
}
mDataAdapter.notifyDataSetChanged();
pb.setVisibility(View.INVISIBLE);
}else{
mNoDataView.setVisibility(View.VISIBLE);
}
}
我的cardview xml如下
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/cardChildGroup"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="05dp"
android:gravity="center">
<RelativeLayout
android:id="@+id/timeBellView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="02dp">
<RelativeLayout
android:id="@+id/bellView"
android:layout_width="wrap_content"
android:layout_margin="02dp"
android:layout_centerVertical="true"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/alarmBell"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:foreground="?android:attr/selectableItemBackground"
android:src="@drawable/bell" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/timeView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_toRightOf="@id/bellView">
<TextView
android:id="@+id/timeDigit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="05dp"
android:text="08:30"
android:textColor="#000000"
android:textSize="30sp" />
<TextView
android:id="@+id/timeUnit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@id/timeDigit"
android:layout_margin="05dp"
android:layout_toRightOf="@id/timeDigit"
android:text="AM"
android:textColor="#000000"
android:textSize="18sp" />
</RelativeLayout>
<!--<RelativeLayout
android:id="@+id/timeUnitView"
android:layout_toRightOf="@id/timeView"
android:gravity="center_vertical"
android:layout_margin="05dp"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<!– <TextView
android:id="@+id/timeUnit"
android:text="AM"
android:textSize="15sp"
android:gravity="bottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />–>
</RelativeLayout>-->
<RelativeLayout
android:id="@+id/switchView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_margin="05dp"
android:layout_toRightOf="@id/timeView">
<android.support.v7.widget.SwitchCompat
android:id="@+id/alarmPowerSwitch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right|center_vertical" />
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:id="@+id/daysView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/timeBellView"
android:layout_margin="02dp"
android:gravity="center">
<TextView
android:id="@+id/sunday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="01dp"
android:onClick="Sunday"
android:textColor="@color/colorSecondary"
android:foreground="?android:attr/selectableItemBackground"
android:padding="12dp"
android:text="SU" />
<TextView
android:id="@+id/monday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="01dp"
android:layout_toRightOf="@id/sunday"
android:textColor="@color/colorSecondary"
android:foreground="?android:attr/selectableItemBackground"
android:padding="12dp"
android:text="MO" />
<TextView
android:id="@+id/tuesday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="01dp"
android:layout_toRightOf="@id/monday"
android:textColor="@color/colorSecondary"
android:foreground="?android:attr/selectableItemBackground"
android:padding="12dp"
android:text="TU" />
<TextView
android:id="@+id/wednesday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="01dp"
android:textColor="@color/colorSecondary"
android:layout_toRightOf="@id/tuesday"
android:foreground="?android:attr/selectableItemBackground"
android:padding="12dp"
android:text="WE" />
<TextView
android:id="@+id/thursday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="01dp"
android:textColor="@color/colorSecondary"
android:layout_toRightOf="@id/wednesday"
android:foreground="?android:attr/selectableItemBackground"
android:padding="12dp"
android:text="TH" />
<TextView
android:id="@+id/friday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="01dp"
android:textColor="@color/colorSecondary"
android:layout_toRightOf="@id/thursday"
android:foreground="?android:attr/selectableItemBackground"
android:padding="12dp"
android:text="FR" />
<TextView
android:id="@+id/saturday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="01dp"
android:textColor="@color/colorSecondary"
android:layout_toRightOf="@id/friday"
android:foreground="?android:attr/selectableItemBackground"
android:padding="12dp"
android:text="SA" />
</RelativeLayout>
</RelativeLayout>
下面的我的适配器类。
公共类AlarmAdapter扩展了RecyclerView.Adapter {
private final int VIEW_ITEM = 1;
private final int VIEW_PROG = 0;
private List<String> myDataSet;
private static String KEY1;
// The minimum amount of items to have below your current scroll position
// before loading more.
private int visibleThreshold = 5;
private int lastVisibleItem, totalItemCount;
private boolean loading;
private OnLoadMoreListener onLoadMoreListener;
private static String TAG = "DATA_ADAPTER";
private static ArrayList<String> mSavedTime = new ArrayList<String>();
private static ArrayList<String> mTimeUnit = new ArrayList<>();
private static String mCurrentHour;
private static String mCurrentMinute;
private static Context context;
private static int mBellCount = 1;
private static int mSunCount = 1;
private static int mMonCount = 1;
private static int mTueCount = 1;
private static int mWedCount = 1;
private static int mThuCount = 1;
private static int mFriCount = 1;
private static int mSatCount = 1;
private static ArrayList<Integer> mDaysArrayList = new ArrayList<Integer>();
private static ArrayList<Integer> mRecievedDays = new ArrayList<Integer>();
private SharedPreferences mSharedPreferences;
/*public AlarmAdapter(Context context, RecyclerView recyclerView, ArrayList<String> mSavedTime,
String mUserSavedPin, ArrayList<Integer> mFileId, Integer mFamilyMemberId,
String mResponseToken, ArrayList<String> mFileDate, ArrayList<String> mFileUrl,
ArrayList<String> mFileDescription) {*/
public AlarmAdapter(Context context, RecyclerView recyclerView, ArrayList<String> mTime,ArrayList<String> mTimeUnit,String mCurrentHour,String mCurrentMinute) {
this.mSavedTime = mTime;
this.mTimeUnit = mTimeUnit;
this.mCurrentHour = mCurrentHour;
this.mCurrentMinute = mCurrentMinute;
/*this.mSavedTime = mSavedTime;
this.mFileId = mFileId;
this.mUserSavedPin = mUserSavedPin;
this.mFamilyMemberId = mFamilyMemberId;
this.mResponseToken = mResponseToken;
this.mFileDate = mFileDate;
this.mFileUrl = mFileUrl;
this.mFileDescription = mFileDescription;*/
this.context = context;
mSharedPreferences=context.getSharedPreferences("AlarmAdapterSharedPreferences",Context.MODE_PRIVATE);
if (recyclerView.getLayoutManager() instanceof LinearLayoutManager) {
final LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView
.getLayoutManager();
recyclerView
.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView,
int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
totalItemCount = linearLayoutManager.getItemCount();
lastVisibleItem = linearLayoutManager
.findLastVisibleItemPosition();
if (!loading
&& totalItemCount <= (lastVisibleItem + visibleThreshold)) {
// End has been reached
// Do something
if (onLoadMoreListener != null) {
onLoadMoreListener.onLoadMore();
}
loading = true;
}
}
});
}
}
@Override
public int getItemViewType(int position) {
return mSavedTime.get(position) != null ? VIEW_ITEM : VIEW_PROG;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
RecyclerView.ViewHolder vh;
if (viewType == VIEW_ITEM) {
View v = LayoutInflater.from(parent.getContext()).inflate(
R.layout.saved_alarm_card, parent, false);
vh = new StudentViewHolder(v);
} else {
View v = LayoutInflater.from(parent.getContext()).inflate(
R.layout.more_progress_item, parent, false);
vh = new ProgressViewHolder(v);
}
return vh;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof StudentViewHolder) {
((StudentViewHolder) holder).mTimeDigitText.setText(mSavedTime.get(position));
((StudentViewHolder) holder).mTimeUnitText.setText(mTimeUnit.get(position));
// ((StudentViewHolder) holder).dateField.setText(mFileDate.get(position));
if (ActivityUserMedication.mReminderDaysHashMap.get(position).contains(1)){
((StudentViewHolder) holder).mSundayButton.setTextColor(ContextCompat.getColor(context,R.color.colorProfilePrimary));
}
mRecievedDays.add(ActivityUserMedication.savedMedicationPojo1.get(1).getReminder().get(1).getDays().get(1));
/* if (mRecievedDays.contains(1)){
((StudentViewHolder) holder).mSundayButton.setTextColor(ContextCompat.getColor(context,R.color.colorProfilePrimary));
}
else if (mRecievedDays.contains(2)){
((StudentViewHolder) holder).mMondayButton.setTextColor(ContextCompat.getColor(context,R.color.colorProfilePrimary));
}
else if (mRecievedDays.contains(3)){
((StudentViewHolder) holder).mTuesdayButton.setTextColor(ContextCompat.getColor(context,R.color.colorProfilePrimary));
}
else if (mRecievedDays.contains(4)){
((StudentViewHolder) holder).mWednesdayButton.setTextColor(ContextCompat.getColor(context,R.color.colorProfilePrimary));
}
else if (mRecievedDays.contains(5)){
((StudentViewHolder) holder).mThursdayButton.setTextColor(ContextCompat.getColor(context,R.color.colorProfilePrimary));
}
else if (mRecievedDays.contains(6)){
((StudentViewHolder) holder).mFridayButton.setTextColor(ContextCompat.getColor(context,R.color.colorProfilePrimary));
}
else if (mRecievedDays.contains(7)){
((StudentViewHolder) holder).mSaturdayButton.setTextColor(ContextCompat.getColor(context,R.color.colorProfilePrimary));
}
else{
((StudentViewHolder) holder).mSundayButton.setTextColor(ContextCompat.getColor(context,R.color.colorSecondary));
((StudentViewHolder) holder).mMondayButton.setTextColor(ContextCompat.getColor(context,R.color.colorSecondary));
((StudentViewHolder) holder).mTuesdayButton.setTextColor(ContextCompat.getColor(context,R.color.colorSecondary));
((StudentViewHolder) holder).mWednesdayButton.setTextColor(ContextCompat.getColor(context,R.color.colorSecondary));
((StudentViewHolder) holder).mThursdayButton.setTextColor(ContextCompat.getColor(context,R.color.colorSecondary));
((StudentViewHolder) holder).mFridayButton.setTextColor(ContextCompat.getColor(context,R.color.colorSecondary));
((StudentViewHolder) holder).mSaturdayButton.setTextColor(ContextCompat.getColor(context,R.color.colorSecondary));
}*/
/* if(position==1)
{
((StudentViewHolder) holder).mSaturdayButton.setTextColor(ContextCompat.getColor(context,R.color.blue_color));
((StudentViewHolder) holder).mWednesdayButton.setTextColor(ContextCompat.getColor(context,R.color.blue_color));
((StudentViewHolder) holder).mTuesdayButton.setTextColor(ContextCompat.getColor(context,R.color.blue_color));
}*/
} else {
((ProgressViewHolder) holder).progressBar.setIndeterminate(true);
}
}
public void setLoaded() {
loading = false;
}
@Override
public int getItemCount() {
return mSavedTime.size();
}
public void setOnLoadMoreListener(OnLoadMoreListener onLoadMoreListener) {
this.onLoadMoreListener = onLoadMoreListener;
}
//
public class StudentViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView mTimeDigitText;
public TextView mTimeUnitText;
private SwitchCompat mAlarmButtonPower;
private ImageView mAlarmBellImage;
private TextView mSundayButton;
private TextView mMondayButton;
private TextView mTuesdayButton;
private TextView mWednesdayButton;
private TextView mThursdayButton;
private TextView mFridayButton;
private TextView mSaturdayButton;
public CardView cardView1;
public StudentViewHolder(View v) {
super(v);
mTimeDigitText = (TextView) v.findViewById(R.id.timeDigit);
mTimeUnitText = (TextView) v.findViewById(R.id.timeUnit);
mAlarmBellImage = (ImageView) v.findViewById(R.id.alarmBell);
mAlarmButtonPower = (SwitchCompat) v.findViewById(R.id.alarmPowerSwitch);
mSundayButton = (TextView) v.findViewById(R.id.sunday);
mMondayButton = (TextView) v.findViewById(R.id.monday);
mTuesdayButton = (TextView) v.findViewById(R.id.tuesday);
mWednesdayButton = (TextView) v.findViewById(R.id.wednesday);
mThursdayButton = (TextView) v.findViewById(R.id.thursday);
mFridayButton = (TextView) v.findViewById(R.id.friday);
mSaturdayButton = (TextView) v.findViewById(R.id.saturday);
// mAlarmBellImage.setId(getAdapterPosition()+100);
mAlarmBellImage.setOnClickListener(this);
//mAlarmBellImage.setId(getAdapterPosition()+100);
//v.findViewById(R.id.alarmBell).setOnClickListener(this);
v.findViewById(R.id.sunday).setOnClickListener(this);
v.findViewById(R.id.monday).setOnClickListener(this);
v.findViewById(R.id.tuesday).setOnClickListener(this);
v.findViewById(R.id.wednesday).setOnClickListener(this);
v.findViewById(R.id.thursday).setOnClickListener(this);
v.findViewById(R.id.friday).setOnClickListener(this);
v.findViewById(R.id.saturday).setOnClickListener(this);
Log.d(TAG, "StudentViewHolder: "+mDaysArrayList);
mAlarmButtonPower.setChecked(true);
if (mAlarmButtonPower.isChecked()) {
mBellCount=2;
mAlarmBellImage.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.bell_touch));
Toast.makeText(context, "Alarm Activated!!", Toast.LENGTH_SHORT).show();
}else{
mAlarmBellImage.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.bell));
Toast.makeText(context, "First Activate Your Alarm", Toast.LENGTH_SHORT).show();
}
mAlarmButtonPower.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch (buttonView.getId()) {
case R.id.alarmPowerSwitch:
if (!isChecked) {
mAlarmBellImage.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.bell));
Toast.makeText(context, "Alarm is off!!", Toast.LENGTH_SHORT).show();
} else {
// AlarmReceiver.stopRingtone();
// alarmManager.cancel(pendingIntent);
Log.d("MyActivity", "Alarm Off");
mAlarmBellImage.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.bell_touch));
Toast.makeText(context, "Alarm is on!!", Toast.LENGTH_SHORT).show();
}
break;
default:
break;
}
}
});
}
}
}
public static class ProgressViewHolder extends RecyclerView.ViewHolder {
public ProgressBar progressBar;
public ProgressViewHolder(View v) {
super(v);
progressBar = (ProgressBar) v.findViewById(R.id.progressBar1);
}
}
}
答案 0 :(得分:0)
在为卡片设置自定义适配器值时,您可以解析JSON数组值,并使用if else条件语句设置TextViews的所需颜色。
要获取您需要的特定代码,您需要共享解析JSON数据的方式和位置以及卡视图的适配器类的代码。
答案 1 :(得分:0)
您可以使用以下代码更改任何textview的背景:
txtView.setBackgroundColor(Color.parseColor("#AA3456"));
如果您想在代码工作后的工作日添加支票:
首先在列表或数组中解析你的日子,
搜索您的日期列表是否包含如下日期编号:
if(Arrays.asList(days).contains(“1”)){ ... }
重置所有textview的背景颜色并更改特定日期textview的颜色(如果您的列表包含那一天,如下所示:
//重置颜色 txtViewSunday.setBackgroundColor(Color.TRANSPARENT); txtViewMonday.setBackgroundColor(Color.TRANSPARENT); txtViewTuesday.setBackgroundColor(Color.TRANSPARENT); txtViewWednesday.setBackgroundColor(Color.TRANSPARENT); txtViewThursday.setBackgroundColor(Color.TRANSPARENT); txtViewFriday.setBackgroundColor(Color.TRANSPARENT); txtViewSaturday.setBackgroundColor(Color.TRANSPARENT);
// Update color
if (Arrays.asList(days).contains("1")){
txtViewSunday.setBackgroundColor(Color.parseColor("#AA3456"));
}else if (Arrays.asList(days).contains("2")){
txtViewMonday.setBackgroundColor(Color.parseColor("#AA3456"));
}else if (Arrays.asList(days).contains("3")){
txtViewTuesday.setBackgroundColor(Color.parseColor("#AA3456"));
}else if (Arrays.asList(days).contains("4")){
txtViewWednesday.setBackgroundColor(Color.parseColor("#AA3456"));
}else if (Arrays.asList(days).contains("5")){
txtViewThursday.setBackgroundColor(Color.parseColor("#AA3456"));
}else if (Arrays.asList(days).contains("6")){
txtViewFriday.setBackgroundColor(Color.parseColor("#AA3456"));
}else if (Arrays.asList(days).contains("7")){
txtViewSaturday.setBackgroundColor(Color.parseColor("#AA3456"));
}
您必须在适配器中编写此代码。