Here is my MainActivity.java:
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Parcelable;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Base64;
import android.util.Log;
import android.util.Xml;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.OvershootInterpolator;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.PasswordAuthentication;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import jp.wasabeef.recyclerview.animators.FadeInUpAnimator;
import jp.wasabeef.recyclerview.animators.SlideInLeftAnimator;
public class MainActivity extends ActionBarActivity {
public final static String username = "";
public final static String password = "";
public static String apiURL = "http://ift.tt/1OrxZeX";
final String temp = apiURL;
static final int PICK_ANIME = 1;
public StringBuffer response = new StringBuffer();
Context context = this;
public List<AnimeData> list = new ArrayList<AnimeData>();
public List <AnimeScores> scoreList = new ArrayList<AnimeScores>();
private RecyclerView recyclerView;
private MainRecyclerAdapter adapter;
DatabaseHelper db;
@Override
protected void onCreate(Bundle savedInstanceState) {
// if(list.size()>0){
// TextView t = (TextView)findViewById(R.id.noAnime);
// t.setText("HUR");
// }
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
recyclerView = (RecyclerView)findViewById(R.id.recycler_view);
final LinearLayoutManager layoutManager = new LinearLayoutManager(MainActivity.this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(new SlideInLeftAnimator());
db = new DatabaseHelper(getApplicationContext());
list = db.getTracked();
db.closeDB();
for(AnimeData t : list){
Log.i("Anime TITLE", t.gettitle());
}
if(list.size()>0){
TextView t = (TextView)findViewById(R.id.noAnime);
t.setText("");
}
adapter = new MainRecyclerAdapter(MainActivity.this, list, new CustomItemClickListener(){
@Override
public void onItemClick(View v, final int position) {
Log.d("","onClick " + position + " "+list.get(position).gettitle());
Intent intent = new Intent(MainActivity.this, ViewAnimeScores.class);
intent.putExtra("title", list.get(position).gettitle());
// intent.putExtra("image", list.get(position).getImage());
// intent.putExtra("synopsis", list.get(position).getSynopsis());
// intent.putExtra("scores", (ArrayList<AnimeScores>)scoreList);
startActivity(intent);
}
});
recyclerView.setAdapter(adapter);
recyclerView.setItemAnimator(new FadeInUpAnimator(new OvershootInterpolator(1f)));
FloatingActionButton fab = (FloatingActionButton)findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder =
new AlertDialog.Builder(context, R.style.AppCompatAlertDialogStyle);
builder.setTitle("Search Anime");
final EditText input1 = new EditText(context);
input1.setHint("Anime");
input1.setTextColor(getResources().getColor(R.color.ToolbarColor));
builder.setView(input1);
builder.setView(input1, 25, 0, 25, 0); // 10 spacing, left and right
builder.setPositiveButton("Search", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String value = input1.getText().toString();
String valueCopy = value;
value = value.replaceAll("[^a-zA-Z0-9\\s]", " "); //finds non alpha or space characters and repalces with space
value = value.replaceAll("[^a-zA-Z0-9]", "+"); //replaces all spaces with +
//Toast.makeText(getApplicationContext(), "temp =" + value, Toast.LENGTH_LONG).show();
apiURL += value;
Intent intent = new Intent(MainActivity.this, SearchAnime.class);
intent.putExtra("searchURL",apiURL);
intent.putExtra("searchString",valueCopy);
intent.putExtra("alreadyAdded", (ArrayList<AnimeData>) list);
startActivityForResult(intent, PICK_ANIME);
// new MALSearch().execute("");
apiURL = temp;
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if(requestCode == PICK_ANIME){
if(resultCode == RESULT_OK){
String title = data.getStringExtra("title");
String search = data.getStringExtra("search");
String synopsis = android.text.Html.fromHtml(data.getStringExtra("synopsis")).toString();
double score = data.getDoubleExtra("score", 0);
String imageURL = data.getStringExtra("image");
Log.d("","RESULT: " + title + "\n"+score+ "\n"+search+"\n"+imageURL +"\n"+synopsis);
String date = new SimpleDateFormat("MM/dd/yyyy",Locale.getDefault()).format(new Date());
db = new DatabaseHelper(getApplicationContext());
AnimeScores newScore = new AnimeScores(title, score, date);
AnimeData anime = new AnimeData(title, score, synopsis, imageURL, search);
long result = db.addAnime(anime);
long result1 = db.addScore(newScore);
db.closeDB();
list = db.getTracked();
for(AnimeData t : list){
Log.i("ANIME score", String.valueOf(t.getScore()));
}
//list.add(new AnimeData(title, score, synopsis, imageURL, search));
if(list.size()>0){
TextView t = (TextView)findViewById(R.id.noAnime);
t.setText("");
}
//get the data back
//add anime to list
//update recyclerview
adapter.notifyDataSetChanged();
//String date = new SimpleDateFormat("dd/MM/yyyy",Locale.getDefault()).format(new Date());
//scoreList.add(new AnimeScores(title, score, date));
}
}
}
@Override
public void onPause(){
super.onPause();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Basically, I'm using an SQLite database to keep track of data added by the user. In the onCreate method, I am able to successfully load my database into arraylist list and that into the recyclerview. However, when new data is added in the onActivityResult method, I call adapter.notifyDataSetChanged() but the new data is not visible in the recyclerview until I completely close out of the application and reopen it. The array list however does in fact have the new data I want displayed.
Aucun commentaire:
Enregistrer un commentaire