mardi 7 juillet 2015

Update RecyclerView with new entries in SQLite database

I have an app I am making where the main page shows all of the recent score predictions a user has made. If the user wants to add a new one, they press a plus button and a dialog comes up so the user can enter the new information.

All the user's data is in a SQLite database, so how can I update the RecyclerView with the new/updated user history on return from the dialog?

The main activity:

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.HashMap;
import java.util.List;
import java.util.Map;
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;

final int REQUEST =  1;

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;
List<Integer> homeimages;
List<Integer> awayimages;

public Map<String, Integer> map = new HashMap<>();

public void onCreate(Bundle savedInstanceState) {

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

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

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

    tracker.setScreenName("main screen");

    Parse.enableLocalDatastore(this);

    Parse.initialize(this, "xxxx", "xxxx");

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

    setMap();

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

    if(CR.moveToFirst()) {
        do {
            String currenthome = CR.getString(1);
            hometeams.add(currenthome);
            homeimages.add(map.get(currenthome));
            String currentaway = CR.getString(2);
            awayteams.add(currentaway);
            awayimages.add(map.get(currentaway));
            scores.add(CR.getString(3));
            Log.d("Cloud Files", "OBJECT ID SET");
        } while (CR.moveToNext());

        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()];
        myDataset4 = homeimages.toArray(myDataset4);
        Integer[] myDataset5 = new Integer[awayteams.size()];
        myDataset5 = awayimages.toArray(myDataset5);

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

    }

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

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

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

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

    new Thread(new Runnable() {
        @Override
        public void run() {
  ****stuff unrelated to this question****
    }).start();

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

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

}

public void setMap() {
    map.put("Arsenal", R.drawable.arsenal);
    ...
    map.put("Villarreal", R.drawable.villarreal);
}

}

Thank you in advance, and please comment if you need more code

Aucun commentaire:

Enregistrer un commentaire