本文介绍了使用片段在recyclerView中的android自定义cardview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是我的应用程序每次运行都会崩溃..这里有问题吗?我正在尝试在片段活动中初始化的recyclerView中实现cardviews.

这是我的代码.

这是片段布局

home.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container2"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<android.support.design.widget.FloatingActionButton
    android:id="@+id/floatingActionButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:clickable="true"
    app:backgroundTint="@color/colorPrimary"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_marginRight="8dp"
    android:layout_marginBottom="8dp"
    app:layout_constraintVertical_bias="1.0"
    app:srcCompat="@drawable/ic_home_black_24dp" />

 <android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true" />
</android.support.constraint.ConstraintLayout>

这是Cardview布局,其中包含大量文本

Here is the cardview layout with a bunch of texts

item_list.xml

item_list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/linear"
    android:layout_margin="8dp"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

<android.support.v7.widget.CardView
    android:id="@+id/cardView"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:id="@+id/linearListTitle"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:id="@+id/linearList1"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <TextView
                android:id="@+id/tv_title"
                android:textSize="20dp"
                android:text="@string/app_name"
                android:layout_marginLeft="8dp"
                android:paddingTop="5dp"
                android:paddingBottom="8dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
            <LinearLayout
                android:id="@+id/linearList2"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/tv_date"
                    android:layout_width="212dp"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="8dp"
                    android:text="23/04/2018" />
                <TextView
                    android:id="@+id/tv_number"
                    android:layout_width="wrap_content"
                    android:text="14/15"
                    android:textColor="@android:color/holo_green_dark"
                    android:layout_marginLeft="8dp"
                    android:layout_height="wrap_content" />

            </LinearLayout>
        </LinearLayout>

        <LinearLayout
            android:id="@+id/linearList3"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/tv_currency"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:text="$"
                android:textColor="@android:color/holo_green_dark"
                android:textSize="60dp" />

            <TextView
                android:id="@+id/tv_budget"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:gravity="center_vertical"
                android:text="170"
                android:textColor="@android:color/holo_green_dark"
                android:textSize="30dp" />
            </LinearLayout>
        </LinearLayout>
    </android.support.v7.widget.CardView>
</LinearLayout>

在这里,是我有二传手和二传手的班级.

while here, is the class where i have the setters and the getters.

Items.java

Items.java

public class Items {
    private String currency;
    private String title;
    private int number;
    private int date;
    private int budget;
        public Items(){
        }
    public Items(String currency, String title,int number, int date, int

    budget){
    this.currency = currency;
    this.title = title;
    this.number = number;
    this.date = date;
    this.budget = budget;
}
public String getTitle(){
    return title;
}
public void setTitle(String title){
    this.title = title;
}
public String getCurrency(){
    return currency;
}
public void setCurrency(String currency){
    this.currency = currency;
}
public int getNumber(){
    return number;
}
public void setNumber(int number){
    this.number = number;
}
public int getDate(){
    return date;
}
public void setDate(int date){
    this.date = date;
}
public int getBudget(){
    return budget;
}
public void setBudget(int budget){
    this.budget = budget;
    }


}

下面是您所看到的上一类的适配器!

Below is the adapter of the above class as you can see!

ItemsAdapter.java

ItemsAdapter.java

import android.content.ClipData;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;

public class ItemsAdapter extends
RecyclerView.Adapter<ItemsAdapter.myViewHolder>{
private Context mContext;
private List<Items> itemsList;

public class myViewHolder extends RecyclerView.ViewHolder {
    TextView title, currency, date, budget, number;

    public myViewHolder(View itemView) {
        super(itemView);
        title = (TextView) itemView.findViewById(R.id.tv_title);
        currency = (TextView) itemView.findViewById(R.id.tv_currency);
        date = (TextView) itemView.findViewById(R.id.tv_date);
        budget = (TextView) itemView.findViewById(R.id.tv_budget);
        number = (TextView) itemView.findViewById(R.id.tv_number);
    }
}

public ItemsAdapter(Context mContext, List<Items>itemsList){
    this.mContext = mContext;
    this.itemsList = itemsList;
}

@Override
public myViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view  =

LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent,
false);

    return new myViewHolder(view);
}

@Override
public void onBindViewHolder(final myViewHolder holder, int position) {
    Items items = itemsList.get(position);
    holder.title.setText(items.getTitle());
    holder.currency.setText(items.getCurrency());
    holder.number.setText(items.getNumber());
    holder.budget.setText(items.getBudget());
    holder.date.setText(items.getDate());

}

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

下面是片段类,我希望在调用此类时显示卡片视图.

While here below is the fragment class where i want the cardviews to be shown when this class is invoked.

Home.java

Home.java

public class Home extends Fragment {
    private RecyclerView rv;
    private ItemsAdapter adapter;
    private List<Items> itemsList;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.home, container, false);
    rv = (RecyclerView) rootView.findViewById(R.id.recyclerView);
    itemsList = new ArrayList<>();
    adapter = new ItemsAdapter(getActivity(),itemsList);
    RecyclerView.LayoutManager lm = new LinearLayoutManager(getActivity());
    rv.setAdapter(adapter);
    rv.setLayoutManager(lm);

return rootView;
}

private void populate() {
    Items a = new Items("$","this",7,8,190);
    itemsList.add(a);
    Items b = new Items("$","thi",7,8,90);
    itemsList.add(b);
    Items c = new Items("$","thi",7,8,99);
    itemsList.add(c);

    adapter.notifyDataSetChanged();

}

public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    populate();
}

任何有经验的人的任何帮助/解决方案/贡献,都将受到高度赞赏.谢谢!

Any help/solution/contribution by any of you experienced guys will be highly appreciated any helpful. Thanks!

崩溃日志

05-01 18:58:20.936 12739-12739/? E/dnsmasq: [set_servers]

0xf0067|197.239.34.134|197.239.0.250
05-01 18:58:23.951 12739-12739/? E/dnsmasq: [set_servers]
0xf0067|197.239.34.134|197.239.0.250
05-01 18:59:12.056 12739-12739/? E/dnsmasq: [set_servers]
0xf0067|197.239.34.134|197.239.0.250
05-01 18:59:18.658 12739-12739/? E/dnsmasq: [set_servers]
0xf0067|197.239.34.134|197.239.0.250
05-01 18:59:36.279 1470-31364/? E/WakeLock: release without a matched
acquire!
05-01 18:59:46.744 12739-12739/? E/dnsmasq: [set_servers
0xf0067|197.239.34.134|197.239.0.250
05-01 18:59:50.427 12739-12739/? E/dnsmasq: [set_servers]
0xf0067|197.239.34.134|197.239.0.250
05-01 18:59:59.928 12739-12739/? E/dnsmasq: [set_servers]
0xf0067|197.239.34.134|197.239.0.250
05-01 19:00:07.068 12739-12739/? E/dnsmasq: [set_servers]
0xf0067|197.239.34.134|197.239.0.250
05-01 19:00:08.213 12739-12739/? E/dnsmasq: [set_servers]
0xf0067|197.239.34.134|197.239.0.250
05-01 19:00:14.927 12739-12739/? E/dnsmasq: [set_servers]
0xf0067|197.239.34.134|197.239.0.250
05-01 19:00:17.407 12739-12739/? E/dnsmasq: [set_servers]
0xf0067|197.239.34.134|197.239.0.250
05-01 19:00:21.026 12739-12739/? E/dnsmasq: [set_servers]
0xf0067|197.239.34.134|197.239.0.250
05-01 19:00:37.165 12739-12739/? E/dnsmasq: [set_servers]
0xf0067|197.239.34.134|197.239.0.250
05-01 19:00:40.252 12739-12739/? E/dnsmasq: [set_servers]
0xf0067|197.239.34.134|197.239.0.250
05-01 19:01:07.625 17890-18174/? E/memtrack_graphic: open
dir: /d/ion/clients/.
05-01 19:01:13.748 12739-12739/? E/dnsmasq: [set_servers]
0xf0067|197.239.34.134|197.239.0.250
05-01 19:01:16.867 20134-20134/? E/PQ: [PQ][PQWhiteList] libwlparser.so is
absent
05-01 19:01:18.823 20153-20153/? E/PQ: [PQ][PQWhiteList] libwlparser.so is
absent
05-01 19:01:21.310 12739-12739/? E/dnsmasq: [set_servers
0xf0067|197.239.34.134|197.239.0.250
05-01 19:01:25.563 20166-20166/? E/PQ: [PQ][PQWhiteList] libwlparser.so is
absent
05-01 19:01:25.595 20168-20168/? E/PQ: [PQ][PQWhiteList] libwlparser.so is
absent
05-01 19:01:27.572 20189-20189/? E/PQ: [PQ][PQWhiteList] libwlparser.so is
absent
05-01 19:01:29.291 20201-20201/com.alfapps.shoppinglist E/AndroidRuntime:
FATAL EXCEPTION: main

Process: com.alfapps.shoppinglist, PID: 20201

android.content.res.Resources$NotFoundException: String resource ID #0x7
                                                                                at android.content.res.Resources.getText(Resources.java:353)
                                                                                at android.widget.TextView.setText(TextView.java:4598)

                                                                                at com.alfapps.shoppinglist.ItemsAdapter.onBindViewHolder
(ItemsAdapter.java:51)

at com.alfapps.shoppinglist.ItemsAdapter.onBindViewHolder
(ItemsAdapter.java:18)

 at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder
 (RecyclerView.java:6482)

 at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder
 (RecyclerView.java:6515)

 at
 android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline
 (RecyclerView.java:5458)

 at

android.support.v7.widget.RecyclerView $ Recycler.tryGetViewHolderForPositionByDea dline(RecyclerView.java:5724)

android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDea dline(RecyclerView.java:5724)

at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition
(RecyclerView.java:5563)
                                                                                at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition
 (RecyclerView.java:5559)

at android.support.v7.widget.LinearLayoutManager$LayoutState.next
(LinearLayoutManager.java:2229)

at android.support.v7.widget.LinearLayoutManager.layoutChunk
  (LinearLayoutManager.java:1556)

  at android.support.v7.widget.LinearLayoutManager.fill
   (LinearLayoutManager.java:1516)

  at android.support.v7.widget.LinearLayoutManager.onLayoutChildren
   (LinearLayoutManager.java:608)

 at android.support.v7.widget.RecyclerView.dispatchLayoutStep2
 (RecyclerView.java:3693)

 at android.support.v7.widget.RecyclerView.dispatchLayout
(RecyclerView.java:3410)

at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3962)

 at android.view.View.layout(View.java:18419)

at android.view.ViewGroup.layout(ViewGroup.java:5792)

 at android.support.constraint.ConstraintLayout.onLayout
 (ConstraintLayout.java:1855)

at android.view.View.layout(View.java:18419)

  at android.view.ViewGroup.layout(ViewGroup.java:5792)

at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1767)

at android.view.View.layout(View.java:18419)

 at android.view.ViewGroup.layout(ViewGroup.java:5792)

 at android.support.constraint.ConstraintLayout.onLayout
(ConstraintLayout.java:1855)

 at android.view.View.layout(View.java:18419)

 at android.view.ViewGroup.layout(ViewGroup.java:5792)

 at android.widget.FrameLayout.layoutChildren(FrameLayout.java:383)

 at android.widget.FrameLayout.onLayout(FrameLayout.java:321)
                                                                                 at android.view.View.layout(View.java:18419)
                                                                                 at android.view.ViewGroup.layout(ViewGroup.java:5792)

at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:443)
                                                                          at android.view.View.layout(View.java:18419)
                                                                          at android.view.ViewGroup.layout(ViewGroup.java:5792)
                                                                          at android.widget.FrameLayout.layoutChildren(FrameLayout.java:383)
                                                                          at android.widget.FrameLayout.onLayout(FrameLayout.java:321)
                                                                          at android.view.View.layout(View.java:18419)
                                                                          at android.view.ViewGroup.layout(ViewGroup.java:5792)
                                                                          at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1982)
                                                                          at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1826)
                                                                          at android.widget.LinearLayout.onLayout(LinearLayout.java:1735)
                                                                          at android.view.View.layout(View.java:18419)
                                                                          at android.view.ViewGroup.layout(ViewGroup.java:5792)
                                                                          at android.widget.FrameLayout.layoutChildren(FrameLayout.java:383)
                                                                          at android.widget.FrameLayout.onLayout(FrameLayout.java:321)
                                                                          at com.android.internal.policy.DecorView.onLayout(DecorView.java:751)
                                                                          at android.view.View.layout(View.java:18419)
                                                                          at android.view.ViewGroup.layout(ViewGroup.java:5792)
                                                                          at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2711)
                                                                          at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2384)
                                                                          at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1462)
                                                                          at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6965)
                                                                          at android.view.Choreographer$CallbackRecord.run(Choreographer.java:917)
                                                                          at android.view.Choreographer.doCallbacks(Choreographer.java:715)

05-01 19:01:29.291 20201-20201/com.alfapps.shoppinglist E/AndroidRuntime:位于android.view.Choreographer.doFrame(Choreographer.java:650)在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:903)在android.os.Handler.handleCallback(Handler.java:836)在android.os.Handler.dispatchMessage(Handler.java:103)在android.os.Looper.loop(Looper.java:203)在android.app.ActivityThread.main(ActivityThread.java:6251)在java.lang.reflect.Method.invoke(本机方法)在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run
(ZygoteInit.java:1063)在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)

05-01 19:01:29.291 20201-20201/com.alfapps.shoppinglist E/AndroidRuntime: at android.view.Choreographer.doFrame(Choreographer.java:650) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:903) at android.os.Handler.handleCallback(Handler.java:836) at android.os.Handler.dispatchMessage(Handler.java:103) at android.os.Looper.loop(Looper.java:203) at android.app.ActivityThread.main(ActivityThread.java:6251) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run
(ZygoteInit.java:1063) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)

推荐答案

Android的 TextView setText 方法之一.android.com/reference/android/widget/TextView.html#setText(int)"rel =" nofollow noreferrer>接受整数资源ID .在 ItemsAdapter onBindViewHolder 方法中,以下三行尝试将文本设置为整数:

One of the setText methods of Android's TextView takes in an integer resource ID. In the onBindViewHoldermethod of your ItemsAdapter, these three lines attempt to set the text to an integer:

holder.number.setText(items.getNumber());
holder.budget.setText(items.getBudget());
holder.date.setText(items.getDate());

Android似乎无法理解您的意图,并认为您正在向其传递资源ID,而不是应该转换为字符串的整数.在将它们传递到 setText 方法之前,尝试将每个整数参数显式转换为字符串:

It looks like Android doesn't understand your intent here, and thinks you're passing it resource IDs, rather than integers that should be converted to strings. Try converting each integer argument to a string explicitly before passing them into the setText method:

holder.number.setText(String.valueOf(items.getNumber()));
holder.budget.setText(String.valueOf(items.getBudget()));
holder.date.setText(String.valueOf(items.getDate()));

这篇关于使用片段在recyclerView中的android自定义cardview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-02 02:02