I am using a grid view for a movie app. When the user clicks on a movie, details of the movie are revealed. Inside this new activity, the user can mark the movie as favorite. I'm using sharedPreferences to store the state of the checkbox when checked or unchecked. The problem is that, when i go back to the grid view to choose another movie to mark it as favorite, all the movies in the grid view show marked as favorite even though i did not explicitly check these movies. Please, what could be responsible for this behavior? Below is my detail activity code:
public class MovieDetails extends AppCompatActivity {
private static final String SHARED_PF_NAME = "movieSP";
private static final String CHECK_BOX_STATE = "check_state";
private SharedPreferences sharedPreferences;
CheckBox checkBox;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_details);
getSupportActionBar().setTitle("Movie Details");
/*//get values passed from main
movie = getIntent().getParcelableExtra(MainActivity.MOVIE_IN_CURRENT_CLICKED_POSITION);
// get movie id of the movie that was just passed
final int movieIdOfMovieInCurrentlyClicked= movie.getMovieid();
//get path for trailer and reviews
String movieVideoPath = "https://api.themoviedb.org/3/movie/" + movieIdOfMovieInCurrentlyClicked+ "/videos?api_key=" + MovieDataSource.API_KEY;
String movieReviewPath = "https://api.themoviedb.org/3/movie/" + movieIdOfMovieInCurrentlyClicked + "/reviews?api_key=" + MovieDataSource.API_KEY;
// initiate asynctask
new MovieTrailerAsyncTask().execute(movieVideoPath);
new MovieReviewAsyncTask().execute(movieReviewPath);
// trailer recycler
trailerRecyclerview = findViewById(R.id.trailers_RecyclerView);
trailerRecyclerview.setHasFixedSize(true);
trailerRecyclerview.setLayoutManager(trailerLinearLayoutManager);
movieTrailerRecyclerViewAdapter = new MovieTrailerRecyclerViewAdapter(MovieDetails.this, movieTrailerArrayList);
trailerRecyclerview.setAdapter(movieTrailerRecyclerViewAdapter);
//reviews recycler
reviewRecyclerView = findViewById(R.id.reviews_recyclerview);
trailerRecyclerview.setHasFixedSize(true);
reviewRecyclerView.setLayoutManager(reviewLinearLayoutManager);
movieReviewRecyclerViewAdapter = new MovieReviewRecyclerViewAdapter(MovieDetails.this, movieReviewsArrayList);
reviewRecyclerView.setAdapter(movieReviewRecyclerViewAdapter);
TextView movieTitleTextView = findViewById(R.id.movieTitle);
ImageView movieImageView = findViewById(R.id.movieImage);
TextView movieReleaseDateView = findViewById(R.id.movieReleaseDate);
TextView movieRatingView = findViewById(R.id.movieRating);
TextView movieDescriptionView = findViewById(R.id.movieDescription);
Picasso.with(this)
.load(movie.getMovieImagePath())
.fit()
.placeholder(R.drawable.progress_file)
.error(R.drawable.ic_launcher_background)
.into(movieImageView);
movieTitleTextView.setText(movie.getMovieTitle());
movieReleaseDateView.setText(movie.getMovieReleaseDate());
movieRatingView.setText(String.format("%s%s", String.valueOf(movie.getMovieRating()), ratingDenominator));
movieDescriptionView.setText(movie.getMovieDescripton());
*/
checkBox= findViewById(R.id.checkbox_button);
sharedPreferences = getSharedPreferences(SHARED_PF_NAME, Context.MODE_PRIVATE);
checkBox.setChecked(sharedPreferences.getBoolean(CHECK_BOX_STATE, false));
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
sharedPreferences.edit().putBoolean(CHECK_BOX_STATE, isChecked).apply();
if (isChecked){
Toast.makeText(getApplicationContext(), "checked",
Toast.LENGTH_LONG).show();
}else {
Toast.makeText(getApplicationContext(), "not checked",
Toast.LENGTH_LONG).show();
}
}
});
}
Grid view Adapter:
public class MovieDisplayAdapter extends BaseAdapter {
private Context context;
private ArrayList<Movie> movies;
MovieDisplayAdapter(Context context,ArrayList<Movie> movies) {
this.context = context;
this.movies = movies;
}
#Override
public int getCount() {
Log.i("count",String.valueOf(movies.size()));
return movies.size();
}
#Override
public Object getItem(int position) {
return movies.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
class ViewHolder {
ImageView imageView;
ViewHolder(View view) {
imageView = view.findViewById(R.id.imageView);
}
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
LayoutInflater inflater = LayoutInflater.from(context);
if (convertView == null) {
convertView = inflater.inflate(R.layout.single_row, parent, false);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
} else
{
holder = (ViewHolder) convertView.getTag();
}
Picasso.with(context)
.load(movies.get(position).getMovieImagePath())
.placeholder(R.drawable.progress_file)
.fit()
.error(R.drawable.ic_launcher_background)
.centerCrop()
.into(holder.imageView); // View where image is loaded.
return convertView;
}
}
MainActivity(intent code):
gridView.setAdapter(new MovieDisplayAdapter(MainActivity.this, movies));
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
movieIdOfMovieInCurrentlyClicked = movies.get(position).getMovieid();
moviePosition = position;
Intent intent = new Intent(MainActivity.this, MovieDetails.class);
//When clicked, the position of the current movie
Movie movieIncurrentClickedPosition = movies.get(moviePosition);
intent.putExtra(MOVIE_IN_CURRENT_CLICKED_POSITION, movieIncurrentClickedPosition);
startActivity(intent);
}
});
}
You are using a single variable (CHECK_BOX_STATE) to store the favorite status of all the movies, rather than storing the favorite status for each movie. You need to create a different boolean entry in the SharedPreferences for each favourite movie, and then remove it when the movie is no longer favourite.
public class MovieDetails extends AppCompatActivity {
private static final String SHARED_PF_NAME = "favoriteMovies";
private Movie movie; // I'm guessing the class name is Movie
private CheckBox checkBox;
private SharedPreferences sharedPreferences;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_details);
getSupportActionBar().setTitle("Movie Details");
movie = getIntent().getParcelableExtra(
MainActivity.MOVIE_IN_CURRENT_CLICKED_POSITION);
final String key = Integer.toString(movie.getMovieId());
// all the other stuff...
sharedPreferences = getSharedPreferences(SHARED_PF_NAME, Context.MODE_PRIVATE);
checkBox = findViewById(R.id.checkbox_button);
checkBox.setChecked(sharedPreferences.getBoolean(key, false));
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
sharedPreferences.edit().putBoolean(key, true).apply();
Toast.makeText(getApplicationContext(), "checked",
Toast.LENGTH_LONG).show();
} else {
sharedPreferences.edit().remove(key).apply();
Toast.makeText(getApplicationContext(), "not checked",
Toast.LENGTH_LONG).show();
}
}
});
}
}
the thing here is that you are saving a single check state, so when you try to retrieve that checkstate is just one, you have to filter each of the checkStates by an identifier for each of the movies...
as an additional comment is better to store that information in the movies object in a local database so you just do a simple query to update and retrieve that state
Related
I have 3 classes relating to my checkbox section of my app in Android studio, atm the check box loads, but when selecting and deselecting the value doesn't save when I go bak into it from the main menu. any help would great!!!
public class WatchList extends AppCompatActivity {
ArrayList dataModels;
ListView listView;
private WatchListAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
Log.i("watchlist", "created watchlist activity");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_watch_list);
listView = (ListView) findViewById(R.id.listview2);
dataModels = new ArrayList();
dataModels.add(new WatchListClass(R.drawable.kookaburra,"Kookaburra","Albury", false));
dataModels.add(new WatchListClass(R.drawable.cockatoo, "Cockatoo" , "Bathurst", true));
dataModels.add(new WatchListClass(R.drawable.emu,"Emu", "Echuca", true));
dataModels.add(new WatchListClass(R.drawable.magpie, "Magpie", "Sydney", true));
adapter = new WatchListAdapter(dataModels, getApplicationContext());
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView parent, View view, int position, long id) {
WatchListClass dataModel= (WatchListClass) dataModels.get(position);
dataModel.checked = !dataModel.checked;
adapter.notifyDataSetChanged();
}
});
}
}
public class WatchListAdapter extends ArrayAdapter {
private ArrayList dataSet;
Context mContext;
private static class ViewHolder {
TextView birdWatchName, birdWatchLocation;
ImageView birdWatchImage;
CheckBox checkBox;
}
public WatchListAdapter(ArrayList data, Context context) {
super(context, R.layout.watch_list, data);
this.dataSet = data;
this.mContext = context;
}
#Override
public int getCount() {
return dataSet.size();
}
#Override
public WatchListClass getItem(int position) {
return (WatchListClass) dataSet.get(position);
}
#Override
public View getView(int position, View convertView, #NonNull ViewGroup parent) {
ViewHolder viewHolder;
final View result;
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.watch_list, parent, false);
viewHolder.birdWatchImage = (ImageView) convertView.findViewById(R.id.birdWatchImage);
viewHolder.birdWatchName = (TextView) convertView.findViewById(R.id.birdWatchName);
viewHolder.birdWatchLocation = (TextView) convertView.findViewById(R.id.birdWatchLocation);
viewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.checkbox);
result=convertView;
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
result=convertView;
}
WatchListClass item = getItem(position);
viewHolder.birdWatchImage.setImageResource(item.birdWatchImage);
viewHolder.birdWatchName.setText(item.birdWatchName);
viewHolder.birdWatchLocation.setText(item.birdWatchLocation);
viewHolder.checkBox.setChecked(item.checked);
return result;
}
}
public class WatchListClass {
public String birdWatchName, birdWatchLocation;
int birdWatchImage;
boolean checked;
WatchListClass(int birdWatchImage, String birdWatchName,String birdWatchLocation, boolean checked) {
this.birdWatchName = birdWatchName;
this.birdWatchLocation = birdWatchLocation;
this.birdWatchImage = birdWatchImage;
this.checked = checked;
}
}
u can user shared preferences for check Box
try this one
How to save checkbox value with shared preferences?
you can use local database in app to store check box values and retrieve it back.
Good day!
There is a code:
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
private LayoutInflater inflater;
private List<PojoClassPrc> prc;
Context context;
RecyclerViewAdapter(Context context, List<PojoClassPrc> procedures) {
this.prc = procedures;
this.inflater = LayoutInflater.from(context);
}
#Override
public RecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.list_item, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(RecyclerViewAdapter.ViewHolder holder, int position) {
PojoClassPrc procedures = prc.get(position);
holder.sName1.setText(procedures.getsText1());
holder.sName3.setText(procedures.getsText3());
holder.sName2.setText(procedures.getsText2());
holder.sName1.setTextColor(Color.parseColor("#010101"));
holder.sName2.setTextColor(Color.parseColor("#ACACAC"));
holder.sName3.setTextColor(Color.parseColor("#737373"));
}
#Override
public int getItemCount() {
return prc.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
final TextView sName1, sName2, sName3;
final CheckBox sName5;
final CardView sName4;
ViewHolder(View view) {
super(view);
sName1 = (TextView) view.findViewById(R.id.lblListItem);
sName5 = (CheckBox) view.findViewById(R.id.checkBox2);
sName3 = (TextView) view.findViewById(R.id.lblListItem3);
sName2 = (TextView) view.findViewById(R.id.lblListItem2);
sName4 = (CardView) view.findViewById(R.id.item_card);
final boolean[] clicked = {false};
SharedPreferences prefs = context.getSharedPreferences("myPrefs", Context.MODE_PRIVATE);
boolean cbSelect = prefs.getBoolean("sName", false);
if (cbSelect){
sName3.setTextColor(Color.parseColor("#178DFC"));
sName4.setCardBackgroundColor(Color.parseColor("#C9FDFE"));
} else {
sName3.setTextColor(Color.parseColor("#737373"));
sName4.setCardBackgroundColor(Color.WHITE);
}
sName4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(!clicked[0]) {
sName5.setChecked(true);
sName3.setTextColor(Color.parseColor("#178DFC"));
sName4.setCardBackgroundColor(Color.parseColor("#C9FDFE"));
clicked[0] = true;
savePrefs(true, "sName");
} else {
sName5.setChecked(false);
sName3.setTextColor(Color.parseColor("#737373"));
sName4.setCardBackgroundColor(Color.WHITE);
clicked[0] = false;
savePrefs(false, "sName");
}
}});
}
}
private void savePrefs(boolean value, String name) {
SharedPreferences.Editor editor = context.getSharedPreferences("myPrefs", Context.MODE_PRIVATE).edit();
editor.putBoolean(name, value);
editor.apply();
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public int getItemViewType(int position) {
return position;
}
}
sName4 is CardView, the point is that I want to implement such a thing:
I clicked on the card, the color of the text changed and the checkbox was set, but in the second press it is necessary to make it as before, say the checkbox is removed, and the color turns white, I can’t realize it in any way with if else, I don’t catch up with what and how, tell me please!
Thank you in advance!!!
P.S. I have a lot of fragments, so I do everything in the adapter
You can use state of CheckBox by calling method isChecked()
Try this:
sName4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (sName3.isChecked()){
sName3.setChecked(false);
sName3.setTextColor(Color.parseColor("#17fc6b")); // set your color
}else{
sName3.setChecked(true);
sName3.setTextColor(Color.parseColor("#178DFC"));
}
}});
more about checkbox
I'am new to android and i use sample for my listview. because of that i can't change the code. i have one switch button in each item of my Listview. when i scroll the Listview switches change state randomly. how can i make switch state stable?
my listview class adapter:
public class MyCustomCursorAdapter extends CursorAdapter {
LIGHTS calling_activity;
private DatabaseHelper dbHelper;
public MyCustomCursorAdapter(Context context, Cursor c) {
super(context, c, 0);
this.calling_activity = (LIGHTS) context;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
return view;
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.adapter,parent,false);
}
#Override
public void bindView(final View view, Context context, Cursor cursor) {
((TextView)view.findViewById(R.id.id)).setText(cursor.getString(cursor.getColumnIndex(dbHelper._ID)));
((TextView)view.findViewById(R.id.KEYCODE)).setText(cursor.getString(cursor.getColumnIndex(dbHelper.TITLE)));
((TextView)view.findViewById(R.id.NAME)).setText(cursor.getString(cursor.getColumnIndex(dbHelper.DESC)));
ImageView option = view.findViewById(R.id.itemoption);
Switch thisswitch = view.findViewById(R.id.onoff);
thisswitch.setTag(cursor.getString(cursor.getColumnIndex(dbHelper._ID)));
thisswitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { calling_activity.myOnCheckedChangedHandler((String)buttonView.getTag(),isChecked);
}
});
option.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView itemID = (TextView) view.findViewById(R.id.id);
TextView itemTitle = (TextView) view.findViewById(R.id.KEYCODE);
TextView itemDesc = (TextView) view.findViewById(R.id.NAME);
String myId = itemID.getText().toString();
String myTitle = itemTitle.getText().toString();
String myDesc = itemDesc.getText().toString();
Intent intent = new Intent(calling_activity, ModifyActivity.class);
intent.putExtra("Id", myId);
intent.putExtra("Title", myTitle);
intent.putExtra("Desc", myDesc);
calling_activity.startActivity(intent);
}
});
}
}
and in my Lights activity :
#Override
public void myOnCheckedChangedHandler(String id, boolean check_status) {
Toast.makeText(
this,
"You changed the status for the row with an id of " + id +
" the status is now " + new Boolean(check_status).toString(),
Toast.LENGTH_SHORT).show();
String name = cursor.getString(cursor.getColumnIndex(dbHelper.DESC));
if(new Boolean(check_status).toString().equals("true")){
Toast.makeText(this,name +" is ON", Toast.LENGTH_SHORT).show();
}
}
Please manage through if/else case inside bindView at adapter, You need to check the switch condition like below :
// set the code into bind
if(switchCondition1 == 1)
{
//set the code as per condition wise
}else{
//
}
After change the state you also need to change the state into your list item and use notifydatasetChanged() method to refresh the list items.
I'm trying to go on MainActivity listview.itemclick to SecondActivity where is also listview. But what ever items on MainActivity listview items I have clicked, it always opens the same listview in SecondActivity. I want to open new listview always when I click different listview.itemclick at MainActivity
MainActivity listview on click
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
Intent intent;
Teams entry = (Teams) parent.getItemAtPosition(position);
intent = new Intent(TeamMenu.this, TeamSelectMenu.class);
intent.putExtra("team_id", entry.getTeamName());
startActivity(intent);
}
And when I try to catch it on SecondActivity:
Intent intent = getIntent();
intent.getStringExtra("team_id");
It still open same listview all the time.
sharedPreferences where I save my SecondActivity
private void savePlayerListToSharedpreference(ArrayList<Teams> playerList) {
String jsonPlayer = gson.toJson(playerList);
sharedPreference.savePlayersList(jsonPlayer);
}
Here I load sharedPreferences on my SecondActivity
private void getPlayerListFromSharedPreference() {
String jsonPlayer = sharedPreference.getPlayersList();
Type type = new TypeToken<List<Teams>>(){}.getType();
playerList = gson.fromJson(jsonPlayer, type);
if (playerList == null) {
playerList = new ArrayList<>();
}
}
Here I set it on my listview at SecondActivity
public void onGettingDataListener() {
if (playerList.size() == 0) {
Toast.makeText(TeamSelectMenu.this, "No data in sharedPreferences", Toast.LENGTH_SHORT).show();
} else {
getPlayerListFromSharedPreference();
PlayersListViewAdapter adapter = new PlayersListViewAdapter(TeamSelectMenu.this, R.layout.team_list, playerList);
listView.setAdapter(adapter);
}
}
SharedPreferences
public class TeamSharedPreferences {
private SharedPreferences pref;
private SharedPreferences.Editor editor;
// Context
private Context _context;
// Shared pref mode
int PRIVATE_MODE = 0;
private static final String PREF_NAME = "pref";
private static final String TEAMS = "teams";
private static final String PLAYERS = "players";
public TeamSharedPreferences(Context context) {
this._context = context;
pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
editor = pref.edit();
}
public void saveTeamsList(String scoreString) {
editor.putString(TEAMS, scoreString);
editor.commit();
}
public String getTeamsList() {
return pref.getString(TEAMS, "");
}
public void savePlayersList(String playerString) {
editor.putString(PLAYERS, playerString);
editor.commit();
}
public String getPlayersList() {
return pref.getString(PLAYERS, "");
}
}
ArrayAdapter
public class TeamListViewAdapter extends ArrayAdapter<Teams> {
private Activity activity;
public TeamListViewAdapter(Activity activity, int resource, List<Teams> teams) {
super(activity, resource, teams);
this.activity = activity;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
LayoutInflater inflater = (LayoutInflater) activity
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
// If holder not exist then locate all view from UI file.
if (convertView == null) {
// inflate UI from XML file
convertView = inflater.inflate(R.layout.team_list, parent, false);
// get all UI view
holder = new ViewHolder(convertView);
// set tag for holder
convertView.setTag(holder);
} else {
// if holder created, get tag from view
holder = (ViewHolder) convertView.getTag();
}
Teams teams = getItem(position);
holder.teamName.setText(teams.getTeamName());
return convertView;
}
private static class ViewHolder {
private TextView teamName;
//private TextView playerNames;
public ViewHolder(View v) {
teamName = (TextView) v.findViewById(R.id.teamList);
}
}}
I need help to fix the following problem:
When i scroll down my ListView Adapter the list that contains the country change to only one country.
This is my Adapter:
public class Nraeby_ListViewAdapter extends BaseAdapter {
private String Liked;
Context mContext;
// Declare Variables
LayoutInflater inflater;
private ArrayList<HashMap<String, String>> data;
ImageLoader imageLoader;
HashMap<String, String> resultp = new HashMap<String, String>();
public Nraeby_ListViewAdapter(Context context,
ArrayList<HashMap<String, String>> arraylist) {
this.data = arraylist;
mContext = context;
imageLoader = new ImageLoader(mContext);
inflater = LayoutInflater.from(mContext);
}
#Override
public int getCount() {
return data.size();
}
#Override
public Object getItem(int position) {
return data.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
public class ViewHolder {
// Declare Variables
TextView rank;
TextView country;
TextView population;
test.Droidlogin.CircleImage flag;
test.Droidlogin.material.AnimateCheckBox checkBox;
ImageButton btnFavourite;
}
public View getView(final int position, View view, ViewGroup parent) {
final ViewHolder holder;
if (view == null) {
holder = new ViewHolder();
view = inflater.inflate(R.layout.nearby_listview_item, null);
// Get the position
resultp = data.get(position);
// Locate the TextViews in nearby_listview_item.xmltem.xml
holder.rank = (TextView) view.findViewById(R.id.rank);
holder.country = (TextView) view.findViewById(R.id.country);
// Locate the ImageView in nearby_listview_item.xmltem.xml
holder.flag = (test.Droidlogin.CircleImage) view.findViewById(R.id.flag);
holder.checkBox = (test.Droidlogin.material.AnimateCheckBox) view.findViewById(R.id.checkbox);
holder.btnFavourite = (ImageButton) view.findViewById(R.id.like);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
// Capture position and set results to the TextViews
holder.rank.setText(resultp.get(NearbyUsers.RANK));
holder.country.setText(resultp.get(NearbyUsers.COUNTRY));
// Capture position and set results to the ImageView
// Passes flag images URL into ImageLoader.class
imageLoader.DisplayImage(resultp.get(NearbyUsers.FLAG), holder.flag);
TinyDB tinydb = new TinyDB(mContext);
Liked = tinydb.getString("MyUsers");
//This handle and change icon when click on.
holder.btnFavourite.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
TinyDB tinydb = new TinyDB(mContext);
holder.btnFavourite.setImageResource(R.drawable.icon_liked);
tinydb.putString("MyUsers",resultp.get(NearbyUsers.COUNTRY));
holder.btnFavourite.setImageResource(R.drawable.icon_liked);
}
});
// Capture ListView item click
view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// Get the position
Intent intent = new Intent(mContext, SingleItemViewNearbyProfile.class);
// Pass all data rank
intent.putExtra("rank", resultp.get(NearbyUsers.RANK));
// Pass all data country
intent.putExtra("country", resultp.get(NearbyUsers.COUNTRY));
// Pass all data population
intent.putExtra("population",resultp.get(NearbyUsers.POPULATION));
// Pass all data flag
intent.putExtra("flag", resultp.get(NearbyUsers.FLAG));
// Start SingleItemView Class
mContext.startActivity(intent);
}
});
return view;
}
}
pls i need help on how to fix the error
so that when scroll down it will show the list of all country and a button
You should move the line in getView to outside the if block. Like this
final ViewHolder holder;
resultp = data.get(position);
if (view == null) {
///
}
You are only updating the resultp when you create a new view. For a recycled view, you are using a stale data and that's the reason you see some incorrect country data after you scroll.