I am using an SQLite db to populate a RecyclerView. I want to make the RecyclerView update real-time. But I am failing. I have tried using "notifyDataSetChanged()" but it doesn't work.
Can you please also tell me how can I refresh the RecyclerView by making a "new adapter".
Which one is better "notifyDataSetChanged();" or any other?
Here is my RecyclerViewAdapter
package e.wolverine2.thewalkingapp;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {
Context context;
List<World> worldList = new ArrayList<>();
public RecyclerViewAdapter() {
//Default Constructor
}
public RecyclerViewAdapter(Context context, List<World> worldList) {
this.context = context;
this.worldList = worldList;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view;
view = LayoutInflater.from(context).inflate(R.layout.single_row, parent, false);
MyViewHolder viewHolder = new MyViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull RecyclerViewAdapter.MyViewHolder holder, int position) {
holder.textView_Latitude.setText("Latitude : " + String.valueOf(worldList.get(position).getLatitiude()));
holder.textView_Longitude.setText("Longitude : " + String.valueOf(worldList.get(position).getLongitude()));
holder.textView_Time.setText("Time : " + worldList.get(position).getDate());
holder.textView_Location.setText("Location : \n" + "NEW LOCATION!");
}
@Override
public int getItemCount() {
return worldList.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
TextView textView_Latitude;
TextView textView_Longitude;
TextView textView_Time;
TextView textView_Location;
CardView cardView;
public MyViewHolder(View view) {
super(view);
textView_Latitude = (TextView) view.findViewById(R.id.textView_Latitude);
textView_Longitude = (TextView) view.findViewById(R.id.textView_Longitude);
textView_Time = (TextView) view.findViewById(R.id.textView_Time);
textView_Location = (TextView) view.findViewById(R.id.textView_Location);
cardView = (CardView) view.findViewById(R.id.cardView);
}
}
public void updateData(List<World> newWorldList) {
if (newWorldList != null && newWorldList.size() > 0) {
worldList.clear();
worldList.addAll(newWorldList);
new MainActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
notifyDataSetChanged();
Toast.makeText(context, "updateData Called!", Toast.LENGTH_SHORT).show();
}
});
}
}
}
**EDIT :
adapter.updateData(List);
gets called in "locationChanged(double, double)" method.
MainActivity.class :**
public class MainActivity extends AppCompatActivity implements Tab1.OnFragmentInteractionListener, Tab2.OnFragmentInteractionListener, Tab3.OnFragmentInteractionListener {
DBHelper helper;
World world;
Location location;
GPSTracker tracker;
RecyclerViewAdapter adapter;
private Tab1 tab1;
public static final String BROADCAST_ACTION = "e.wolverine2.thewalkingapp";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 123);
MessageReciever reciever = new MessageReciever(new Message());
Intent intent = new Intent(this, MyService.class);
intent.putExtra("reciever", reciever);
startService(intent);
tracker = new GPSTracker(getApplicationContext());
location = tracker.getLocation();
helper = new DBHelper(getApplicationContext());
tab1 = new Tab1();
adapter = new RecyclerViewAdapter(getApplicationContext(), helper.getList());
final TabLayout tabLayout = (TabLayout) findViewById(R.id.myTabLayout);
tabLayout.addTab(tabLayout.newTab().setText("LOCATIONS"));
tabLayout.addTab(tabLayout.newTab().setText("TOTAL DISTANCE"));
tabLayout.addTab(tabLayout.newTab().setText("CALS"));
tabLayout.getTabAt(0).setIcon(R.drawable.ic_list);
tabLayout.getTabAt(1).setIcon(R.drawable.ic_person_pin);
tabLayout.getTabAt(2).setIcon(R.drawable.ic_fitness_excercise);
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
//onError();
final ViewPager viewPager = (ViewPager) findViewById(R.id.myViewPager);
final PagerAdapter pagerAdapter = new PagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(pagerAdapter);
viewPager.setOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
try {
if (tab.getPosition() == 0) {
adapter.updateData(new DBHelper(getApplicationContext()).getList());
}
} catch (Exception e) {
Toast.makeText(MainActivity.this, "Main Activity " + e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
}
public void locationChanged(double longi, double lati) {
final Location location = new Location("");
location.setLatitude(lati);
location.setLongitude(longi);
world = new World();
String timeStamp = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss").format(new Date());
world.setLongitude(location.getLongitude());
world.setLatitiude(location.getLatitude());
world.setDate(timeStamp);
world.setTime(timeStamp);
world.setLocation("Anonymous");
helper.addRow(world);
adapter.updateData(new DBHelper(getApplicationContext()).getList());
}
@Override
public void onFragmentInteraction(Uri uri) {
}
public class Message {
public void displayMessage(int resultCode, Bundle resultData) {
try {
double longi = resultData.getDouble("longitude");
double lati = resultData.getDouble("latitude");
locationChanged(longi, lati);
} catch (Exception e) {
Toast.makeText(MainActivity.this, "TOASTY X : " + e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
}
public void onError() {
helper.onDropTable();
Toast.makeText(this, "TABLE DROPED!", Toast.LENGTH_SHORT).show();
}
}