通过holder和RecyclerView更改文本视图中显示的数据



请帮助我解决这个问题,因为我目前正在开发一个在特定时间和日期执行任务的应用程序,当我以数字的形式在数据库中保存实施的日期时,当在应用程序中对其进行审查时,我希望显示基于条件的日期名称,而不是这些数字,该条件取决于其编号(如果返回数字1,则应显示当天的名称,而非此编号)请帮助我实现这件事,因为我试图将值分配给一个变量,但当天的名称没有出现在(RecyclerView)中。

这是";CustomAdapter";代码

public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.MyViewHolder> {
private Context context;
private Activity activity;
private ArrayList Id, Active, Type_Id, DateCreated , Event_Id , Hour , Minute, SAT ,
SUN , MON , TUS  , WED , THU , FRI , Execute_Id , DateModifed ;
String SAT_Id = "0";
//String SAT_name = "0" ;
CustomAdapter(Activity activity, Context context, ArrayList Id, ArrayList Active,
ArrayList Type_Id, ArrayList DateCreated , ArrayList Event_Id, ArrayList Hour,
ArrayList Minute, ArrayList SAT ,ArrayList SUN, ArrayList MON, ArrayList TUS ,
ArrayList WED, ArrayList THU, ArrayList FRI , ArrayList Execute_Id , ArrayList DateModifed){
this.activity = activity;
this.context = context;
this.Id = Id;
this.Active = Active;
this.Type_Id = Type_Id;
this.DateCreated = DateCreated;
this.Event_Id = Event_Id;
this.Hour = Hour;
this.Minute = Minute;
this.SAT = SAT;
this.SUN = SUN;
this.MON = MON;
this.TUS = TUS;
this.WED = WED;
this.THU = THU;
this.FRI = FRI;
this.Execute_Id = Execute_Id;
this.DateModifed = DateModifed;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.db_times_my_rows, parent, false);
return new MyViewHolder(view);
}
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position) {
String Exec_stat_Id = String.valueOf(Event_Id.get(position));
String Exec_stat_name = "" ;
if(String.valueOf(Event_Id.get(position)) == "-1")
{
Exec_stat_name = "Not" ;
}
else if(String.valueOf(Event_Id.get(position)) == "2")
{
Exec_stat_name = "Video" ;
}
SAT_Id = String.valueOf(SAT.get(position));
String SAT_name = "-1" ;
if(SAT_Id == "0")
{
SAT_name = " " ;
}
else if(SAT_Id == "1")
{
SAT_name = "SAT" ;
}

holder.Id_txt.setText(String.valueOf(Id.get(position)));
//holder.name_txt.setText("Name: " + Exec_stat_name);
holder.name_txt.setText(Exec_stat_name);
holder.Time_txt.setText(String.valueOf(Hour.get(position)) + " : " + String.valueOf(Minute.get(position)));
//holder.SAT_txt.setText(String.valueOf(SAT.get(position)));
holder.SAT_txt.setText(SAT_name);
//MyViewHolder.notifyDataSetChanged();
holder.SUN_txt.setText(String.valueOf(SUN.get(position)));
holder.MON_txt.setText(String.valueOf(MON.get(position)));
holder.TUS_txt.setText(String.valueOf(TUS.get(position)));
holder.WED_txt.setText(String.valueOf(WED.get(position)));
holder.THU_txt.setText(String.valueOf(THU.get(position)));
holder.FRI_txt.setText(String.valueOf(FRI.get(position)));
//Recyclerview onClickListener
holder.mainLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(context, Times_Insert_Update.class);
intent.putExtra("Id", String.valueOf(Id.get(position)));
intent.putExtra("Active", String.valueOf(Active.get(position)));
intent.putExtra("Type_Id", String.valueOf(Type_Id.get(position)));
intent.putExtra("DateCreated", String.valueOf(DateCreated.get(position)));
intent.putExtra("Event_Id", String.valueOf(Event_Id.get(position)));
intent.putExtra("Hour", String.valueOf(Hour.get(position)));
intent.putExtra("Minute", String.valueOf(Minute.get(position)));
intent.putExtra("SAT", String.valueOf(SAT.get(position)));
intent.putExtra("SUN", String.valueOf(SUN.get(position)));
intent.putExtra("MON", String.valueOf(MON.get(position)));
intent.putExtra("TUS", String.valueOf(TUS.get(position)));
intent.putExtra("WED", String.valueOf(WED.get(position)));
intent.putExtra("THU", String.valueOf(THU.get(position)));
intent.putExtra("FRI", String.valueOf(FRI.get(position)));
intent.putExtra("Execute_Id", String.valueOf(Execute_Id.get(position)));
intent.putExtra("DateModifed", String.valueOf(DateModifed.get(position)));
activity.startActivityForResult(intent, 1);
}
});
}
@Override
public int getItemCount() {
return Id.size();
}
class MyViewHolder extends RecyclerView.ViewHolder {
LinearLayout mainLayout;
TextView Id_txt, name_txt, Time_txt, SAT_txt , SUN_txt , MON_txt, TUS_txt, WED_txt,
THU_txt, FRI_txt;
MyViewHolder(@NonNull View itemView) {
super(itemView);
Id_txt = itemView.findViewById(R.id.Id_txt);
name_txt = itemView.findViewById(R.id.name_txt);
Time_txt = itemView.findViewById(R.id.Time_txt);
SAT_txt = itemView.findViewById(R.id.SAT_txt);
SUN_txt = itemView.findViewById(R.id.SUN_txt);
MON_txt = itemView.findViewById(R.id.MON_txt);
TUS_txt= itemView.findViewById(R.id.TUS_txt);
WED_txt = itemView.findViewById(R.id.WED_txt);
THU_txt = itemView.findViewById(R.id.THU_txt);
FRI_txt = itemView.findViewById(R.id.FRI_txt);
mainLayout = itemView.findViewById(R.id.mainLayout);
Animation translate_anim = AnimationUtils.loadAnimation(context, R.anim.translate_anim);
mainLayout.setAnimation(translate_anim);
}
}

}

这是";回收活动"代码

public class Times_Recyclerview extends AppCompatActivity {
RecyclerView recyclerView;
Button add_button;
ImageView empty_imageview;
TextView no_data;
CustomAdapter customAdapter;
MyDatabaseHelper myDB;
ArrayList<String> Id, Active, Type_Id, DateCreated , Event_Id , Hour , Minute , SAT ,
SUN , MON , TUS  , WED , THU , FRI , Execute_Id , DateModifed ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.db_times_recyclerview);
recyclerView = findViewById(R.id.recyclerView);
add_button = findViewById(R.id.add_button);
empty_imageview = findViewById(R.id.empty_imageview);
no_data = findViewById(R.id.no_data);
add_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(Times_Recyclerview.this, Times_Insert_Update.class);
startActivity(intent);
}
});
myDB = new MyDatabaseHelper(Times_Recyclerview.this);
Id = new ArrayList<>();
Active = new ArrayList<>();
Type_Id = new ArrayList<>();
DateCreated = new ArrayList<>();
Event_Id = new ArrayList<>();
Hour = new ArrayList<>();
Minute = new ArrayList<>();
SAT = new ArrayList<>();
SUN = new ArrayList<>();
MON = new ArrayList<>();
TUS = new ArrayList<>();
WED = new ArrayList<>();
THU = new ArrayList<>();
FRI = new ArrayList<>();
Execute_Id = new ArrayList<>();
DateModifed = new ArrayList<>();
storeDataInArrays();
customAdapter = new CustomAdapter(Times_Recyclerview.this,this, Id,
Active, Type_Id, DateCreated , Event_Id , Hour , Minute, SAT ,
SUN , MON , TUS  , WED , THU , FRI , Execute_Id , DateModifed);
recyclerView.setAdapter(customAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(Times_Recyclerview.this));
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 1)
{
recreate();
}
}
void storeDataInArrays(){
Cursor cursor = myDB.readAllData();
if(cursor.getCount() == 0)
{
empty_imageview.setVisibility(View.VISIBLE);
no_data.setVisibility(View.VISIBLE);
}
else
{
while (cursor.moveToNext()){
Id.add(cursor.getString(0));
Active.add(cursor.getString(1));
Type_Id.add(cursor.getString(2));
DateCreated.add(cursor.getString(3));
Event_Id.add(cursor.getString(4));
Hour.add(cursor.getString(5));
Minute.add(cursor.getString(6));
SAT.add(cursor.getString(7));
SUN.add(cursor.getString(8));
MON.add(cursor.getString(9));
TUS.add(cursor.getString(10));
WED.add(cursor.getString(11));
THU.add(cursor.getString(12));
FRI.add(cursor.getString(13));
Execute_Id.add(cursor.getString(14));
DateModifed.add(cursor.getString(15));
}
empty_imageview.setVisibility(View.GONE);
no_data.setVisibility(View.GONE);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.times_my_menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == R.id.delete_all){
confirmDialog();
}
return super.onOptionsItemSelected(item);
}
void confirmDialog(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Delete All?");
builder.setMessage("Are you sure you want to delete all Data?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
MyDatabaseHelper myDB = new MyDatabaseHelper(Times_Recyclerview.this);
myDB.deleteAllData();
//Refresh Activity
Intent intent = new Intent(Times_Recyclerview.this, Times_Recyclerview.class);
startActivity(intent);
finish();
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
builder.create().show();
}

}

这是";my_row";XML代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/mainLayout">
<androidx.cardview.widget.CardView
android:id="@+id/cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<TextView
android:id="@+id/Id_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:textColor="#000"
android:textSize="35sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/name_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="25dp"
android:text="Title 1"
android:textColor="#000"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintStart_toEndOf="@+id/Id_txt"
app:layout_constraintTop_toTopOf="@+id/id_txt" />
<TextView
android:id="@+id/Time_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Time"
android:textColor="#000"
android:textSize="20sp"
android:textStyle="bold"
android:layout_marginStart="20dp"
android:layout_marginLeft="16dp"
app:layout_constraintStart_toEndOf="@+id/name_txt"
app:layout_constraintTop_toTopOf="@+id/id_txt" />
<TextView
android:id="@+id/SAT_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SAT"
android:layout_marginStart="25dp"
android:layout_marginLeft="16dp"
app:layout_constraintStart_toEndOf="@+id/Id_txt"
app:layout_constraintTop_toBottomOf="@+id/name_txt" />
<TextView
android:id="@+id/SUN_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SUN"
android:layout_marginStart="5dp"
android:layout_marginLeft="16dp"
app:layout_constraintStart_toEndOf="@+id/SAT_txt"
app:layout_constraintTop_toBottomOf="@+id/name_txt" />
<TextView
android:id="@+id/MON_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MON"
android:layout_marginStart="5dp"
android:layout_marginLeft="16dp"
app:layout_constraintStart_toEndOf="@+id/SUN_txt"
app:layout_constraintTop_toBottomOf="@+id/name_txt" />
<TextView
android:id="@+id/TUS_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TUS"
android:layout_marginStart="5dp"
android:layout_marginLeft="16dp"
app:layout_constraintStart_toEndOf="@+id/MON_txt"
app:layout_constraintTop_toBottomOf="@+id/name_txt" />
<TextView
android:id="@+id/WED_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="WED"
android:layout_marginStart="5dp"
android:layout_marginLeft="16dp"
app:layout_constraintStart_toEndOf="@+id/TUS_txt"
app:layout_constraintTop_toBottomOf="@+id/name_txt" />
<TextView
android:id="@+id/THU_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="THU"
android:layout_marginStart="5dp"
android:layout_marginLeft="16dp"
app:layout_constraintStart_toEndOf="@+id/WED_txt"
app:layout_constraintTop_toBottomOf="@+id/name_txt" />
<TextView
android:id="@+id/FRI_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FRI"
android:layout_marginStart="5dp"
android:layout_marginLeft="16dp"
app:layout_constraintStart_toEndOf="@+id/THU_txt"
app:layout_constraintTop_toBottomOf="@+id/name_txt" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>

这是";RecyclerView"XML代码

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DB.Times_Recyclerview">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="390dp"
android:layout_height="650dp"
android:layout_marginStart="10dp"
android:layout_marginTop="1dp"
android:layout_marginEnd="5dp"
android:background="#FFFFFF"
app:layout_constraintTop_toBottomOf="parent" />
<ImageView
android:id="@+id/empty_imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="230dp"
android:alpha="0.1"
android:visibility="gone"
app:srcCompat="@drawable/ic_empty"
tools:ignore="VectorDrawableCompat" />
<TextView
android:id="@+id/no_data"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:alpha="0.4"
android:text="No Data."
android:textSize="20sp"
android:visibility="gone" />
</LinearLayout>
</ScrollView>
<Button
android:id="@+id/add_button"
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="10dp"
android:background="@drawable/add1"
android:clickable="true"
android:focusable="true"
android:lineSpacingExtra="36sp"
android:src="@drawable/ic_add"
android:text="+"
android:textAlignment="center"
android:textSize="30sp"
android:textStyle="bold"
app:iconPadding="5dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />

<androidx.constraintlayout.widget.constraintlayout>


**更新:**

我找过你,没有找到任何能解决这个问题的解决方案,但在实验之后,我每天都制作了另一个文本视图(在这里调用:SUN_no_txt),这样它就携带了从数据库中检索到的结果的值,然后通过这个文本视图来设置我想要的值,因此,这暂时解决了问题(我知道这是一个不专业的解决方案,但这给了我想要的结果)

holder.SUN_no_txt.setText(String.valueOf(SAT.get(position)));
if(holder.SUN_no_txt.getText().toString().equals("-1") || holder.SUN_no_txt.getText().toString().equals("0"))
{
holder.SUN_txt.setText("");
}
else if(holder.SUN_no_txt.getText().toString().equals("1"))
{
holder.SUN_txt.setText("SUNDAY");
}
holder.SUN_no_txt.setVisibility(View.GONE);  // To hide
public class DateUtil{
private static final List<String> DAYS_IN_WEEK = new ArrayList<>();
static{
DAYS_IN_WEEK.add("SUNDAY");
DAYS_IN_WEEK.add("MONDAY");
DAYS_IN_WEEK.add("TUESDAY");
DAYS_IN_WEEK.add("WEDNESDAY");
DAYS_IN_WEEK.add("THRUSDAY");
DAYS_IN_WEEK.add("FRIDAY");
DAYS_IN_WEEK.add("SATURDAY");
}
public static String getDayByPosition(Integer position){
return (position != null && 
position >= 0 && 
position < DAYS_IN_WEEK.size())?
DAYS_IN_WEEK.get(position) : "INVALID DAY";
}
}

当你需要获得当天的名称,并且你只有职位时,使用这个实用程序类和函数。

在一个不相关的主题中,将信息组合到一个这样的数据模型中会有很多好处

public class Time{
private String id;
private String active;
private String typeId;
private String dateCreated;
private String eventId;
private String hour;
private String minute;
private String saturday;
private String sunday;
private String monday;
private String tuesday;
private String wednesday;
private String thursday;
private String friday;
private String executeId;
private String dateModifed;
public Time(
String id,
String active,
String typeId,
String dateCreated,
String eventId,
String hour,
String minute,
String saturday,
String sunday,
String monday,
String tuesday,
String wednesday,
String thursday,
String friday,
String executeId,
String dateModifed
)
//getters and setters
}

这样,您就可以以更合适的方式传递数据。

List<Times> times = new ArrayList<>();
new CustomAdapter(this,times);

此外,您的所有模型数据成员都是String,通过实现这两个接口中的任何一个,它们都可以很容易地成为Serializable/Parcelable

当你通过Intent时,你所要做的就是

intent.putExtra("time", time);

当你取回

Time time = (Time)intent.getSerializableExtra("time");

OR(取决于Time实现的interface)

Time time = intent.getParcelableExtra("time");

相关内容

  • 没有找到相关文章

最新更新