错误:没有连接适配器;跳过布局

时间:2018-01-12 03:41:29

标签: java android

我知道这个问题已被问过很多次了,但是那里没有解决的问题似乎解决了我的问题。

大多数解决方案建议附加适配器(setadapter()),我已经这样做了。但问题似乎仍然存在。

我还检查了我的listitems是否为空并且它不是

我已将适配器附加到 loadUserAccounts() 功能中。

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_accounts);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        // Swipe to refresh
        swipeRefresh = (SwipeRefreshLayout) findViewById(R.id.userSwipeRefresh);

        //Recycler View
        recyclerView = (RecyclerView) findViewById(R.id.userRecyclerView);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(userAccountsActivity.this));
        listItems = new ArrayList<>();
        recyclerViewUserAdapter = new RecyclerViewUserAdapter(listItems, userAccountsActivity.this);
        recyclerView.setAdapter(recyclerViewUserAdapter);


        //Function to load the users to the recycler view
        loadUserAccounts();

        /*
        * Sets up a SwipeRefreshLayout.OnRefreshListener that is invoked when the user
        * performs a swipe-to-refresh gesture.
        */
        swipeRefresh.setOnRefreshListener(
                new SwipeRefreshLayout.OnRefreshListener() {
                    @Override
                    public void onRefresh() {
                        Log.i("Swipe_log: ", "onRefresh called from SwipeRefreshLayout");

                        // This method performs the actual data-refresh operation.
                        // The method calls setRefreshing(false) when it's finished.
                        loadUserAccounts();
                    }
                }
        );
    }

这是我的 loadUserAccounts() 功能:

   /**
     * Function to load the user account
     * into the recycler view from the log database
     */
   public void loadUserAccounts(){

        String tag_string_req = "req_user_acc";
        clear();

        StringRequest stringRequest = new StringRequest(Request.Method.GET, URL_USER_ACCOUNTS, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {

                    JSONArray jArray = new JSONArray(response);

                    for(int i = jArray.length()-1; i>=0; i--){
                        JSONObject log = jArray.getJSONObject(i);

                        String name = log.getString("name");
                        String cpfNumber = log.getString("cpfNumber");
                        String type = log.getString("type");
                        String created_at = log.getString("created_at");

                        Log.d("Adapter: ", name + cpfNumber + type + created_at);
                        ListItem_RecyclerView_User item = new ListItem_RecyclerView_User (name, cpfNumber, type, created_at);
                        listItems.add(item);
                    }
                    recyclerViewUserAdapter.updateDataList(responseList);

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                if(error!=null && error.getMessage() !=null){
                    Log.e(TAG,"User Accounts Error: " + error.getMessage());
                    Toast.makeText(getApplicationContext(),error.getMessage(), Toast.LENGTH_LONG).show();
                }
                else{
                    Toast.makeText(getApplicationContext(),"Something went wrong",Toast.LENGTH_LONG).show();
                }
            }
        });

        //Adding request to request queue
        AppController.getInstance().addToRequestQueue(stringRequest, tag_string_req);
       if (swipeRefresh == null)
           return;
       else {
           if (swipeRefresh.isRefreshing()) {
               swipeRefresh.setRefreshing(false);
           }
       }
    }

我的适配器类RecyclerViewUserAdapter

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

    private List<ListItem_RecyclerView_User> listItems;
    private Context context;

    public RecyclerViewUserAdapter(List<ListItem_RecyclerView_User> listItems, Context context) {
        this.listItems = listItems;
        this.context = context;
    }

    public void updateDataList(List<ListItem_RecyclerView_User> newDatas) {
        listItems.clear();
        listItems.addAll(newDatas);
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.users_recycler_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerViewUserAdapter.ViewHolder holder, int position) {
        ListItem_RecyclerView_User listItem = listItems.get(position);

        holder.txtUserName.setText(listItem.getTxtUserName());
        holder.txtUserCpf.setText(listItem.getTxtUserCpf());
        holder.txtAccountType.setText(listItem.getTxtAccountType());


        try {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = null;
            date = df.parse(listItem.getTxtCreatedDate());
            SimpleDateFormat sdf = new SimpleDateFormat("h:mm a");
            String loginTime = sdf.format(date);

            holder.txtCreatedDate.setText(loginTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    @Override
    public int getItemCount() {
        return 0;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private TextView txtUserName;
        private TextView txtUserCpf;
        private TextView txtAccountType;
        private TextView txtCreatedDate;

        public ViewHolder(View itemView) {
            super(itemView);
            txtUserName = (TextView) itemView.findViewById(R.id.txtUserName);
            txtUserCpf = (TextView) itemView.findViewById(R.id.txtUserCpf);
            txtAccountType = (TextView) itemView.findViewById(R.id.txtAccountType);
            txtCreatedDate = (TextView) itemView.findViewById(R.id.txtCreatedDate);
        }
    }
}

请帮我解决这个问题。 提前谢谢。

2 个答案:

答案 0 :(得分:1)

试试这个

  1. 使用空列表数据创建适配器

    complete
  2. 在您的回复中

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState)
         // your normal code
    
        recyclerView = (RecyclerView) findViewById(R.id.userRecyclerView);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(userAccountsActivity.this));
        listItems = new ArrayList<>();
    
        // create new adapter 
        recyclerViewUserAdapter = new RecyclerViewUserAdapter(new ArrayList<>(), userAccountsActivity.this);
        recyclerView.setAdapter(recyclerViewUserAdapter);
    
        //your normal code
    }
    
  3. 在适配器中创建方法

    @Override
    public void onResponse(String response) {
            try {
                List<ListItem_RecyclerView_User> responseList = new ArrayList<>();
    
                JSONArray jArray = new JSONArray(response);
    
                for(int i = jArray.length()-1; i>=0; i--){
                    JSONObject log = jArray.getJSONObject(i);
    
                    String name = log.getString("name");
                    String cpfNumber = log.getString("cpfNumber");
                    String type = log.getString("type");
                    String created_at = log.getString("created_at");
    
                    Log.d("Adapter: ", name + cpfNumber + type + created_at);
                    ListItem_RecyclerView_User item = new ListItem_RecyclerView_User (name, cpfNumber, type, created_at);
                    responseList.add(item);
                }
                /*recyclerViewUserAdapter = new RecyclerViewUserAdapter(listItems, userAccountsActivity.this);
                recyclerView.setAdapter(recyclerViewUserAdapter);*/
    
                // write method to update your data
                recyclerViewUserAdapter.updateDataList(responseList);
    
    
            } catch (JSONException e) {
                e.printStackTrace();
            }
    
    }
    

答案 1 :(得分:1)

试试这个......

getItemCount()修改为喜欢这个。

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

快乐的编码......