пользовательский просмотр карт в Android в режиме recyclerView с использованием фрагментов - PullRequest
0 голосов
/ 01 мая 2018

Проблема в том, что мое приложение вылетает каждый раз, когда я его запускаю .. есть ли здесь проблемы? Я пытаюсь реализовать просмотры карт в программе recyclerView, которая инициализируется во фрагменте.

Вот мой код.

Это макет фрагмента

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>

Вот макет карты с кучей текстов

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>

пока здесь, это класс, где у меня есть сеттеры и геттеры.

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;
    }


}

Ниже представлен адаптер указанного класса, как вы можете видеть!

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();
    }
}

В то время как здесь ниже класс фрагмента, где я хочу, чтобы при просмотре этого класса отображались карты.

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();
}

Любая помощь / решение / вклад любого из вас, опытных ребят, будет высоко оценен любым полезным. Спасибо!

Журнал аварии

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)

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: at 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)

Ответы [ 2 ]

0 голосов
/ 01 мая 2018

Замените ваш onBindViewHolder следующим образом. Это может решить вашу проблему.

@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(String.valueOf(items.getNumber()));
        holder.budget.setText(String.valueOf(items.getBudget()));
        holder.date.setText(String.valueOf(items.getDate()));
    }
0 голосов
/ 01 мая 2018

Один из setText методов Android TextView принимает целочисленный идентификатор ресурса . В методе onBindViewHolder вашего ItemsAdapter эти три строки пытаются установить текст в целое число:

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

Похоже, что Android здесь не понимает ваших намерений и думает, что вы передаете ему идентификаторы ресурсов, а не целые числа, которые должны быть преобразованы в строки. Попробуйте преобразовать каждый целочисленный аргумент в строку перед передачей их в метод setText:

holder.number.setText(String.valueOf(items.getNumber()));
holder.budget.setText(String.valueOf(items.getBudget()));
holder.date.setText(String.valueOf(items.getDate()));
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...