我希望像我有三种布局一样进行聊天: MainActivity:我有一个recyclerview 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();
}
}
答案 0 :(得分:2)
首先,由于视图的位置已经通过,因此不需要数组中的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
实例作为参数传递,因此您需要在运行时检查其类型以了解要绑定的布局(使用{{ 1}} operator)。
instanceof
希望这有帮助!
答案 1 :(得分:0)
覆盖适配器中的getItemViewType()。然后根据onCreateViewHolder返回视图中的viewtype参数。
I hope this below link help you for multi layout in recycleview adapter :