Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/195.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 以图片和字符串作为数据类型的RecyclerView_Java_Android_Android Recyclerview - Fatal编程技术网

Java 以图片和字符串作为数据类型的RecyclerView

Java 以图片和字符串作为数据类型的RecyclerView,java,android,android-recyclerview,Java,Android,Android Recyclerview,我正在制作一个带有RecyclerView的应用程序,它需要显示2个图像视图和3个文本视图。我遵循developers.android.com网站上的教程,效果不错。然后我尝试编辑适配器,现在我的活动不起作用 这是我的适配器.java文件: package com.winansbros.soccerpredictor; import android.graphics.drawable.Drawable; import android.support.v7.widget.RecyclerView

我正在制作一个带有RecyclerView的应用程序,它需要显示2个图像视图和3个文本视图。我遵循developers.android.com网站上的教程,效果不错。然后我尝试编辑适配器,现在我的活动不起作用

这是我的适配器.java文件:

package com.winansbros.soccerpredictor;

import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private String[] mDataset1;
private String[] mDataset2;
private String[] mDataset3;
private Drawable[] mDataset4;
private Drawable[] mDataset5;

// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
public static class ViewHolder extends RecyclerView.ViewHolder {
    // each data item is just a string in this case

    public TextView team1textview;
    public TextView team2textview;
    public TextView scoretextview;
    public ImageView team1imageview;
    public ImageView team2imageview;

    public ViewHolder(View v) {
        super(v);
        team1textview = (TextView) v.findViewById(R.id.team1name);
        team2textview = (TextView) v.findViewById(R.id.team2name);
        scoretextview = (TextView) v.findViewById(R.id.card1score);
        team1imageview = (ImageView) v.findViewById(R.id.team1imageview);
        team2imageview = (ImageView) v.findViewById(R.id.team2imageview);
    }
}

// Provide a suitable constructor (depends on the kind of dataset)
public MyAdapter(String[] myDataset, String[] myDataset2, String[] myDataset3, Drawable[] myDataset4, Drawable[] myDataset5) {
    mDataset1 = myDataset;
    mDataset2 = myDataset2;
    mDataset3 = myDataset3;
    mDataset4 = myDataset4;
    mDataset5 = myDataset5;
}

// Create new views (invoked by the layout manager)
@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                               int viewType) {
    // create a new view
    View v = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.cardviewwidget, parent, false);
    // set the view's size, margins, paddings and layout parameters
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    layoutParams.setMargins(10, 20, 10, 20);
    v.setLayoutParams(layoutParams);

    ViewHolder vh = new ViewHolder(v);
    return vh;
}

// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    // - get element from your dataset at this position
    // - replace the contents of the view with that element
    holder.team1textview.setText(mDataset1[position]);
    holder.team2textview.setText(mDataset2[position]);
    holder.scoretextview.setText(mDataset3[position]);
    holder.team1imageview.setImageDrawable(mDataset4[position]);
    holder.team2imageview.setImageDrawable(mDataset5[position]);
}

// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
    return mDataset1.length;
}
}

它以什么方式不起作用?会发生什么?logcat似乎就在有趣的东西开始的地方被切断了crash@JHHlogcat的唯一额外输出是06-13 16:52:07.545 2085-2095/com.winansbros.soccerpredictor I/art﹕ 线程[5,tid=2095,WaitingMainSignalCatcherLoop,线程*=0xb4a33c00,对等方=0x12c000a0,“信号捕捉器”]:对信号3作出反应06-13 16:52:07.545 2085-2095/com.winansbros.soccerpredictor I/art﹕ [06-13 16:52:07.745 546:569 I/Process]正在发送信号。PID:546 SIG:3发生了什么事?@JHH当我按下按钮进入活动时,我的平板电脑屏幕变黑,新活动永远不会加载。然后会出现“应用程序没有响应,是否要关闭它?”提示。请看,这是有用的信息。ANR表示您正在阻塞主线程。您发布了一堵代码墙,因此我不清楚您在哪里进行阻塞调用,但很明显您正在从onCreate方法(即从主线程)进行一些数据库调用等。您应该将任何I/O卸载到单独的线程。也就是说,我现在不知道到底是什么导致了ANR,除非那些db调用非常缓慢。启动调试器并暂停主线程,然后查看它挂起的位置。
package com.winansbros.soccerpredictor;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import java.util.ArrayList;
import java.util.List;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;

public class History extends Activity {

Context CTX = this;

AdView mAdView;
AdRequest adRequest;

Button clearButton;
Button tipsButton;

StringBuilder sb;
DatabaseOperations DOP;

Integer team1image;
Integer team2image;

TypedArray imgs;

List<String> winners;
List<String> hometeams;
List<String> awayteams;
List<String> scores;
List<Drawable> homeimages;
List<Drawable> awayimages;

private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;

Resources res;

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_history);

    res = getResources();

    DOP = new DatabaseOperations(CTX);
    Cursor CR = DOP.getInformation(DOP);
    CR.moveToFirst();

    winners = new ArrayList<>();
    hometeams = new ArrayList<>();
    awayteams = new ArrayList<>();
    scores = new ArrayList<>();
    homeimages = new ArrayList<>();
    awayimages = new ArrayList<>();

    if( CR.moveToFirst() ) {
        getHistory();
    }

    mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);

    mRecyclerView.setHasFixedSize(true);

    mLayoutManager = new LinearLayoutManager(CTX);
    mRecyclerView.setLayoutManager(mLayoutManager);

    String[] myDataset = hometeams.toArray(new String[hometeams.size()]);
    String[] myDataset2 = awayteams.toArray(new String[awayteams.size()]);
    String[] myDataset3 = scores.toArray(new String[scores.size()]);
    Drawable[] myDataset4 = homeimages.toArray(new Drawable[homeimages.size()]);
    Drawable[] myDataset5 = awayimages.toArray(new Drawable[awayimages.size()]);


    mAdapter = new MyAdapter(myDataset, myDataset2, myDataset3, myDataset4, myDataset5);
    mRecyclerView.setAdapter(mAdapter);

    clearButton = (Button) findViewById(R.id.clearSQLite);
    tipsButton = (Button) findViewById(R.id.TipsButton);

    mAdView = (AdView) findViewById(R.id.adView);
    adRequest = new AdRequest.Builder()
            .addTestDevice("8AC41E108CD62B7703FF28358AEEC8BC")
            .build();
    mAdView.loadAd(adRequest);

    Log.d("Progress", "Far");

    clearButton.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            DatabaseOperations DOB = new DatabaseOperations(CTX);
            DOB.DeleteInformation(CTX);
            DOB.close();
            finish();
        }
    });

    Log.d("Progress", "First On Click Listener");

    //sb = new StringBuilder();

    //historyTextView = (TextView) findViewById(R.id.historyText);
    //historyTextView.setMovementMethod(new ScrollingMovementMethod());



    tipsButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getApplicationContext(), TipsActivity.class);
            startActivity(intent);
        }
    });

    Log.d("Progress", "second On Click Listener");

    DOP.close();
}

public void getHistory() {
    Log.i("GetHistory", "Initialized");

    Cursor CR = DOP.getInformation(DOP);
    CR.moveToFirst();


    do {
        winners.add(CR.getString(0));
        hometeams.add(CR.getString(1));
        awayteams.add(CR.getString(2));
        scores.add(CR.getString(3));
        Log.d("Cloud Files", "OBJECT ID SET");

    } while (CR.moveToNext());

    Log.d("GetHistory", "Lists all set");

    int size = winners.size();

    for (int i = 0; i < size; i++)
    {
        if (hometeams.get(i) == "Arsenal")
        {
            int resID = res.getIdentifier("arsenal", "drawable", getPackageName());
            homeimages.add(res.getDrawable(resID));
        }
        ...

        if (awayteams.get(i) == "Arsenal")
        {
            int resID = res.getIdentifier("arsenal", "drawable", getPackageName());
            awayimages.add(res.getDrawable(resID));
        }

        if (awayteams.get(i) == "West Ham")
        {
            int resID = res.getIdentifier("westham", "drawable", getPackageName());
            awayimages.add(res.getDrawable(resID));
        }
    }

    Log.d("GetHistory", "Images set");

    Log.d("Cloud Files", Integer.toString(size));

    Log.d("Cloud Files", awayimages.get(0).toString());

}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            xmlns:card_view="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

<android.support.v7.widget.CardView
tools:context="com.winansbros.soccerpredictor.History"
android:id="@+id/card_view"
android:layout_gravity="center"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:id="@+id/team1imageview"
        android:layout_alignParentTop="true"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/team1name"
        android:layout_centerVertical="true"
        android:layout_below="@+id/team2name"
        android:layout_toRightOf="@+id/team1imageview"
        android:layout_toEndOf="@+id/team1imageview"
        android:width="100dp"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/card1score"
        android:width="20dp"
        android:layout_centerVertical="true"
        android:layout_alignTop="@+id/team1name"
        android:layout_toRightOf="@+id/team1name"
        android:layout_toEndOf="@+id/team1name"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/team2name"
        android:width="100dp"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@+id/team2imageview"
        android:layout_toStartOf="@+id/team2imageview"/>
    <ImageView
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:id="@+id/team2imageview"
        android:layout_centerVertical="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"/>

</RelativeLayout>

</android.support.v7.widget.CardView>

</LinearLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
xmlns:ads="http://schemas.android.com/apk/res-auto" xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin">

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        ads:adSize="BANNER"
        ads:adUnitId="@string/banner_ad_unit_id">
    </com.google.android.gms.ads.AdView>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Clear History"
        android:id="@+id/clearSQLite"
        android:layout_above="@+id/adView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:width="150dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Tips"
        android:id="@+id/TipsButton"
        android:width="150dp"
        android:layout_above="@+id/adView"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"/>

<android.support.v7.widget.RecyclerView
    android:id="@+id/my_recycler_view"
    android:scrollbars="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/clearSQLite"/>

</RelativeLayout>
...
06-13 16:51:47.510    2085-2085/com.winansbros.soccerpredictor I/GetHistory﹕ Initialized
06-13 16:51:47.510    2085-2085/com.winansbros.soccerpredictor D/Cloud Files﹕ OBJECT ID SET
06-13 16:51:47.511    2085-2085/com.winansbros.soccerpredictor D/Cloud Files﹕ OBJECT ID SET
06-13 16:51:47.511    2085-2085/com.winansbros.soccerpredictor D/GetHistory﹕ Lists all set
06-13 16:51:47.511    2085-2085/com.winansbros.soccerpredictor D/GetHistory﹕ Images set
06-13 16:51:47.511    2085-2085/com.winansbros.soccerpredictor D/Cloud Files﹕ 2
06-13 16:51:47.511    2085-2085/com.winansbros.soccerpredictor D/AndroidRuntime﹕ Shutting down VM
06-13 16:52:07.545    2085-2095/com.winansbros.soccerpredictor I/art﹕ Thread[5,tid=2095,WaitingInMainSignalCatcherLoop,Thread*=0xb4a33c00,peer=0x12c000a0,"Signal Catcher"]: reacting to signal 3
06-13 16:52:07.545    2085-2095/com.winansbros.soccerpredictor I/art﹕ [ 06-13 16:52:07.745   546:  569 I/Process  ]
Sending signal. PID: 546 SIG: 3