Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/188.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用RecycleViewAdapter的变量_Java_Android - Fatal编程技术网

Java 使用RecycleViewAdapter的变量

Java 使用RecycleViewAdapter的变量,java,android,Java,Android,我正在应用程序中使用RecycleView,我想在RecycleViewAdapter类中创建一个计数器,并在MainActivity中获取其值 我设置了2个公共变量,并使用调试器查看计数器值是否发生变化,它是否正确计数。 问题是,当我检查MainActivity中计数器的值时,该值为0,看起来它根本不算,或者RecycleViewAdapter的实例再次初始化,但它没有 希望你能帮我弄明白,这真的很奇怪。 这是我的密码: public class RecycleViewAdapter exte

我正在应用程序中使用RecycleView,我想在RecycleViewAdapter类中创建一个计数器,并在MainActivity中获取其值

我设置了2个公共变量,并使用调试器查看计数器值是否发生变化,它是否正确计数。 问题是,当我检查MainActivity中计数器的值时,该值为0,看起来它根本不算,或者RecycleViewAdapter的实例再次初始化,但它没有

希望你能帮我弄明白,这真的很奇怪。 这是我的密码:

public class RecycleViewAdapter extends RecyclerView.Adapter<RecyclerViewHolders> {

    private List<User> userList;
    private Context context;
    public int goodCount;
    static public int badCount; 

    public RecycleViewAdapter(Context context, List<User> userList)
    {
        this.userList = userList;
        this.context = context;
    }

    public RecyclerViewHolders onCreateViewHolder(ViewGroup parent, int viewType) {

        View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, null);
        RecyclerViewHolders rcv = new RecyclerViewHolders(layoutView);
        return rcv;
    }

    @Override
    public void onBindViewHolder(RecyclerViewHolders holder, int position) {
        holder.tvName.setText(userList.get(position).name);
        if(userList.get(position).result.equals("good")) {
            holder.itemView.setBackgroundColor(ContextCompat.getColor(context, R.color.good));
            goodCount++;
        }
        else if(userList.get(position).result.equals("bad")){
            holder.itemView.setBackgroundColor(ContextCompat.getColor(context, R.color.bad));
            badCount++;
        } 
    }

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

您正在更改适配器的onBindViewHolder方法中的goodCount和badCount的值。但是,在onResponse方法中,您正在创建适配器,将其分配给RecyclerView,然后立即调用int num=recAdapter.goodCount


适配器此时尚未布局或绑定其任何视图,因此onBindViewHolder尚未被调用,计数器将为零。

您正在立即调用count,而recylerview尚未调用。使用此代码获取count。我已测试,它可以正常工作

 recyclerView.post(new Runnable() {
            @Override
            public void run() {
             int num = recAdapter.goodCount;  
            }
        });
RecycleViewAdapter的实例再次初始化,但未初始化

嗯,是的

// Sets the value back to zero
recAdapter = new RecycleViewAdapter(MainActivity.this ,userList);
如果你想要相同的适配器,你需要这样做

final Response.Listener<String> onPostsLoaded = new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
        dialog.dismiss();
        mainIcon.setVisibility(View.GONE);

        // Don't recreate the adapter, just update the data
        userList.clear();
        userList.addAll(Arrays.asList(gson.fromJson(response, User[].class)));
        recAdapter.notifyDataSetChanged();

        int num = recAdapter.goodCount;

        Snackbar.make(findViewById(android.R.id.content), String.valueOf(num), Snackbar.LENGTH_LONG)
                .show();
    }

那么您多久调用一次这个recAdapter=new RecycleViewAdapterMainActivity.this,userList;线这将创建适配器的新实例,即内存中的新对象,所有字段都设置为初始值。在GC清理之前,旧对象仍然是它们的对象。如果您有多个Adatper实例,recAdapter将只指向一个实例。如果您需要每个项目的计数,则需要将该变量放在ViewHolder中,而不是适配器中
final Response.Listener<String> onPostsLoaded = new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
        dialog.dismiss();
        mainIcon.setVisibility(View.GONE);

        // Don't recreate the adapter, just update the data
        userList.clear();
        userList.addAll(Arrays.asList(gson.fromJson(response, User[].class)));
        recAdapter.notifyDataSetChanged();

        int num = recAdapter.goodCount;

        Snackbar.make(findViewById(android.R.id.content), String.valueOf(num), Snackbar.LENGTH_LONG)
                .show();
    }
public int getGoodCount() {
    int count = 0;
    for (User u : this.userList) {
        if (u.result.equals("good")) count += 1;
    }
    return count;
}

// Assuming there is only good and bad
public int getBadCount() {
    return getItemCount() - getGoodCount();
}