如何在Android Studio中将RecyclerView中的总项目数显示在TextView中。

huangapple 未分类评论49阅读模式
英文:

How to show total number of recyclerview in a textview in android studio

问题

<RelativeLayout
    android:id="@+id/rlRunning"
    android:background="#00ff0000"
    android:layout_below="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/del_t"
        android:text="mmmm"
        android:textSize="30sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <Button
        android:onClick="clickMe"
        android:id="@+id/btn_t"
        android:layout_below="@+id/del_t"
        android:text="Click me"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <android.support.v7.widget.RecyclerView
        android:layout_below="@+id/btn_t"
        android:id="@+id/recyclerview"
        android:background="#00ff0000"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </android.support.v7.widget.RecyclerView>
</RelativeLayout>

Adapter (适配器):

public class BlogzoneImages {

    // ... (其他代码)

    // 添加getter和setter方法 (getters and setters)
}

在MainActivity.java中获取数据显示在RecyclerView:

// ... (其他代码)

public class MainActivity extends AppCompatActivity {

    // ... (其他代码)

    // 以下是你现有的代码

    int count = 0;

    public void showTotal() {
        RecyclerView recyclerView = findViewById(R.id.recyclerview);
        if (recyclerView.getAdapter() != null) {
            count = recyclerView.getAdapter().getItemCount();
        }
        TextView tvvv = findViewById(R.id.del_t);
        tvvv.setText(String.valueOf(count)); // 将整数转换为字符串
    }

    public void clickMe(View view) {
        showTotal();
    }

    // ... (其他代码)
}

如果你的应用崩溃了,可能是因为你在tvvv.setText(count);中尝试将整数值直接设置为TextView的文本,而setText()需要传递一个字符串。在上述代码中,我已经将整数count转换为字符串,然后将其设置为TextView的文本。这应该解决你的问题。

英文:

I have data on firebase, the data is shown in recyclerview. I have so many recyclerviews showing. Above a recyclerview I have a textview where I want to show the total number of recyclerviews. I have also a button so that when I click on it does the count and show the result in a textview. In summary: If i click a button i want to see the number of recyclerviews in a textview.

Here is my .xml code:

 &lt;RelativeLayout
            android:id=&quot;@+id/rlRunning&quot;
            android:background=&quot;#00ff0000&quot;
            android:layout_below=&quot;@+id/rl&quot;
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;match_parent&quot;&gt;

            &lt;TextView
                android:id=&quot;@+id/del_t&quot;
                android:text=&quot;mmmm&quot;
                android:textSize=&quot;30sp&quot;
                android:layout_width=&quot;wrap_content&quot;
                android:layout_height=&quot;wrap_content&quot; /&gt;
            &lt;Button
                android:onClick=&quot;clickMe&quot;
                android:id=&quot;@+id/btn_t&quot;
                android:layout_below=&quot;@+id/del_t&quot;
                android:text=&quot;Click me&quot;
                android:layout_width=&quot;wrap_content&quot;
                android:layout_height=&quot;wrap_content&quot; /&gt;

            &lt;android.support.v7.widget.RecyclerView
                android:layout_below=&quot;@+id/btn_t&quot;
                android:id=&quot;@+id/recyclerview&quot;
                android:background=&quot;#00ff0000&quot;
                android:layout_width=&quot;match_parent&quot;
                android:layout_height=&quot;match_parent&quot;&gt;
            &lt;/android.support.v7.widget.RecyclerView&gt;
        &lt;/RelativeLayout&gt;

Here is my adapter:

public class BlogzoneImages {

    private String ititle, idate, idesc,imageUrl;

    public BlogzoneImages(String ititle, String idate, String imageUrl, String idesc ) {
        this.idate = idate;
        this.ititle = ititle;
        this.idesc = idesc;
        this.imageUrl=imageUrl;
    }

    public BlogzoneImages() {
    }

    public void setImageUrl(String imageUrl) {
        this.imageUrl = imageUrl;
    }

    public void setiDate(String idate) {
        this.idate = idate;
    }

    public void setiTitle(String ititle) {
        this.ititle = ititle;
    }

    public void setiDesc(String idesc) {
        this.idesc = idesc;
    }

    public String getImageUrl() {
        return imageUrl;
    }

    public String getiDate() {
        return idate;
    }

    public String getiTitle() {
        return ititle;
    }

    public String getiDesc() {
        return idesc;
    }
}

Here is how I am getting the data to display in MainActivity.java

protected void loadImage_setups() {
        mAuth.addAuthStateListener(mAuthListener);
        FirebaseRecyclerAdapter&lt;BlogzoneImages, BlogzoneViewHolder2&gt; FBRA_Images = new FirebaseRecyclerAdapter&lt;BlogzoneImages, BlogzoneViewHolder2&gt;(
                BlogzoneImages.class,
                R.layout.setups_card,
                BlogzoneViewHolder2.class,
                image_Database

        ) {
            @Override
            protected void populateViewHolder(BlogzoneViewHolder2 viewHolder, BlogzoneImages model, int position) {
                final String post_key = getRef(position).getKey().toString();
                viewHolder.setiDate(model.getiDate());
                viewHolder.setiTitle(model.getiTitle());
                viewHolder.setiDesc(model.getiDesc());
                viewHolder.setImageUrl(getApplicationContext(), model.getImageUrl());
                viewHolder.mView2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        //   Intent singleActivity = new Intent(MainActivity.this, SinglePostActivity.class);
                        //   singleActivity.putExtra(&quot;PostID&quot;, post_key);
                        //   startActivity(singleActivity);
                    }
                });
            }
        };
        recyclerView.setAdapter(FBRA_Images);
    }

    public static class BlogzoneViewHolder2 extends RecyclerView.ViewHolder {
        View mView2;

        public BlogzoneViewHolder2(View itemView2) {
            super(itemView2);
            mView2 = itemView2;
        }

        public void setiDate(String idate) {
            TextView post_date = mView2.findViewById(R.id.setup_date);
            post_date.setText(idate);
        }

        public void setiTitle(String ititle) {
            TextView post_title = mView2.findViewById(R.id.setup_title);
            post_title.setText(ititle);
        }

        public void setiDesc(String idesc) {
            TextView post_desc = mView2.findViewById(R.id.setup_desc);
            post_desc.setText(idesc);
        }

        public void setImageUrl(Context ctx, String imageUrl) {
            ImageView post_image = mView2.findViewById(R.id.setup_image);
            Picasso.with(ctx).load(imageUrl).into(post_image);
        }
    }

All this is well, the data is showing nicely on recyclerview.

My worry now is to show the total number of these recyclerviews in a texview when i click the button.

I have tried this in MainActivity.java:

int count = 0;
    public void showTotal() {
        RecyclerView recyclerView = (RecyclerView)findViewById(R.id.recyclerview);
        if (recyclerView.getAdapter() != null) {
            count = recyclerView.getAdapter().getItemCount();
        }
        TextView tvvv = (TextView)findViewById(R.id.del_t);
        tvvv.setText(count);
    }
    public void clickMe(View view) {
        showTotal();
    }

When i click the button, the app crashes. I have also tried this:

public void showTotal() {
        RecyclerView recyclerView = (RecyclerView)findViewById(R.id.recyclerview);
        int itemCount = recyclerView.getAdapter().getItemCount();
        TextView tvvv = (TextView)findViewById(R.id.del_t);
        tvvv.setText(itemCount);
    }
    public void clickMe(View view) {
        showTotal();
    }

When I click the button, the app crashes again. So now I am stuck. I have no idea how to solve this. My question is, how can i show the total number of recyclerviews in a TextView after a button click?

huangapple
  • 本文由 发表于 2020年7月24日 18:52:38
  • 转载请务必保留本文链接:https://java.coder-hub.com/63072124.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定