使用SQLite数据库中的新条目更新RecyclerView

时间:2015-07-07 16:39:55

标签: android sqlite dialog android-recyclerview

我正在创建一个应用程序,其中主页面显示用户所做的所有最近得分预测。如果用户想要添加新的按钮,则按下加号按钮会出现一个对话框,以便用户输入新信息。

所有用户的数据都在SQLite数据库中,那么如何从对话框返回时使用新的/更新的用户历史更新RecyclerView?

主要活动:

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

}

提前感谢您,如果您需要更多代码,请发表评论

1 个答案:

答案 0 :(得分:0)

我认为你想要的是返回NewPrediction.class的结果。 在History中使用startActivityForResult()。然后在Predictions中使用setResult()返回结果

class History  {

     ...
     ...

    private static final int PERFORM_PREDICTION_REQUEST = 26252;

    PLUSBUTTON.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(History.this, NewPrediction.class);
            startActivityForResult(intent, PERFORM_PREDICTION_REQUEST);
        }
    });


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (PERFORM_PREDICTION_REQUEST == requestCode) {
            if (Activity.RESULT_OK == resultCode) {
                data.getString("datakey")
            }           
        }
    }       
}


class NewPrediction {
    ...
    Intent intent = new Intent();
    intent.putExtra("datakey", "somedata_that_is_somehow_related_to_prediction");
    setResult(Activity.RESULT_OK, intent)
    ...
}