gpt4 book ai didi

android - recyclerview 中的多种布局

转载 作者:行者123 更新时间:2023-11-29 06:49:33 25 4
gpt4 key购买 nike

我想制作一个类似聊天的表示,其中我有三种布局:MainActivity:我有一个回收 View layout_sent:这是发送消息的布局layout_received:这是接收消息的布局

我正在使用 mySQL 数据库:获取数据的查询是:

Select cr.user_id, cr.reply, d.griev_desc, c.user_one, c.user_two from grievance_details d , conversation_reply cr, conversation c where d.complain_id = 11 and cr.complain_id = d.complain_id and c.complain_id = 11

这给了我输出:

我的 Android 适配器代码是:

public class DifferentRowAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

String user_one;
String user_two;
ArrayList<String> user_id_array;
ArrayList<String> griev_desc_array;
ArrayList<String> reply_array_emp;
ArrayList<String> reply_array_hr;
Context c;

public DifferentRowAdapter(Context c, String user_one, String user_two,
ArrayList<String> user_id_array, ArrayList<String> griev_desc_array,
ArrayList<String> reply_array_emp, ArrayList<String> reply_array_hr) {
this.c =c ;
this.user_id_array = user_id_array;
this.user_one = user_one;
this.user_two = user_two;
this.griev_desc_array = griev_desc_array;
this.reply_array_emp = reply_array_emp;
this.reply_array_hr = reply_array_hr;
}


@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;

System.out.println("top of for onCreateView");
//for(String s : user_id_array)
for(int i =0;i<user_id_array.size();i++)
{
System.out.println("in side for");
if(user_id_array.get(i).equals(user_one)){
//if(!s.equals(user_one)){
//System.out.println("in side if onCreateViewHolder"+s);
System.out.println("in side if onCreateViewHolder"+user_id_array.get(i));
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.model_emp_conv, parent, false);
return new EmployeeViewHolder(view);
}else if(user_id_array.get(i).equals(user_two)){
//System.out.println("in side else if onCreateViewHolder"+s);
System.out.println("in side if onCreateViewHolder"+user_id_array.get(i));
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.model_hr_conv, parent, false);
return new HrViewHolder(view);
}
else
{
//System.out.println("in side else onCreateViewHolder"+s);
System.out.println("in side if onCreateViewHolder"+user_id_array.get(i));
}
}
return null;
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

System.out.println("top of for");
//for(String s : user_id_array)
for(int i =0;i<user_id_array.size();i++)
{
System.out.println("in side for");
if(user_id_array.get(i).equals(user_one)){
System.out.println("in side if onBindViewHolder"+ user_id_array.get(i));
//System.out.println("in side if onBindViewHolder"+s);
//holder.employee_conversation.setText(reply_array_emp.get(position));
((EmployeeViewHolder)holder).employee_conversation.setText(reply_array_emp.get(position));

}else if(user_id_array.get(i).equals(user_two)){
//System.out.println("in side else if onBindViewHolder"+s);
System.out.println("in side if onBindViewHolder"+user_id_array.get(i));
((HrViewHolder) holder).hr_conversation.setText(reply_array_hr.get(position));
}
else
{
//System.out.println("in side else onBindViewHolder"+s);
}
}

}



@Override
public int getItemCount() {
return user_id_array.size();
}

}

但是,我仍然无法膨胀这两个布局,因为我收到错误:

((HrViewHolder) holder).hr_conversation.setText(reply_array_hr.get(position));

说 TypeCast 不可能。

另外两个 Holder 类是:

public class HrViewHolder extends RecyclerView.ViewHolder {
TextView hr_conversation;

public HrViewHolder(View itemView) {
super(itemView);
hr_conversation =(TextView) itemView.findViewById(R.id.hr_conversation);
System.out.println("in hrViewHolder");
}

}

public class EmployeeViewHolder extends RecyclerView.ViewHolder {

TextView employee_conversation;

public EmployeeViewHolder(View itemView) {
super(itemView);
employee_conversation=(TextView) itemView.findViewById(R.id.employee_conversation);
System.out.println("in employeeViewHolder");
}

}

我将代码修改为:

public class DifferentRowAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

String user_one;
String user_two;
ArrayList<String> user_id_array;
ArrayList<String> griev_desc_array;
ArrayList<String> reply_array_emp;
ArrayList<String> reply_array_hr;
Context c;

public DifferentRowAdapter(Context c, String user_one, String user_two,
ArrayList<String> user_id_array, ArrayList<String> griev_desc_array,
ArrayList<String> reply_array_emp, ArrayList<String> reply_array_hr) {
this.c =c ;
this.user_id_array = user_id_array;
this.user_one = user_one;
this.user_two = user_two;
this.griev_desc_array = griev_desc_array;
this.reply_array_emp = reply_array_emp;
this.reply_array_hr = reply_array_hr;
}

@Override
public int getItemViewType(int position) {
for(String s: user_id_array)
{
if(s.equals(user_one))
{
return Integer.parseInt(user_one);
}
else if(s.equals(user_two))
return Integer.parseInt(user_two);
else
return 3;
}

return 0;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;

System.out.println("top of for onCreateView");

switch (viewType)
{
case 1 :
System.out.println("in side case 1 onCreateViewHolder viewType "+ viewType);
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.model_emp_conv, parent, false);
return new EmployeeViewHolder(view);
case 2:
System.out.println("in side case 2 onCreateViewHolder viewType "+ viewType);
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.model_hr_conv, parent, false);
return new HrViewHolder(view);


}
return null;
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

System.out.println("top of for");
//for(String s : user_id_array)
for(int i =0;i<user_id_array.size();i++)
{
System.out.println("in side for");
if(user_id_array.get(i).equals(user_one)){
System.out.println("in side if onBindViewHolder"+ user_id_array.get(i));
//System.out.println("in side if onBindViewHolder"+s);
//holder.employee_conversation.setText(reply_array_emp.get(position));
//((EmployeeViewHolder)holder).employee_conversation.setText(reply_array_emp.get(position));

}else if(user_id_array.get(i).equals(user_two)){
//System.out.println("in side else if onBindViewHolder"+s);
System.out.println("in side if onBindViewHolder"+user_id_array.get(i));
//((HrViewHolder) holder).hr_conversation.setText(reply_array_hr.get(position));
}
else
{
//System.out.println("in side else onBindViewHolder"+s);
}
}

}



@Override
public int getItemCount() {
return user_id_array.size();
}

}

最佳答案

首先,由于传递了 View 的位置,因此数组中不需要 for 循环,因此您可以安全地将其修改为以下内容

@Override
public int getItemViewType(int position) {
if(user_id_array.get(position).equals(user_one))
return 1;
else if(user_id_array.get(position).equals(user_two))
return 2;
else
return 3;
}

这同样适用于 onBindViewHolder,您将 ViewHolder 实例作为参数传递,因此您需要在运行时检查其类型以了解要绑定(bind)的布局到(使用 instanceof 运算符)。

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

if (holder instanceof EmployeeViewHolder) {
System.out.println("in side for");
System.out.println("in side if onBindViewHolder"+ user_id_array.get(position));
//System.out.println("in side if onBindViewHolder"+s);
//holder.employee_conversation.setText(reply_array_emp.get(position));
((EmployeeViewHolder)holder).employee_conversation.setText(reply_array_emp.get(position));

} else if(holder instanceof HrViewHolder){
//System.out.println("in side else if onBindViewHolder"+s);
System.out.println("in side if onBindViewHolder"+user_id_array.get(position));
((HrViewHolder) holder).hr_conversation.setText(reply_array_hr.get(position));
} else {
//System.out.println("in side else onBindViewHolder"+s);
}
}

希望这有帮助!

关于android - recyclerview 中的多种布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48048688/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com