对话后更新RecyclerView

时间:2015-06-26 20:58:58

标签: android sqlite dynamic android-recyclerview

我有一个预测足球比赛胜利者的应用程序,我正在尝试更改用户界面。 我创建了将数据输入对话框活动的屏幕,并将“历史记录”活动作为主要活动。按“历史记录”活动中的按钮调用输入数据的对话框。

在对话框终止后,是否有任何方法可以使用用户的历史记录更新recyclerView。

换句话说,当用户在对话活动中输入数据时,有没有办法在recyclelerview中为cardview添加条目,在sqlite数据库中同时包含新旧数据?

在我的代码中,启动获取用户输入的对话框的按钮在我的代码中称为PLUSBUTTON。当用户在Dialog上单击Enter时,他们的数据将添加到SQLite数据库中。 SQLite数据库仅在创建主活动时读取,因此显然需要在该方面添加一些内容。

历史活动数据:

package com.winansbros.soccerpredictor;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.*;
import android.support.v7.widget.CardView;
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 android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.Tracker;
import com.parse.FindCallback;
import com.parse.Parse;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;

public class History extends Activity {

Context CTX = this;

AdView mAdView;
AdRequest adRequest;

CardView GOTWCARDVIEW;

TextView GOTWHOMETEAM;
TextView GOTWAWAYTEAM;
TextView GOTWSCORE;
ImageView GOTWTEAMHOME;
ImageView GOTWTEAMAWAY;
ImageView PLUSBUTTON;

public static GoogleAnalytics analytics;
public static Tracker tracker;

DatabaseOperations DOP;

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

List<String> hometeams;
List<String> awayteams;
List<String> scores;

public void onCreate(Bundle savedInstanceState) {

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

    analytics = GoogleAnalytics.getInstance(this);
    analytics.setLocalDispatchPeriod(1800);

    tracker = analytics.newTracker(***);
    tracker.enableExceptionReporting(true);
    tracker.enableAdvertisingIdCollection(true);
    tracker.enableAutoActivityTracking(true);

    tracker.setScreenName("main screen");

    Parse.enableLocalDatastore(this);

    Parse.initialize(****);

    hometeams = new ArrayList<>();
    awayteams = new ArrayList<>();
    scores = new ArrayList<>();

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

    if(CR.moveToFirst()) {
        do {
            hometeams.add(CR.getString(1));
            awayteams.add(CR.getString(2));
            scores.add(CR.getString(3));
            Log.d("Cloud Files", "OBJECT ID SET");
            getHistory();
        } while (CR.moveToNext());
    }

    CR.close();
    DOP.close();

    mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
    mRecyclerView.setHasFixedSize(false);
    mLayoutManager = new LinearLayoutManager(CTX);
    mRecyclerView.setLayoutManager(mLayoutManager);

    mRecyclerView.setAdapter(mAdapter);

    GOTWCARDVIEW = (CardView) findViewById(R.id.featuredgotwcardview);
    GOTWHOMETEAM = (TextView) findViewById(R.id.gotwteam1textview);
    GOTWAWAYTEAM = (TextView) findViewById(R.id.gotwteam2textview);
    GOTWSCORE = (TextView) findViewById(R.id.gotwpredictedscore);
    GOTWTEAMAWAY= (ImageView) findViewById(R.id.gotwteam2imageview);
    GOTWTEAMHOME = (ImageView) findViewById(R.id.gotwteam1imageview);
    PLUSBUTTON = (ImageView) findViewById(R.id.newGame);

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

    new Thread(new Runnable() {
        @Override
        public void run() {

            final ParseQuery<ParseObject> query = ParseQuery.getQuery("GameOfTheWeek");
            query.orderByDescending("createdAt").setLimit(1);
            query.findInBackground(new FindCallback<ParseObject>() {
                @Override
                public void done(List<ParseObject> list, ParseException e) {
                    if (e == null) {
                        Log.d("name", "Retrieved GOTW successfully");
                        GOTWHOMETEAM.setText(list.get(0).getString("HomeTeam"));
                        GOTWAWAYTEAM.setText(list.get(0).getString("AwayTeam"));
                        GOTWSCORE.setText(list.get(0).getString("PredictedScore"));
                        String homeimage = list.get(0).getString("homeimage");
                        int resID= getResources().getIdentifier(homeimage, "drawable", getPackageName());
                        GOTWTEAMHOME.setImageResource(resID);
                        String awayimage = list.get(0).getString("awayimage");
                        int resID2= getResources().getIdentifier(awayimage, "drawable", getPackageName());
                        GOTWTEAMAWAY.setImageResource(resID2);
                    } else {
                        Log.d("name", "Error: " + e.getMessage());
                    }
                }
            });

            GOTWCARDVIEW.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(CTX, gameoftheweekfurtherinfo.class);
                    startActivity(intent);
                }
            });

        }
    }).start();

    PLUSBUTTON.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent intent = new Intent(History.this, MainActivity.class);
            startActivity(intent);
        }
    });

}

public void getHistory(){

    int size = hometeams.size();

    String[] myDataset = new String[hometeams.size()];
    myDataset = hometeams.toArray(myDataset);
    String[] myDataset2 = new String[awayteams.size()];
    myDataset2 = awayteams.toArray(myDataset2);
    String[] myDataset3 = new String[scores.size()];
    myDataset3 = scores.toArray(myDataset3);
    Log.d("Progress", "DOnt worry");
    Integer[] myDataset4 = new Integer[hometeams.size()];
    Integer[] myDataset5 = new Integer[awayteams.size()];

    for (int i = 0; i < size; i++) {
        if (myDataset[i].equals("Arsenal")) {
            myDataset4[i]=(R.drawable.arsenal);
        } else if (myDataset[i].equals("Aston Villa")) {
            myDataset4[i]=(R.drawable.astonvilla);
        } else if (myDataset[i].equals("Burnley")) {
            myDataset4[i]=((R.drawable.burnley));
        } else if (myDataset[i].equals("Chelsea")) {
            myDataset4[i]=((R.drawable.chelsea));
        } else if (myDataset[i].equals("Crystal Palace")) {
            myDataset4[i]=(R.drawable.crystalpalace);
        } else if (myDataset[i].equals("Everton")) {
            myDataset4[i]=(R.drawable.everton);
        } else if (myDataset[i].equals("Hull City")) {
            myDataset4[i]=((R.drawable.hullcity));
        } else if (myDataset[i].equals("Leicester City")) {
            myDataset4[i]=((R.drawable.leicestercity));
        } else if (myDataset[i].equals("Liverpool")) {
            myDataset4[i]=((R.drawable.liverpool));
        } else if (myDataset[i].equals("Man City")) {
            myDataset4[i]=((R.drawable.mancity));
        } else if (myDataset[i].equals("Man United")) {
            myDataset4[i]=((R.drawable.manunited));
        } else if (myDataset[i].equals("Newcastle")) {
            myDataset4[i]=((R.drawable.newcastle));
        } else if (myDataset[i].equals("QPR")) {
            myDataset4[i]=((R.drawable.qpr));
        } else if (myDataset[i].equals("Southampton")) {
            myDataset4[i]=((R.drawable.southampton));
        } else if (myDataset[i].equals( "Stoke City")) {
            myDataset4[i]=(R.drawable.stokecity);
        } else if (myDataset[i].equals("Sunderland")) {
            myDataset4[i]=((R.drawable.sunderland));
        } else if (myDataset[i].equals("Swansea City")) {
            myDataset4[i]=((R.drawable.swanseacity));
        } else if (myDataset[i].equals("Tottenham")) {
            myDataset4[i]=((R.drawable.tottenham));
        } else if (myDataset[i].equals("West Brom")) {
            myDataset4[i]=((R.drawable.westbrom));
        } else if (myDataset[i].equals("West Ham")){
            myDataset4[i]=((R.drawable.westham));
        } else if (myDataset[i].equals("Almería")) {
            myDataset4[i]=(R.drawable.almeria);
        } else if (myDataset[i].equals("Athletic Bilbao")) {
            myDataset4[i]=(R.drawable.athleticbilbao);
        } else if (myDataset[i].equals("Athlético Madrid")) {
            myDataset4[i]=((R.drawable.athleticomadrid));
        } else if (myDataset[i].equals("Barcalona")) {
            myDataset4[i]=((R.drawable.barcalona));
        } else if (myDataset[i].equals("Celta Vigo")) {
            myDataset4[i]=(R.drawable.celtavigo);
        } else if (myDataset[i].equals("Córdoba")) {
            myDataset4[i]=(R.drawable.cordoba);
        } else if (myDataset[i].equals("Deportivo La Coruña")) {
            myDataset4[i]=((R.drawable.deportivolacoruna));
        } else if (myDataset[i].equals("Eibar")) {
            myDataset4[i]=((R.drawable.eibar));
        } else if (myDataset[i].equals("Elche")) {
            myDataset4[i]=((R.drawable.elche));
        } else if (myDataset[i].equals("Espanyol")) {
            myDataset4[i]=((R.drawable.espanyol));
        } else if (myDataset[i].equals("Getafe")) {
            myDataset4[i]=((R.drawable.getafe));
        } else if (myDataset[i].equals("Granada")) {
            myDataset4[i]=((R.drawable.granada));
        } else if (myDataset[i].equals("Levante")) {
            myDataset4[i]=((R.drawable.levante));
        } else if (myDataset[i].equals("Málaga")) {
            myDataset4[i]=((R.drawable.malaga));
        } else if (myDataset[i].equals( "Rayo Vallecano")) {
            myDataset4[i]=(R.drawable.rayovallecano);
        } else if (myDataset[i].equals("Real Madrid")) {
            myDataset4[i]=((R.drawable.realmadrid));
        } else if (myDataset[i].equals("Real Sociedad")) {
            myDataset4[i]=((R.drawable.realsociedad));
        } else if (myDataset[i].equals("Sevilla")) {
            myDataset4[i]=((R.drawable.sevilla));
        } else if (myDataset[i].equals("Valencia")) {
            myDataset4[i]=((R.drawable.valencia));
        } else if (myDataset[i].equals("Villarreal")){
            myDataset4[i]=((R.drawable.villarreal));
        } else {
            myDataset4[i]=(R.drawable.imageismissing);
        }

        if (myDataset2[i].equals("Arsenal")) {
            myDataset5[i]=((R.drawable.arsenal));
        } else if (myDataset2[i].equals("Aston Villa")) {
            myDataset5[i]=((R.drawable.astonvilla));
        } else if (myDataset2[i].equals("Burnley")) {
            myDataset5[i]=((R.drawable.burnley));
        } else if (myDataset2[i].equals("Chelsea")) {
            myDataset5[i]=((R.drawable.chelsea));
        } else if (myDataset2[i].equals("Crystal Palace")) {
            myDataset5[i]=((R.drawable.crystalpalace));
        } else if (myDataset2[i].equals("Everton")) {
            myDataset5[i]=((R.drawable.everton));
        } else if (myDataset2[i].equals("Hull City")) {
            myDataset5[i]=((R.drawable.hullcity));
        } else if (myDataset2[i].equals("Leicester City")) {
            myDataset5[i]=((R.drawable.leicestercity));
        } else if (myDataset2[i].equals("Liverpool")) {
            myDataset5[i]=((R.drawable.liverpool));
        } else if (myDataset2[i].equals("Man City")) {
            myDataset5[i]=((R.drawable.mancity));
        } else if (myDataset2[i].equals("Man United")) {
            myDataset5[i]=((R.drawable.manunited));
        } else if (myDataset2[i].equals("Newcastle")) {
            myDataset5[i]=((R.drawable.newcastle));
        } else if (myDataset2[i].equals("QPR")) {
            myDataset5[i]=((R.drawable.qpr));
        } else if (myDataset2[i].equals("Southampton")) {
            myDataset5[i]=((R.drawable.southampton));
        } else if (myDataset2[i].equals("Stoke City")) {
            myDataset5[i]=((R.drawable.stokecity));
        } else if (myDataset2[i].equals("Sunderland")) {
            myDataset5[i]=((R.drawable.sunderland));
        } else if (myDataset2[i].equals("Swansea City")) {
            myDataset5[i]=(R.drawable.swanseacity);
        } else if (myDataset2[i].equals("Tottenham")) {
            myDataset5[i]=(R.drawable.tottenham);
        } else if (myDataset2[i].equals("West Brom")) {
            myDataset5[i]=((R.drawable.westbrom));
        } else if (myDataset2[i].equals("West Ham")) {
            myDataset5[i] = ((R.drawable.westham));
        } else if (myDataset2[i].equals("Almería")) {
            myDataset5[i]=(R.drawable.almeria);
        } else if (myDataset2[i].equals("Athletic Bilbao")) {
            myDataset5[i]=(R.drawable.athleticbilbao);
        } else if (myDataset2[i].equals("Athlético Madrid")) {
            myDataset5[i]=((R.drawable.athleticomadrid));
        } else if (myDataset2[i].equals("Barcalona")) {
            myDataset5[i]=((R.drawable.barcalona));
        } else if (myDataset2[i].equals("Celta Vigo")) {
            myDataset5[i]=(R.drawable.celtavigo);
        } else if (myDataset2[i].equals("Córdoba")) {
            myDataset5[i]=(R.drawable.cordoba);
        } else if (myDataset2[i].equals("Deportivo La Coruña")) {
            myDataset5[i]=((R.drawable.deportivolacoruna));
        } else if (myDataset2[i].equals("Eibar")) {
            myDataset5[i]=((R.drawable.eibar));
        } else if (myDataset2[i].equals("Elche")) {
            myDataset5[i]=((R.drawable.elche));
        } else if (myDataset2[i].equals("Espanyol")) {
            myDataset5[i]=((R.drawable.espanyol));
        } else if (myDataset2[i].equals("Getafe")) {
            myDataset5[i]=((R.drawable.getafe));
        } else if (myDataset2[i].equals("Granada")) {
            myDataset5[i]=((R.drawable.granada));
        } else if (myDataset2[i].equals("Levante")) {
            myDataset5[i]=((R.drawable.levante));
        } else if (myDataset2[i].equals("Málaga")) {
            myDataset5[i]=((R.drawable.malaga));
        } else if (myDataset2[i].equals( "Rayo Vallecano")) {
            myDataset5[i]=(R.drawable.rayovallecano);
        } else if (myDataset2[i].equals("Real Madrid")) {
            myDataset5[i]=((R.drawable.realmadrid));
        } else if (myDataset2[i].equals("Real Sociedad")) {
            myDataset5[i]=((R.drawable.realsociedad));
        } else if (myDataset2[i].equals("Sevilla")) {
            myDataset5[i]=((R.drawable.sevilla));
        } else if (myDataset2[i].equals("Valencia")) {
            myDataset5[i]=((R.drawable.valencia));
        } else if (myDataset2[i].equals("Villarreal")){
            myDataset5[i]=((R.drawable.villarreal));
        } else {
            myDataset5[i]=(R.drawable.imageismissing);
        }
}

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


}
}

提前谢谢

1 个答案:

答案 0 :(得分:0)

当您开始对话的意图时,您可以使用startActivityForResult然后覆盖onActivityResult中的History.class来检索从MainActivity.class传递的数据。跟随here。如果你需要我更具体地发布你的MainActivity.class,但这应该是足够的信息来帮助你。