【问题标题】:Transferring data into a separate ArrayList and displaying in RecyclerView将数据传输到单独的 ArrayList 并在 RecyclerView 中显示
【发布时间】:2017-11-08 11:25:30
【问题描述】:

我有两个 ArrayLists,我试图从中将数据插入到另一个类中的单独静态 ArrayList 并显示在 RecyclerView 中,但回收器并未通过使用虚拟 ArrayList 也是如此。
请帮我解决这个问题。

我的班级,我在两个单独的 ArrayList 中插入来自 phoneContactNos 和 phoneContactName 的数据:Common.selectedContactNos 和 Common.selectedContactName。

    public void displayMatchedContacts()
{
    try {
        for (int i = 1; i < phoneContactNos.size(); i++) {
            if (phoneContactNos.contains(registeredContactNos.get(i))) {
                if (registeredContactNos.get(i) != null) {
                    try {
                        indexOfRegNumber = phoneContactNos.indexOf(registeredContactNos.get(i));
                        //Common.indexPosition_contacts=indexOfRegNumber;
                        Toast.makeText(this, "index" + String.valueOf(indexOfRegNumber), Toast.LENGTH_LONG).show();
                        if ((phoneContactNos.get(indexOfRegNumber) != null) &&(phoneContactName.get(indexOfRegNumber) != null)) {

                            //String regName="";
                            //String regContact="";
                            Common.selectedContactNos.add(phoneContactNos.get(indexOfRegNumber));
                            //Toast.makeText(this,selectedContactNos.get(i).toString(),Toast.LENGTH_SHORT).show();
                            //Toast.makeText(this,phoneContactNos.get(indexOfRegNumber).toString(),Toast.LENGTH_SHORT).show();
                            Common.selectedContactName.add(phoneContactName.get(indexOfRegNumber));
                            //Toast.makeText(this,selectedContactName.get(i).toString(),Toast.LENGTH_SHORT).show();
                            //Toast.makeText(this, phoneContactName.get(indexOfRegNumber).toString(), Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(this, "null index no", Toast.LENGTH_SHORT).show();
                        }
                    } catch (Exception e) {
                        Log.i("Contacts display error", e.getLocalizedMessage());
                        e.printStackTrace();
                    }

                }
            }

        }



    }catch (Exception e)
    {
        Log.i("Contacts error in loop", e.getLocalizedMessage());
        e.printStackTrace();
    }


}

我的普通班

public final  class Common {
public static ArrayList<String> selectedContactNos=new ArrayList<>();
public static ArrayList<String> selectedContactName=new ArrayList<>();
public  static String fcmId="";
public static int position;
public  static String contacts_list="";
public static int indexPosition_contacts;

}

我的 RecyclerView 填充代码

   public void populateList() {
  Log.i("Populate List","Entered");
    LinearLayoutManager mLinearLayoutManager = new LinearLayoutManager(this);
    recyclerView_contacts.setLayoutManager(mLinearLayoutManager);
    displayRecyclerAdapter = new DisplayRecyclerAdapter(this);
    recyclerView_contacts.setAdapter(displayRecyclerAdapter);
}

我的适配器类

public class DisplayRecyclerAdapter extends RecyclerView.Adapter<DisplayRecyclerAdapter.displayViewHolder> {
private LayoutInflater mInflater;
private Context context;
Fragment fragment;
FragmentTransaction ft;
FrameLayout container;
public DisplayContacts displayContacts;



public DisplayRecyclerAdapter(Context context) {
    this.mInflater = LayoutInflater.from(context);
    this.context = context;
    ft = ((AppCompatActivity) context).getSupportFragmentManager().beginTransaction();
    //displayContacts = new DisplayContacts();
}

@Override
public displayViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = mInflater.inflate(R.layout.contacts_row, parent, false);
    displayViewHolder holder = new displayViewHolder(view);
    return holder;
}

@Override
public void onBindViewHolder(displayViewHolder holder, int position) {
    holder.setData(position);
    //Common.position = position;
    holder.setListeners();
    //for(int i=0;i<((DisplayContacts)context).selectedContactName.size();i++)
        for(int i=0;i<Common.selectedContactName.size();i++)
    {
        //String contactName=((DisplayContacts)context).selectedContactName.get(i);
        String contactName=Common.selectedContactName.get(i);
        //String contactNumber=((DisplayContacts)context).selectedContactNos.get(i);
        String contactNumber=Common.selectedContactNos.get(i);
        Toast.makeText(context,contactName+","+contactNumber,Toast.LENGTH_SHORT).show();
    }
}


class displayViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    int position;
    //ImageView productSearchImg;
    TextView name_contactList;
    Button call_contact;

    public displayViewHolder(View itemView) {
        super(itemView);

        name_contactList = (TextView) itemView.findViewById(R.id.contactlist_name);
        call_contact = (Button) itemView.findViewById(R.id.contactlist_call);

    }

    public void setData(int position) {
        this.position = position;
        //String displayContacts=((DisplayContacts) context).selectedContactName.get(position);
        String displayContacts=Common.selectedContactName.get(position);
        Toast.makeText(context,"name to display"+ displayContacts,Toast.LENGTH_SHORT).show();
        //name_contactList.setText(((DisplayContacts) context).selectedContactName.get(position));
        //name_contactList.setText(displayContacts);
        name_contactList.setText("dummy text");
    }

    @Override
    public void onClick(View v) {
        //sendPushNotification();
        startAudioCall();
    }


    public void setListeners() {
        call_contact.setOnClickListener(displayViewHolder.this);
    }
}

public void startAudioCall() {
    Intent i = new Intent(context, AudioCallActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(i);
}

@Override
public int getItemCount() {
    return ((DisplayContacts) context).selectedContactName.size();
}

}

【问题讨论】:

  • 您确定在您的适配器类中使用更新的列表吗?发布适配器类
  • 我已经用我的适配器类更新了我的答案。我尝试使用之前显示的虚拟文本,但现在即使没有显示。数据正在根据调试器插入到 Common 类 Arraylist 中跨度>

标签: android arraylist android-recyclerview


【解决方案1】:

可以是以下两种情况之一: 1.你没有用数据附加适配器,因此为空 2.或者你正在更新你的数据但没有调用notifyDataSetChanged()

在您创建它时尝试在您的适配器类中传递列表数据,然后将其附加到recyclerview。

    //to give a vary basic example

        List dataList;
        RvAdapter(Context c,List data){
    this.dataList=data;
        }

        onBidViewHolder(Viewholder holder,int position){
        holder.tvName.setText(dataList.get(position).getName());
    .......
        }

    //in your activity/fragment

    RvAdapter adapter=new RavAdapter(context,dataList);
    recylerview.setAdapter(adapter);
//if you change your data 
adapter.notifyDataSetChanged()

【讨论】:

  • 你能写出包含这一行的完整代码来填充适配器类吗
  • 检查您是否正确初始化了适配器,代码 avobe 也是一个不同的模糊示例,您应该根据需要更改/修改。
  • 这个链接说我应该在 onCreate 上设置适配器,但我首先必须获取数据然后填充适配器 stackoverflow.com/questions/43730887/…
猜你喜欢
  • 1970-01-01
  • 2016-08-24
  • 1970-01-01
  • 1970-01-01
  • 2018-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多