Recyclerview with timer reusing the old views - android

I have a list of 20 custom count up timers in Recyclerview, if i start first one, the 14th or 15th row also showing same timer in the textview. I guess the timer is holding the reference to the first textview and it is updating it continuously which is reused in 14th or 15th row. Can any one tell me how to do this efficiently. Thanks in Advance.
MainActivity.java
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private RecyclerView mRecyclerView;
private CountAdapter mAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAdapter = new CountAdapter(getCount(), this);
mRecyclerView = (RecyclerView) findViewById(R.id.rv);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(linearLayoutManager);
mRecyclerView.setAdapter(mAdapter);
}
private List<String> getCount() {
List<String> strings = new ArrayList<>();
for (int i = 0; i < 20; i++) {
strings.add(i + "");
}
return strings;
}
}
CountAdapter.java
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class CountAdapter extends RecyclerView.Adapter<CountAdapter.ViewHolder> {
private static final String TAG = "CountAdapter";
private final List<String> mData;
private final List<CountUpTimer> mCountUpList = new ArrayList<>();
private final ArrayList<Boolean> mSelectedRows = new ArrayList<>();
private final Context mContext;
public CountAdapter(List<String> mData, Context mContext) {
this.mData = mData;
this.mContext = mContext;
mCountUpList.clear();
for (int i = 0; i < mData.size(); i++) {
mCountUpList.add(i, null);
}
mSelectedRows.clear();
for (int i = 0; i < mData.size(); i++) {
mSelectedRows.add(i, false);
}
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item, parent, false);
return new ViewHolder(v, mContext, mCountUpList, mSelectedRows);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
checkTimerStatus(holder, position);
Log.d(TAG, "onBindViewHolder: " + position + " " + String.valueOf(mCountUpList.get(position)));
}
private void checkTimerStatus(final ViewHolder holder, int position) {
CountUpTimer countUpTimer = mCountUpList.get(position);
if (countUpTimer != null) {
Log.d(TAG, "checkTimerStatus: " + position);
countUpTimer.setOnTickListener(new CountUpTimer.OnTickListener() {
#Override
public void OnTick(long elapsed) {
holder.mTimeText.setText(elapsed + "");
}
});
} else {
holder.mTimeText.setText("00:00:00");
}
boolean state = mSelectedRows.get(position);
holder.mCheckBox.setChecked(state);
}
#Override
public int getItemCount() {
return mData.size();
}
static class ViewHolder extends RecyclerView.ViewHolder implements
CountUpTimer.OnTickListener, View.OnClickListener {
private static final String TAG = "ViewHolder";
private final Context mContext;
private final ArrayList<Boolean> mSelectedRows;
private final TextView mTimeText;
private final ImageButton mStartButton;
private final CheckBox mCheckBox;
private final List<CountUpTimer> mCountUpList;
public ViewHolder(View v, Context mContext, List<CountUpTimer> mCountUp,
ArrayList<Boolean> mSelectedRows) {
super(v);
this.mContext = mContext;
this.mCountUpList = mCountUp;
this.mSelectedRows = mSelectedRows;
mTimeText = (TextView) v.findViewById(R.id.itemText);
mStartButton = (ImageButton) v.findViewById(R.id.ibPlay);
mCheckBox = (CheckBox) v.findViewById(R.id.checkBox);
mStartButton.setOnClickListener(this);
mCheckBox.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.ibPlay:
final CountUpTimer upTimer = new CountUpTimer(100);
upTimer.setOnTickListener(this);
upTimer.start();
mCountUpList.set(getAdapterPosition(), upTimer);
break;
case R.id.checkBox:
mSelectedRows.set(getAdapterPosition(),
!mSelectedRows.get(getAdapterPosition()));
break;
}
}
#Override
public void OnTick(long elapsed) {
mTimeText.setText(elapsed + "");
}
}
}
CountUpTimer.java
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
public class CountUpTimer {
private final long interval;
private long base;
public long mElapsedTime;
private OnTickListener onTickListener;
public CountUpTimer(long interval) {
this.interval = interval;
}
public void start() {
base = SystemClock.elapsedRealtime();
handler.sendMessage(handler.obtainMessage(MSG));
}
public void stop() {
handler.removeMessages(MSG);
}
public void reset() {
synchronized (this) {
base = SystemClock.elapsedRealtime();
}
}
public void setOnTickListener(OnTickListener onTickListener) {
this.onTickListener = onTickListener;
}
private static final int MSG = 1;
private Handler handler = new Handler() {
#Override
public void handleMessage(Message msg) {
synchronized (CountUpTimer.this) {
long elapsedTime = SystemClock.elapsedRealtime() - base;
if (onTickListener != null) {
mElapsedTime = elapsedTime;
onTickListener.OnTick(elapsedTime);
}
sendMessageDelayed(obtainMessage(MSG), interval);
}
}
};
interface OnTickListener {
void OnTick(long elapsed);
}
}

This might happen because you are not updating your holder.mTimeText every time in onBindViewHolder. ie your code is not reaching the statement: ,hence the view is recycling the items.
holder.mTimeText.setText(elapsed + "");
every time a view is updated
Try setting
if(countUpTimer != null) {
holder.mTimeText.setText(elapsed + "");
}
or just anything for that matter to see if it works

Related

Quiz app Randomize answers on Android studio

I bought in courses on creating android application "Quiz app". But in this course, a random order of answers is not implemented. I'm a beginner programmer without any technical education I know that I need to insert somewhere in the code
"Collections.shuffle (list)" but I can not yet figure out where. Help please. I'm making an application for students that would make life easier for them. Helping me you are helping them.
QUIZ ACTIVITY
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v4.app.FragmentManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import com.vn.iambulance.prototype_20.R;
import com.vn.iambulance.prototype_20.adapters.QuizAdapter;
import com.vn.iambulance.prototype_20.constants.AppConstants;
import com.vn.iambulance.prototype_20.data.preference.AppPreference;
import com.vn.iambulance.prototype_20.listeners.ListItemClickListener;
import com.vn.iambulance.prototype_20.models.quiz.QuizModel;
import com.vn.iambulance.prototype_20.models.quiz.ResultModel;
import com.vn.iambulance.prototype_20.utilities.ActivityUtilities;
import com.vn.iambulance.prototype_20.utilities.BeatBox;
import com.vn.iambulance.prototype_20.utilities.DialogUtilities;
import com.vn.iambulance.prototype_20.utilities.SoundUtilities;
public class QuizActivity extends BaseActivity implements DialogUtilities.OnCompleteListener {
private Activity mActivity;
private Context mContext;
private ImageButton btnSpeaker;
private Button btnNext;
private RecyclerView mRecyclerQuiz;
private TextView tvQuestionText;
private TextView tvQuestionTitle;
private ImageView imgFirstLife, imgSecondLife, imgThirdLife, imgFourthLife, imgFifthLife;
private QuizAdapter mAdapter = null;
private List<QuizModel> mItemList;
ArrayList<String> mOptionList;
ArrayList<String> mBackgroundColorList;
private int mQuestionPosition = 0;
private int mQuestionsCount = 0;
private int mScore = 0, mWrongAns = 0, mSkip = 0;
private int mLifeCounter = 5;
private boolean mUserHasPressed = false;
private boolean mIsSkipped = false, mIsCorrect = false;
private String mQuestionText, mGivenAnsText, mCorrectAnsText, mCategoryId;
private ArrayList<ResultModel> mResultList;
private BeatBox mBeatBox;
private List<SoundUtilities> mSounds;
private boolean isSoundOn;
#RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//TODO: initializeRewardedAds();
//TODO: loadRewardedVideoAds();
initVar();
initView();
loadData();
initListener();
}
private void initVar() {
mActivity = QuizActivity.this;
mContext = mActivity.getApplicationContext();
Intent intent = getIntent();
if (intent != null) {
mCategoryId = intent.getStringExtra(AppConstants.BUNDLE_KEY_INDEX);
}
mItemList = new ArrayList<>();
mOptionList = new ArrayList<>();
mBackgroundColorList = new ArrayList<>();
mResultList = new ArrayList<>();
mBeatBox = new BeatBox(mActivity);
mSounds = mBeatBox.getSounds();
}
private void initView() {
setContentView(R.layout.activity_quiz);
imgFirstLife = (ImageView) findViewById(R.id.firstLife);
imgSecondLife = (ImageView) findViewById(R.id.secondLife);
imgThirdLife = (ImageView) findViewById(R.id.thirdLife);
imgFourthLife = (ImageView) findViewById(R.id.fourthLife);
imgFifthLife = (ImageView) findViewById(R.id.fifthLife);
btnSpeaker = (ImageButton) findViewById(R.id.btnSpeaker);
btnNext = (Button) findViewById(R.id.btnNext);
tvQuestionText = (TextView) findViewById(R.id.tvQuestionText);
tvQuestionTitle = (TextView) findViewById(R.id.tvQuestionTitle);
mRecyclerQuiz = (RecyclerView) findViewById(R.id.rvQuiz);
mRecyclerQuiz.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
mAdapter = new QuizAdapter(mContext, mActivity, mOptionList, mBackgroundColorList);
mRecyclerQuiz.setAdapter(mAdapter);
initToolbar(true);
setToolbarTitle(getString(R.string.quiz));
enableUpButton();
initLoader();
}
#RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void loadData() {
showLoader();
isSoundOn = AppPreference.getInstance(mActivity).getBoolean(AppConstants.KEY_SOUND, true);
setSpeakerImage();
loadJson();
}
#RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void setSpeakerImage() {
if (isSoundOn) {
btnSpeaker.setImageResource(R.drawable.ic_speaker);
} else {
btnSpeaker.setImageResource(R.drawable.ic_speaker_not);
}
}
public void initListener() {
btnSpeaker.setOnClickListener(new View.OnClickListener() {
#RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
#Override
public void onClick(View view) {
isSoundOn = !isSoundOn;
AppPreference.getInstance(mActivity).setBoolean(AppConstants.KEY_SOUND, isSoundOn);
setSpeakerImage();
}
});
btnNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (!mUserHasPressed) {
FragmentManager manager = getSupportFragmentManager();
DialogUtilities dialog = DialogUtilities.newInstance(getString(R.string.skip_text), getString(R.string.skip_prompt), getString(R.string.yes), getString(R.string.no), AppConstants.BUNDLE_KEY_SKIP_OPTION);
dialog.show(manager, AppConstants.BUNDLE_KEY_DIALOG_FRAGMENT);
} else {
updateResultSet();
setNextQuestion();
}
}
});
mAdapter.setItemClickListener(new ListItemClickListener() {
#Override
public void onItemClick(int position, View view) {
if (!mUserHasPressed) {
int clickedAnswerIndex = position;
if (mItemList.get(mQuestionPosition).getCorrectAnswer() != -1) {
for (int currentItemIndex = 0; currentItemIndex < mOptionList.size(); currentItemIndex++) {
if (currentItemIndex == clickedAnswerIndex && currentItemIndex == mItemList.get(mQuestionPosition).getCorrectAnswer()) {
mBackgroundColorList.set(currentItemIndex, AppConstants.COLOR_GREEN);
mScore++;
mIsCorrect = true;
if (isSoundOn) {
mBeatBox.play(mSounds.get(AppConstants.BUNDLE_KEY_ZERO_INDEX));
}
} else if (currentItemIndex == clickedAnswerIndex && !(currentItemIndex == mItemList.get(mQuestionPosition).getCorrectAnswer())) {
mBackgroundColorList.set(currentItemIndex, AppConstants.COLOR_RED);
mWrongAns++;
if (isSoundOn) {
mBeatBox.play(mSounds.get(AppConstants.BUNDLE_KEY_SECOND_INDEX));
}
decreaseLifeAndStatus();
} else if (currentItemIndex == mItemList.get(mQuestionPosition).getCorrectAnswer()) {
mBackgroundColorList.set(currentItemIndex, AppConstants.COLOR_GREEN);
((LinearLayoutManager) mRecyclerQuiz.getLayoutManager()).scrollToPosition(currentItemIndex);
}
}
} else {
mBackgroundColorList.set(clickedAnswerIndex, AppConstants.COLOR_GREEN);
mScore++;
mIsCorrect = true;
mBeatBox.play(mSounds.get(AppConstants.BUNDLE_KEY_ZERO_INDEX));
}
mGivenAnsText = mItemList.get(mQuestionPosition).getAnswers().get(clickedAnswerIndex);
mCorrectAnsText = mItemList.get(mQuestionPosition).getAnswers().get(mItemList.get(mQuestionPosition).getCorrectAnswer());
mUserHasPressed = true;
mAdapter.notifyDataSetChanged();
}
}
});
}
public void decreaseLifeAndStatus() {
mLifeCounter--;
setLifeStatus();
}
void increaseLifeAndStatus() {
if (mLifeCounter < AppConstants.BUNDLE_KEY_MAX_LIFE) {
mLifeCounter++;
setLifeStatus();
}
}
public void setLifeStatus() {
switch (mLifeCounter) {
case 1:
imgFirstLife.setVisibility(View.VISIBLE);
imgSecondLife.setVisibility(View.GONE);
imgThirdLife.setVisibility(View.GONE);
imgFourthLife.setVisibility(View.GONE);
imgFifthLife.setVisibility(View.GONE);
break;
case 2:
imgFirstLife.setVisibility(View.VISIBLE);
imgSecondLife.setVisibility(View.VISIBLE);
imgThirdLife.setVisibility(View.GONE);
imgFourthLife.setVisibility(View.GONE);
imgFifthLife.setVisibility(View.GONE);
break;
case 3:
imgFirstLife.setVisibility(View.VISIBLE);
imgSecondLife.setVisibility(View.VISIBLE);
imgThirdLife.setVisibility(View.VISIBLE);
imgFourthLife.setVisibility(View.GONE);
imgFifthLife.setVisibility(View.GONE);
break;
case 4:
imgFirstLife.setVisibility(View.VISIBLE);
imgSecondLife.setVisibility(View.VISIBLE);
imgThirdLife.setVisibility(View.VISIBLE);
imgFourthLife.setVisibility(View.VISIBLE);
imgFifthLife.setVisibility(View.GONE);
break;
case 5:
imgFirstLife.setVisibility(View.VISIBLE);
imgSecondLife.setVisibility(View.VISIBLE);
imgThirdLife.setVisibility(View.VISIBLE);
imgFourthLife.setVisibility(View.VISIBLE);
imgFifthLife.setVisibility(View.VISIBLE);
break;
default:
imgFirstLife.setVisibility(View.GONE);
imgSecondLife.setVisibility(View.GONE);
imgThirdLife.setVisibility(View.GONE);
imgFourthLife.setVisibility(View.GONE);
imgFifthLife.setVisibility(View.GONE);
break;
}
}
public void setNextQuestion() {
if (isSoundOn) {
mBeatBox.play(mSounds.get(AppConstants.BUNDLE_KEY_FIRST_INDEX));
}
mUserHasPressed = false;
if (mQuestionPosition < mItemList.size() - 1 && mLifeCounter > 0) {
mQuestionPosition++;
updateQuestionsAndAnswers();
} else if (mQuestionPosition < mItemList.size() - 1 && mLifeCounter == 0) {
FragmentManager manager = getSupportFragmentManager();
DialogUtilities dialog = DialogUtilities.newInstance(getString(R.string.reward_dialog_title), getString(R.string.reward_dialog_message), getString(R.string.yes), getString(R.string.no), AppConstants.BUNDLE_KEY_REWARD_OPTION);
dialog.show(manager, AppConstants.BUNDLE_KEY_DIALOG_FRAGMENT);
} else {
ActivityUtilities.getInstance().invokeScoreCardActivity(mActivity, ScoreCardActivity.class, mQuestionsCount, mScore, mWrongAns, mSkip, mCategoryId, mResultList, true);
AppPreference.getInstance(mActivity).setQuizResult(mCategoryId, mScore);
}
}
public void updateQuestionsAndAnswers() {
mOptionList.clear();
mBackgroundColorList.clear();
((LinearLayoutManager) mRecyclerQuiz.getLayoutManager()).scrollToPosition(AppConstants.BUNDLE_KEY_ZERO_INDEX);
mOptionList.addAll(mItemList.get(mQuestionPosition).getAnswers());
mBackgroundColorList.addAll(mItemList.get(mQuestionPosition).getBackgroundColors());
mAdapter.notifyDataSetChanged();
mQuestionText = mItemList.get(mQuestionPosition).getQuestion();
tvQuestionText.setText(Html.fromHtml(mQuestionText));
tvQuestionTitle.setText(getString(R.string.quiz_question_title, mQuestionPosition + 1, mQuestionsCount));
}
public void quizActivityClosePrompt() {
FragmentManager manager = getSupportFragmentManager();
DialogUtilities dialog = DialogUtilities.newInstance(getString(R.string.exit), getString(R.string.cancel_prompt), getString(R.string.yes), getString(R.string.no), AppConstants.BUNDLE_KEY_CLOSE_OPTION);
dialog.show(manager, AppConstants.BUNDLE_KEY_DIALOG_FRAGMENT);
}
private void loadJson() {
StringBuffer sb = new StringBuffer();
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(getAssets().open(AppConstants.QUESTION_FILE)));
String temp;
while ((temp = br.readLine()) != null)
sb.append(temp);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
parseJson(sb.toString());
}
public void parseJson(String jsonData) {
try {
JSONObject jsonObjMain = new JSONObject(jsonData);
JSONArray jsonArray = jsonObjMain.getJSONArray(AppConstants.JSON_KEY_QUESTIONNAIRY);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObj = jsonArray.getJSONObject(i);
String question = jsonObj.getString(AppConstants.JSON_KEY_QUESTION);
int correctAnswer = Integer.parseInt(jsonObj.getString(AppConstants.JSON_KEY_CORRECT_ANS));
String categoryId = jsonObj.getString(AppConstants.JSON_KEY_CATEGORY_ID);
Log.d("TAG", categoryId.toString());
JSONArray jsonArray2 = jsonObj.getJSONArray(AppConstants.JSON_KEY_ANSWERS);
ArrayList<String> contents = new ArrayList<>();
ArrayList<String> backgroundColors = new ArrayList<>();
for (int j = 0; j < jsonArray2.length(); j++) {
String item_title = jsonArray2.get(j).toString();
contents.add(item_title);
backgroundColors.add(AppConstants.COLOR_WHITE);
}
if (mCategoryId.equals(categoryId)) {
mItemList.add(new QuizModel(question, contents, correctAnswer, categoryId, backgroundColors));
}
}
mQuestionsCount = mItemList.size();
Collections.shuffle(mItemList);
hideLoader();
updateQuestionsAndAnswers();
} catch (JSONException e) {
e.printStackTrace();
showEmptyView();
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
quizActivityClosePrompt();
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onBackPressed() {
quizActivityClosePrompt();
}
#Override
public void onComplete(Boolean isOkPressed, String viewIdText) {
if (isOkPressed) {
if (viewIdText.equals(AppConstants.BUNDLE_KEY_CLOSE_OPTION)) {
ActivityUtilities.getInstance().invokeNewActivity(mActivity, MainActivity.class, true);
} else if (viewIdText.equals(AppConstants.BUNDLE_KEY_SKIP_OPTION)) {
mSkip++;
mIsSkipped = true;
mGivenAnsText = getResources().getString(R.string.skipped_text);
mCorrectAnsText = mItemList.get(mQuestionPosition).getAnswers().get(mItemList.get(mQuestionPosition).getCorrectAnswer());
updateResultSet();
setNextQuestion();
} else if (viewIdText.equals(AppConstants.BUNDLE_KEY_REWARD_OPTION)) {
//TODO: mRewardedVideoAd.show();
}
} else if (!isOkPressed && viewIdText.equals(AppConstants.BUNDLE_KEY_REWARD_OPTION)) {
ActivityUtilities.getInstance().invokeScoreCardActivity(mActivity, ScoreCardActivity.class, mQuestionsCount, mScore, mWrongAns, mSkip, mCategoryId, mResultList, true);
AppPreference.getInstance(mContext).setQuizResult(mCategoryId, mScore);
AppPreference.getInstance(mContext).setQuizQuestionsCount(mCategoryId, mQuestionsCount);
}
}
public void updateResultSet() {
mResultList.add(new ResultModel(mQuestionText, mGivenAnsText, mCorrectAnsText, mIsCorrect, mIsSkipped));
mIsCorrect = false;
mIsSkipped = false;
}
#Override
protected void onDestroy() {
super.onDestroy();
mBeatBox.release();
}
}
QUIZ ADAPTER
import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;
import com.vn.iambulance.prototype_20.R;
import com.vn.iambulance.prototype_20.listeners.ListItemClickListener;
public class QuizAdapter extends RecyclerView.Adapter<QuizAdapter.ViewHolder> {
private Context mContext;
private Activity mActivity;
private ArrayList<String> mItemList;
private ArrayList<String> mColorList;
private ListItemClickListener mItemClickListener;
public QuizAdapter (Context mContext, Activity mActivity, ArrayList<String> mItemList, ArrayList<String> mColorList) {
this.mContext = mContext;
this.mActivity = mActivity;
this.mItemList = mItemList;
this.mColorList = mColorList;
}
public void setItemClickListener(ListItemClickListener itemClickListener) {
this.mItemClickListener = itemClickListener;
}
#Override
public QuizAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_quiz, parent, false);
return new QuizAdapter.ViewHolder(view, viewType, mItemClickListener);
}
public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private TextView tvItemTitle;
private CardView lytContainer;
private ListItemClickListener itemClickListener;
public ViewHolder(View itemView, int viewType, ListItemClickListener itemClickListener) {
super(itemView);
this.itemClickListener = itemClickListener;
// Find all views ids
tvItemTitle = itemView.findViewById(R.id.answer_text);
lytContainer = itemView.findViewById(R.id.card_view);
lytContainer.setOnClickListener(this);
}
#Override
public void onClick(View view) {
if (itemClickListener != null) {
itemClickListener.onItemClick(getLayoutPosition(), view);
}
}
}
#Override
public int getItemCount() {
return (null != mItemList ? mItemList.size() : 0);
}
#Override
public void onBindViewHolder(QuizAdapter.ViewHolder mainHolder, int position) {
final String model = mItemList.get(position);
final String model1 = mColorList.get(position);
// setting data over views
mainHolder.tvItemTitle.setText(Html.fromHtml(model));
mainHolder.tvItemTitle.setBackgroundResource(mActivity.getResources().getIdentifier(model1, "drawable", mActivity.getPackageName()));
}
}
QUIZ MODEL
import android.os.Parcel;
import android.os.Parcelable;
import java.util.ArrayList;
public class QuizModel implements Parcelable {
String question;
ArrayList<String> answers;
int correctAnswer;
String questinCategoryId;
ArrayList<String> backgroundColors;
public QuizModel (String question, ArrayList<String> answers, int correctAnswer, String questinCategoryId, ArrayList<String> backgroundColors) {
this.question = question;
this.correctAnswer = correctAnswer;
this.answers = answers;
this.questinCategoryId = questinCategoryId;
this.backgroundColors = backgroundColors;
}
public String getQuestion() {
return question;
}
public int getCorrectAnswer() {
return correctAnswer;
}
public ArrayList<String> getAnswers() {
return answers;
}
public String getQuestingCategoryId() {
return questinCategoryId;
}
public void setBackgroundColors(ArrayList<String> backgroundColors) {
this.backgroundColors = backgroundColors;
}
public ArrayList<String> getBackgroundColors() {
return backgroundColors;
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(question);
dest.writeList(answers);
dest.writeInt(correctAnswer);
dest.writeString(questinCategoryId);
dest.writeList(backgroundColors);
}
protected QuizModel (Parcel in) {
question = in.readString();
in.readList(answers, QuizModel.class.getClassLoader());
correctAnswer = in.readInt();
questinCategoryId = in.readString();
in.readList(backgroundColors, QuizModel.class.getClassLoader());
}
public static Creator<QuizModel> getCREATOR() {
return CREATOR;
}
public static final Creator<QuizModel> CREATOR = new Creator<QuizModel>() {
#Override
public QuizModel createFromParcel(Parcel source) {
return new QuizModel(source);
}
#Override
public QuizModel[] newArray(int size) {
return new QuizModel[size];
}
};
}
and JSON database
{
"question": "Тривале перебування в умовах спеки викликало у людини спрагу. Сигналiзацiя вiд яких рецепторiв, перш за все, зумовила її розвиток? ",
"answers": [
" Натрiєвi рецептори гiпоталамусу",
" Осморецептори печiнки",
" Глюкорецептори гiпоталамусу",
" Барорецептори дуги аорти",
" Осморецептори гiпоталамусу"
],
"correct_answer":4,
"question_category":"1"
},
Source code
https://drive.google.com/open?id=1zJ-dap6zvThs4lnj8ujwG0qTBjkT_WbD
Thank you!!!
You can place Collections.shuffle (list) directly in the constructor something like that:
public QuizModel (String question, ArrayList<String> answers, int correctAnswer, String questinCategoryId, ArrayList<String> backgroundColors) {
this.question = question;
this.correctAnswer = correctAnswer;
this.answers = answers;
Collections.shuffle(this.answers);
this.questinCategoryId = questinCategoryId;
this.backgroundColors = backgroundColors;
}
Or else you can define a separate method for this purpose:
public void shuffleAnswers() {
Collections.shuffle(this.answers);
}
And call it from any place in your code where you've instantiated QuizModel:
quizModel.shuffleAnswers();
public class QuizModel implements Parcelable {
String question;
ArrayList<String> answers;
int correctAnswer;
String questinCategoryId;
ArrayList<String> backgroundColors;
String trueAnswer;
public QuizModel (String question, ArrayList<String> answers, int correctAnswer, String questinCategoryId, ArrayList<String> backgroundColors) {
this.question = question;
this.correctAnswer = correctAnswer;
this.answers = answers;
shuffleAnswers();
this.questinCategoryId = questinCategoryId;
this.backgroundColors = backgroundColors;
this.trueAnswer = answers.get(correctAnswer);
}
private void shuffleAnswers() {
Collections.shuffle(answers);
correctAnswer = answers.indexOf(trueAnswer);
}
public boolean isCorrect(String answer) {
return trueAnswer.equals(answer);
}

GridView Always Scroll To The Top After Success Load Next Page

I face a difficulty when working using recyclerview and Gridview. The problem is when my application success load next page (next data), the recyclerview always back to the top.
I want the recyclerview start from last index.
Example : First page load 10 items, after success loan next page, the recycler view start scrolling from item 8.
For resolve that problem, i have tried all the solution on stackoverflow still get nothing.
Here are my code :
package com.putuguna.sitehinduapk.adapters;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.putuguna.sitehinduapk.R;
import com.putuguna.sitehinduapk.activities.DetailBlogPostActivity;
import com.putuguna.sitehinduapk.models.listposts.ItemPostModel;
import com.putuguna.sitehinduapk.utils.GlobalFunction;
import com.putuguna.sitehinduapk.utils.GlobalVariable;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public abstract class ListPostAdapter extends RecyclerView.Adapter<ListPostAdapter.ViewHolder>{
private List<ItemPostModel> mListPost;
private Context mContext;
public ListPostAdapter(List<ItemPostModel> mListPost, Context mContext) {
this.mListPost = mListPost;
this.mContext = mContext;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(mContext);
View view = inflater.inflate(R.layout.adapter_item_list_post, null);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(ViewHolder holder, final int position) {
final ItemPostModel post = mListPost.get(position);
List<String> listURL = extractUrls(post.getContentPosting());
Glide.with(mContext)
.load(listURL.get(0))
.into(holder.ivImagePost);
holder.tvTitle.setText(post.getTitle());
holder.llItemPost.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//TO DO OnClick
}
});
List<String> listLabel = post.getListLabel();
String labelPost="";
for(int i=0; i<listLabel.size();i++){
labelPost += "#"+listLabel.get(i) + " ";
}
holder.tvLabel.setText(labelPost);
if ((position >= getItemCount() - 1))
load();
}
public abstract void load();
#Override
public int getItemCount() {
return mListPost.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder{
public ImageView ivImagePost;
public TextView tvTitle;
public LinearLayout llItemPost;
public TextView tvLabel;
public ViewHolder(View itemView) {
super(itemView);
ivImagePost = (ImageView) itemView.findViewById(R.id.iv_image_post);
tvTitle = (TextView) itemView.findViewById(R.id.tv_blog_title);
llItemPost = (LinearLayout) itemView.findViewById(R.id.ll_item_post);
tvLabel = (TextView) itemView.findViewById(R.id.textview_label_adapter);
}
}
public static List<String> extractUrls(String input) {
List<String> result = new ArrayList<String>();
Pattern pattern = Pattern.compile(
"\\b(((ht|f)tp(s?)\\:\\/\\/|~\\/|\\/)|www.)" +
"(\\w+:\\w+#)?(([-\\w]+\\.)+(com|org|net|gov" +
"|mil|biz|info|mobi|name|aero|jobs|museum" +
"|travel|[a-z]{2}))(:[\\d]{1,5})?" +
"(((\\/([-\\w~!$+|.,=]|%[a-f\\d]{2})+)+|\\/)+|\\?|#)?" +
"((\\?([-\\w~!$+|.,*:]|%[a-f\\d{2}])+=?" +
"([-\\w~!$+|.,*:=]|%[a-f\\d]{2})*)" +
"(&(?:[-\\w~!$+|.,*:]|%[a-f\\d{2}])+=?" +
"([-\\w~!$+|.,*:=]|%[a-f\\d]{2})*)*)*" +
"(#([-\\w~!$+|.,*:=]|%[a-f\\d]{2})*)?\\b");
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
result.add(matcher.group());
}
return result;
}
}
This code of MainActivity.java
private void getListPost(){
mSwipeRefreshLayout.setRefreshing(true);
String nextPageToken = GlobalFunction.getStrings(this, GlobalVariable.keySharedPreference.TOKEN_PAGINATION);
BloggerApiService apiService = BloggerApiClient.getClient().create(BloggerApiService.class);
Call<ListPostModel> call = apiService.getListPost(GlobalVariable.APP_KEY_V3);
call.enqueue(new Callback<ListPostModel>() {
#Override
public void onResponse(Call<ListPostModel> call, Response<ListPostModel> response) {
ListPostModel listpost = response.body();
initDataView(listpost);
mSwipeRefreshLayout.setRefreshing(false);
}
#Override
public void onFailure(Call<ListPostModel> call, Throwable t) {
mSwipeRefreshLayout.setRefreshing(false);
Toast.makeText(MainActivity.this, t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
/**
* this method used for post (next page)
*/
private void getNextListPost(){
mSwipeRefreshLayout.setRefreshing(true);
String nextPageToken = GlobalFunction.getStrings(this, GlobalVariable.keySharedPreference.TOKEN_PAGINATION);
BloggerApiService apiService = BloggerApiClient.getClient().create(BloggerApiService.class);
Call<ListPostModel> call = apiService.getNexPageListPost(GlobalVariable.APP_KEY_V3,nextPageToken);
call.enqueue(new Callback<ListPostModel>() {
#Override
public void onResponse(Call<ListPostModel> call, Response<ListPostModel> response) {
ListPostModel listpost = response.body();
initDataView2(listpost);
mSwipeRefreshLayout.setRefreshing(false);
}
#Override
public void onFailure(Call<ListPostModel> call, Throwable t) {
mSwipeRefreshLayout.setRefreshing(false);
Toast.makeText(MainActivity.this, t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
#Override
public void onRefresh() {
mListPost.clear();
getListPost();
}
private void initDataView(ListPostModel listpost){
GlobalFunction.saveString(this,GlobalVariable.keySharedPreference.TOKEN_PAGINATION, listpost.getNextPageToken());
mListPost.addAll(listpost.getListItemsPost());
mPostAdapter = new ListPostAdapter(mListPost, this) {
#Override
public void load() {
ItemPostModel item = mListPost.get(mListPost.size()-1);
getNextListPost();
}
};
mRecyclerviewPost.setAdapter(mPostAdapter);
//mPostAdapter.notifyDataSetChanged();
mRecyclerviewPost.setHasFixedSize(true);
mGridViewLayoutManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
mGridViewLayoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);
mRecyclerviewPost.setLayoutManager(mGridViewLayoutManager);
}
/**
* this method used for set view (next page)
* #param listpost
*/
private void initDataView2(ListPostModel listpost){
GlobalFunction.saveString(this,GlobalVariable.keySharedPreference.TOKEN_PAGINATION, listpost.getNextPageToken());
final String nextPageToken = GlobalFunction.getStrings(this, GlobalVariable.keySharedPreference.TOKEN_PAGINATION);
List<ItemPostModel> itemNextPost = listpost.getListItemsPost();
// itemNextPost.addAll(mListPost);
mListPost.addAll(itemNextPost);
mPostAdapter = new ListPostAdapter(mListPost, this) {
#Override
public void load() {
ItemPostModel item = mListPost.get(mListPost.size()-1);
if(nextPageToken==null){
}else{
getNextListPost();
}
}
};
mRecyclerviewPost.setAdapter(mPostAdapter);
mRecyclerviewPost.setHasFixedSize(true);
mGridViewLayoutManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
mGridViewLayoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);
mRecyclerviewPost.setLayoutManager(mGridViewLayoutManager);
an
}
Any suggestion will be appreciated.
The problem with the code is, your resetting the complete data again into your adapter and setting it to RecyclerView again, That's why it is re instanced everything in RecyclerView and i.e scrolling to top. Instead of that you can try something like just add /append the updated data into the list ( where you holds the data ) and then just call the adapter.notifyDataSetChanged(); method. automatically it will add the updated data and you no need to set the recycler view again.
Probably for your case you need to change your initDataView2() mehtod like below
private void initDataView2(ListPostModel listpost){
GlobalFunction.saveString(this,GlobalVariable.keySharedPreference.TOKEN_PAGINATION, listpost.getNextPageToken());
mListPost.addAll(listpost.getListItemsPost());\
mPostAdapter .notifyDataSetChanged();
}
For nextPageToken thing you can move into your main code onCreate() or the initDataView() method where you already have the adapter initialization
like this,
private void initDataView(ListPostModel listpost){
GlobalFunction.saveString(this,GlobalVariable.keySharedPreference.TOKEN_PAGINATION, listpost.getNextPageToken());
final String nextPageToken = GlobalFunction.getStrings(this, GlobalVariable.keySharedPreference.TOKEN_PAGINATION);
mListPost.addAll(listpost.getListItemsPost());
mPostAdapter = new ListPostAdapter(mListPost, this) {
#Override
public void load() {
ItemPostModel item = mListPost.get(mListPost.size()-1);
if(nextPageToken==null){
}else{
getNextListPost();
}
}
};
mRecyclerviewPost.setAdapter(mPostAdapter);
//mPostAdapter.notifyDataSetChanged();
mRecyclerviewPost.setHasFixedSize(true);
mGridViewLayoutManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
mGridViewLayoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);
mRecyclerviewPost.setLayoutManager(mGridViewLayoutManager);
}
I hope it will work :)

Save State of RecyclerView

I am making a app with RecylerView populated with data form an online server. It has a custom layout with a favorite button that when clicked the icon changes.I have a problem trying to save state of a selected view on the RecyclerView. The RecyclerView does not save the selected state on scrolling back up. Kindly help.
Model Class
package com.smartdevelopers.kandie.nicedrawer.newsModel;
/**
* Created by 4331 on 25/09/2015.
*/
public class Latest {
public String excerpt;
public String articleImage;
public String articleUrl;
public boolean mfavourite;
public boolean isFavourite(){
return mfavourite;
}
public String getArticleUrl() {
return articleUrl;
}
public void setArticleUrl(String articleUrl) {
this.articleUrl = articleUrl;
}
public String getExcerpt() {
return excerpt;
}
public void setExcerpt(String excerpt) {
this.excerpt = excerpt;
}
public String getArticleImage() {
return articleImage;
}
public void setArticleImage(String articleImage) {
this.articleImage = articleImage;
}
}
Adapter
package com.smartdevelopers.kandie.nicedrawer.newsAdapter;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.nispok.snackbar.Snackbar;
import com.nispok.snackbar.SnackbarManager;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.smartdevelopers.kandie.nicedrawer.R;
import com.smartdevelopers.kandie.nicedrawer.ReadArticleActivity;
import com.smartdevelopers.kandie.nicedrawer.newsModel.Latest;
import com.smartdevelopers.kandie.nicedrawer.util.SharedPreferenceRecycler;
import java.util.HashMap;
import java.util.List;
/**
* Created by 4331 on 29/09/2015.
*/
public class OtherNewsAdapter extends RecyclerView.Adapter<OtherNewsAdapter.ViewHolder> {
private List<Latest> feedItemList;
private Context mContext;
private static final String TAG = "ActivityGplus";
SharedPreferenceRecycler sharedPreference = new SharedPreferenceRecycler();
public OtherNewsAdapter(Context context, List<Latest> feedItemList) {
this.feedItemList = feedItemList;
this.mContext = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row_other_news, null);
ViewHolder mh = new ViewHolder(v);
return mh;
}
#Override
public void onBindViewHolder(final ViewHolder viewHolder, int i) {
final Latest feedItem = feedItemList.get(i);
ImageLoader imageLoader = ImageLoader.getInstance();
DisplayImageOptions options = new DisplayImageOptions.Builder().cacheInMemory(true)
.cacheOnDisc(true).resetViewBeforeLoading(true)
.showImageForEmptyUri(R.drawable.placeholder)
.showImageOnFail(R.drawable.placeholder)
.showImageOnLoading(R.drawable.placeholder).build();
//download and display image from url
imageLoader.displayImage(feedItem.getArticleImage(), viewHolder.thumbnail, options);
// Glide.with(mContext).load(feedItem.getArticleImage())
// .error(R.drawable.placeholder)
// .placeholder(R.drawable.placeholder)
// .into(viewHolder.thumbnail);
viewHolder.title.setText(Html.fromHtml(feedItem.getExcerpt()));
viewHolder.articleUrl.setText(Html.fromHtml(feedItem.getArticleUrl()));
viewHolder.title.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(mContext, ReadArticleActivity.class);
intent.putExtra("articleUrl", viewHolder.articleUrl.getText().toString());
mContext.startActivity(intent);
}
});
/*If a product exists in shared preferences then set heart_red drawable
* and set a tag*/
if (checkFavoriteItem(feedItem)) {
viewHolder.favImage.setImageResource(R.drawable.heart_red);
viewHolder.favImage.setSelected(true);
viewHolder.favImage.setTag("red");
hashMap.get(i);
}
else {
viewHolder.favImage.setImageResource(R.drawable.heart_grey);
viewHolder.favImage.setSelected(false);
viewHolder.favImage.setTag("grey");
}
viewHolder.title.setTag(viewHolder);
viewHolder.thumbnail.setId(R.id.otherImage);
viewHolder.articleUrl.setTag(viewHolder);
}
#Override
public int getItemCount() {
return (null != feedItemList ? feedItemList.size() : 0);
}
#Override
public long getItemId(int position) {
return super.getItemId(position);
}
View.OnClickListener clickListener = new View.OnClickListener() {
#Override
public void onClick(View view) {
OtherNewsRowHolder holder = (OtherNewsRowHolder) view.getTag();
int position = holder.getPosition();
Latest feedItem = feedItemList.get(position);
Toast.makeText(mContext, feedItem.getExcerpt(), Toast.LENGTH_SHORT).show();
}
};
/*Checks whether a particular product exists in SharedPreferences*/
public boolean checkFavoriteItem(Latest checkProduct) {
boolean check = false;
List<Latest> favorites = sharedPreference.getFavorites(mContext);
if (favorites != null) {
for (Latest product : favorites) {
if (product.equals(checkProduct)) {
check = true;
break;
}
}
}
return check;
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
protected ImageView thumbnail, favImage;
protected TextView title,articleUrl;
public ViewHolder(View itemView) {
super(itemView);
this.thumbnail = (ImageView) itemView.findViewById(R.id.otherImage);
this.title = (TextView) itemView.findViewById(R.id.otherExcerpt);
this.articleUrl=(TextView)itemView.findViewById(R.id.otherUrl);
this.favImage = (ImageView) itemView.findViewById(R.id.imgbtn_favorite);
favImage.setOnClickListener(this);
}
#Override
public void onClick(View v) {
String tag = favImage.getTag().toString();
if(!favImage.isSelected()) {
if (tag.equalsIgnoreCase("grey")) {
sharedPreference.addFavorite(mContext, feedItemList.get(getItemCount() - 1));
// Toast.makeText(mContext, mContext.getResources().getString(R.string.add_favr),
// Toast.LENGTH_SHORT).show();
//SnackBack
SnackbarManager.show(
Snackbar.with(mContext)
.text(R.string.add_favr)
.textColor(Color.WHITE)
.color(Color.RED)
.duration(Snackbar.SnackbarDuration.LENGTH_SHORT));
//End of SnackBack
favImage.setTag("red");
favImage.setImageResource(R.drawable.heart_red);
}
}
else {
sharedPreference.removeFavorite(mContext, feedItemList.get(getItemCount() - 1));
favImage.setTag("grey");
favImage.setImageResource(R.drawable.heart_grey);
//SnackBack
SnackbarManager.show(
Snackbar.with(mContext)
.text(R.string.remove_favr)
.textColor(Color.WHITE)
.color(Color.RED) .duration(Snackbar.SnackbarDuration.LENGTH_SHORT).animation(false));
//End of SnackBack
}
}
}
}
I ran into this problem a couple of days ago - the reason is that you have not updated your feedItemList with the applicable changes. If you update the list by setting some of the member variables (like setExcerpt()) of the Latest class once it has change, your recyclerview will work properly.

notifyDataSetChanged() method not working

I am using a RecyclerView and after a user input I would like to update the RecyclerView. But there simply happens nothing.
The Layout is just refreshed when I reload the whole Fragment again.
My code:
package com.stack.overflow.fragment.Einstellungen;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.stack.overflow.HelperClasses.RecyclerView.DividerItemDecoration;
import com.stack.overflow.HelperClasses.RecyclerView.ItemClickSupport;
import com.stack.overflow.R;
import com.stack.overflow.fragment.Einstellungen.QrCode.FragmentQrCode;
import com.afollestad.materialdialogs.MaterialDialog;
import com.pixplicity.easyprefs.library.Prefs;
import java.util.ArrayList;
import java.util.List;
public class FragmentEinstellungen extends Fragment {
private static final String KEY_STUFE = "Stufe";
private static final String DEFAULT_EINTRAG_STUFE = "keine Stufe ausgewählt";
private RecyclerView mRecyclerView;
private EinstellungenDataAdapter mAdapter;
private static String[] titles = null;
private static String[] titlesStufen = null;
private static String[] titlesKlassen = null;
int callbackDialog;
String stufe = "";
public FragmentEinstellungen(){}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
titles = getActivity().getResources().getStringArray(R.array.einstellungen);
titlesStufen = getActivity().getResources().getStringArray(R.array.stufenauswahl);
titlesKlassen = getActivity().getResources().getStringArray(R.array.klassenauswahl);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.fragment_einstellungen, container, false);
mRecyclerView = (RecyclerView) layout.findViewById(R.id.einstellungenRecyclerView);
mAdapter = new EinstellungenDataAdapter(getActivity(), getData());
mRecyclerView.setAdapter(mAdapter);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
mRecyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST));
final ItemClickSupport itemClick = ItemClickSupport.addTo(mRecyclerView);
itemClick.setOnItemClickListener(new ItemClickSupport.OnItemClickListener() {
#Override
public void onItemClick(RecyclerView parent, View child, int position, long id) {
//SnackbarManager.show(
// Snackbar.with(getActivity())
// .text("KLICK"));
displayView(0);
}
});
return layout;
}
public static List<EinstellungenItem> getData() {
List<EinstellungenItem> data = new ArrayList<>();
// preparing navigation drawer items
for (int i = 0; i < titles.length; i++) {
EinstellungenItem navItem = new EinstellungenItem();
navItem.setTitle(titles[i]);
if(i==0){
navItem.setSubtitle("kein gültiger Scan");
} else if(i==1){
navItem.setSubtitle(Prefs.getString(KEY_STUFE, DEFAULT_EINTRAG_STUFE));
}
data.add(navItem);
}
return data;
}
private void displayView(int position) {
switch (position) {
case 0:
break;
case 1:
new MaterialDialog.Builder(getActivity())
.title(R.string.title_dialog_stufe)
.items(R.array.stufenauswahl)
.itemsCallbackSingleChoice(-1, new MaterialDialog.ListCallbackSingleChoice() {
#Override
public boolean onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
callbackDialog = which;
stufe = titlesStufen[which];
if (callbackDialog <= 4) {
new MaterialDialog.Builder(getActivity())
.title(R.string.title_dialog_klasse)
.items(R.array.klassenauswahl)
.itemsCallbackSingleChoice(-1, new MaterialDialog.ListCallbackSingleChoice() {
#Override
public boolean onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
stufe = stufe + titlesKlassen[which];
Prefs.putString(KEY_STUFE, stufe);
mAdapter.notifyDataSetChanged();
return true;
}
})
.positiveText(R.string.choosetext_dialog_klasse)
.show();
} else {
Prefs.putString(KEY_STUFE, stufe);
mAdapter.notifyDataSetChanged();
}
return true;
}
})
.positiveText(R.string.choosetext_dialog_stufe)
.show();
callbackDialog = 0;
break;
default:
break;
}
}
}
Am I doing the refresh at the wrong time?
Your mAdapter uses getData() which returns every time another instance of new ArrayList<>() and when you call the mAdapter.notifyDataSetChanged(); the adapter tries to refresh the old instance of the arrayList but there is no changes.
You need to save your data into a field and update that field like mArrayList.clear() and mArrayList.addAll(getData()).

android xml: Error inflating class fragment

I am having problem with fragments.I am developing an app like 360 security and i am having problem implementing fragments ,getting "Binary XML file line #1: Error inflating class fragment".
Here is my xml file.I have tried every solution please help me.
Thanks in advance.
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/item_list"
android:name="sabby.completesecurity.MainListFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ItemListActivity"
tools:layout="#android:layout/list_content" />
This is my activity file:
package sabby.completesecurity;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.FrameLayout;
import android.widget.ImageView;
import sabby.completesecurity.R;
public class MainActivityCache extends FragmentActivity implements sabby.completesecurity.MainCallbacks {
private boolean mIsDualPane;
private boolean mIsArtShowed = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_cache);
mIsDualPane = findViewById(R.id.item_detail_container) != null;
//Show an art when no fragment is showed, we make sure no detail fragment is present.
if (mIsDualPane && getFragmentManager().findFragmentByTag(sabby.completesecurity.DetailFragment.FRAGMENT_TAG) == null) {
ImageView imageView = new ImageView(this);
imageView.setImageResource(R.mipmap.ic_launcher);
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
((FrameLayout) findViewById(R.id.item_detail_container)).addView(imageView);
mIsArtShowed = true;
}
}
#Override
public void onItemSelected(String packageName) {
if (mIsDualPane) {
//Hide art when a fragment is showed.
if (mIsArtShowed) {
((FrameLayout) findViewById(R.id.item_detail_container)).removeAllViews();
mIsArtShowed = false;
}
getFragmentManager()
.beginTransaction()
.replace(R.id.item_detail_container, DetailFragment.getInstance(packageName), DetailFragment.FRAGMENT_TAG)
.commit();
} else {
Intent intent = new Intent(this, DetailActivity.class);
intent.putExtra(DetailFragment.EXTRA_PACKAGE_NAME, packageName);
startActivity(intent);
}
}
#Override
protected void onStart() {
super.onStart();
registerReceiver(receiver, getIntentFilter());
}
#Override
protected void onStop() {
super.onStop();
unregisterReceiver(receiver);
}
private void showAboutDialog() {
new AlertDialog.Builder(this)
.setTitle("About")
.setView(getLayoutInflater().inflate(R.layout.about_dialog_message, null))
.setNegativeButton(android.R.string.ok, null)
.show();
}
/**
* Used to update the list if a package is added or removed.
*/
private IntentFilter getIntentFilter() {
IntentFilter filter = new IntentFilter();
filter.addDataScheme("package");
filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
filter.addAction(Intent.ACTION_PACKAGE_REPLACED);
filter.addAction(Intent.ACTION_PACKAGE_FULLY_REMOVED);
filter.addAction(Intent.ACTION_PACKAGE_ADDED);
return filter;
}
enter code here
BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
MainListFragment mainListFragment = (MainListFragment) getFragmentManager().findFragmentById(R.id.item_list);
if (mainListFragment != null)
mainListFragment.loadList();
}
};
}
MainListFragment.java
package sabby.completesecurity;
import android.app.ActionBar;
import android.app.Activity;
import android.app.ListFragment;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.IPackageStatsObserver;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageStats;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.RemoteException;
import android.text.format.Formatter;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.SectionIndexer;
import android.widget.Spinner;
import android.widget.TextView;
import sabby.completesecurity.utils.Utils;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
public class MainListFragment extends ListFragment implements AdapterView.OnItemClickListener, AdapterView.OnItemSelectedListener {
private static final int SORT_NAME = 0;
private static final int SORT_PKG = 1;
private static final int SORT_DOMAIN = 2;
private static final int SORT_INSTALLATION = 3;
private static final int SORT_SIZE = 4;
private static final String INSTANCE_STATE_SORT_BY = "sort_by";
private Adapter mAdapter;
private List<Item> mItemList = new ArrayList<Item>();
private int mOnSizeFinishedItemCount;
private PackageManager mPackageManager;
private ProgressDialog mProgressDialog;
private LayoutInflater mLayoutInflater;
private MainCallbacks mCallbacks;
private Context mContext;
private Async mAsyncLoader;
private Spinner mSpinner;
private boolean mSpinnerListenerAuthorized;
private SimpleDateFormat mSimpleDateFormat;
private int mSortBy = 0;
class Item {
ApplicationInfo applicationInfo;
String label;
Long date;
Long size = -1L;
}
private int mColorGrey1;
private int mColorGrey2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
mProgressDialog = new ProgressDialog(mContext);
mProgressDialog.setTitle(R.string.loading_apps);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setCancelable(false);
//Used to prevent message not showing later
mProgressDialog.setMessage("");
mPackageManager = mContext.getPackageManager();
mSimpleDateFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
mColorGrey1 = getResources().getColor(R.color.grey_1);
mColorGrey2 = getResources().getColor(R.color.grey_2);
ActionBar actionBar = getActivity().getActionBar();
actionBar.setDisplayShowCustomEnabled(true);
mSpinner = new Spinner(actionBar.getThemedContext());
SpinnerAdapter spinnerAdapter = new SpinnerAdapter(actionBar.getThemedContext(),
R.array.sort_spinner_items, android.R.layout.simple_list_item_1);
mSpinner.setAdapter(spinnerAdapter);
mSpinnerListenerAuthorized = false;
mSpinner.setOnItemSelectedListener(this);
ActionBar.LayoutParams layoutParams = new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
actionBar.setCustomView(mSpinner, layoutParams);
if (savedInstanceState != null)
setSortBy(savedInstanceState.getInt(INSTANCE_STATE_SORT_BY, -1), false);
}
#Override
public void onStart() {
super.onStart();
mSpinner.setSelection(mSortBy);
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(INSTANCE_STATE_SORT_BY, mSortBy);
}
private void onTaskEnded(List<Item> list) {
RetainedFragment retainedFragment = (RetainedFragment) getFragmentManager().findFragmentByTag(RetainedFragment.FRAGMENT_TAG);
retainedFragment.setList(list);
mItemList = list;
mAdapter.notifyDataSetChanged();
if (getListView().getAdapter() == null)
setListAdapter(mAdapter);
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
getListView().setOnItemClickListener(this);
getListView().setFastScrollEnabled(true);
mAdapter = new Adapter();
RetainedFragment retainedFragment = (RetainedFragment) getFragmentManager()
.findFragmentByTag(RetainedFragment.FRAGMENT_TAG);
if (retainedFragment == null) {
retainedFragment = new RetainedFragment();
getFragmentManager()
.beginTransaction()
.add(retainedFragment, RetainedFragment.FRAGMENT_TAG)
.commit();
}
if (retainedFragment.getList() != null) {
onTaskEnded(retainedFragment.getList());
mOnSizeFinishedItemCount = mItemList.size();
//Notify spinner that size sort is available
SpinnerAdapter adapter = (SpinnerAdapter) mSpinner.getAdapter();
adapter.notifyDataSetChanged();
} else
loadList();
}
public void loadList() {
mAsyncLoader = new Async();
mAsyncLoader.execute();
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
mCallbacks = (MainCallbacks) activity;
mContext = activity;
mLayoutInflater = activity.getLayoutInflater();
}
#Override
public void onDetach() {
super.onDetach();
if (mAsyncLoader != null)
mAsyncLoader.cancel(true);
mCallbacks = null;
mContext = null;
mLayoutInflater = null;
}
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
if (mCallbacks != null)
mCallbacks.onItemSelected(mItemList.get(i).applicationInfo.packageName);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.fragment_main_list, menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_refresh:
loadList();
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
if (mSpinnerListenerAuthorized)
setSortBy(i, true);
mSpinnerListenerAuthorized = true;
}
/**
* Sort main list if provided value is valid.
* #param sort Must be one of SORT_*
* #param checkViews Set if views have to be updated, eg. when restoring state, views aren't
* created yet, so value must be false
*/
public void setSortBy(int sort, boolean checkViews) {
if (sort >= SORT_NAME && sort <= SORT_SIZE) {
mSortBy = sort;
if (checkViews) {
checkFastScroll();
sortApplicationList(mItemList, mSortBy);
mAdapter.notifyDataSetChanged();
}
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
private void checkFastScroll() {
getListView().setFastScrollEnabled(mSortBy == SORT_NAME);
}
public void sortApplicationList(List<Item> list, final int sortBy) {
Collections.sort(list, new Comparator<Item>() {
#Override
public int compare(Item item1, Item item2) {
switch (sortBy) {
case SORT_NAME:
return item1.label.compareTo(item2.label);
case SORT_PKG:
return item1.applicationInfo.packageName.compareTo(item2.applicationInfo.packageName);
case SORT_DOMAIN:
boolean isSystem1 = (item1.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0;
boolean isSystem2 = (item2.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0;
return Utils.compareBooleans(isSystem1, isSystem2);
case SORT_INSTALLATION:
//Sort in decreasing order
return -item1.date.compareTo(item2.date);
case SORT_SIZE:
return -item1.size.compareTo(item2.size);
default:
return 0;
}
}
});
}
/**
* This method is called by each item when it has finished retrieving its size
* When all items have finished, we set size sort available in spinner, and invalidate
* main list to display sizes in UI.
*/
private void onItemFinishedSizeProcess() {
mOnSizeFinishedItemCount ++;
if (mOnSizeFinishedItemCount == mItemList.size()) {
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
SpinnerAdapter adapter = (SpinnerAdapter) mSpinner.getAdapter();
adapter.notifyDataSetChanged();
mAdapter.notifyDataSetChanged();
}
});
}
}
class Adapter extends BaseAdapter implements SectionIndexer {
class ViewHolder {
ImageView icon;
TextView label;
TextView packageName;
TextView version;
TextView isSystemApp;
TextView date;
TextView size;
IconAsyncTask iconLoader;
}
String sections = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
#Override
public int getCount() {
return mItemList.size();
}
#Override
public Object getItem(int i) {
return mItemList.get(i);
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder holder;
if (view == null) {
view = mLayoutInflater.inflate(R.layout.main_list_item, null);
holder = new ViewHolder();
holder.icon = (ImageView) view.findViewById(R.id.icon);
holder.label = (TextView) view.findViewById(R.id.label);
holder.packageName = (TextView) view.findViewById(R.id.packageName);
holder.version = (TextView) view.findViewById(R.id.version);
holder.isSystemApp = (TextView) view.findViewById(R.id.isSystem);
holder.date = (TextView) view.findViewById(R.id.date);
holder.size = (TextView) view.findViewById(R.id.size);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
holder.iconLoader.cancel(true);
}
view.setBackgroundColor(i % 2 == 0 ? mColorGrey2 : mColorGrey1);
Item item = mItemList.get(i);
ApplicationInfo info = item.applicationInfo;
try {
PackageInfo packageInfo = mPackageManager.getPackageInfo(info.packageName, 0);
holder.version.setText(packageInfo.versionName);
Date date = new Date(packageInfo.firstInstallTime);
holder.date.setText(mSimpleDateFormat.format(date));
} catch (PackageManager.NameNotFoundException e) {
//Do nothing
}
holder.iconLoader = new IconAsyncTask(holder.icon, info);
holder.iconLoader.execute();
holder.label.setText(info.loadLabel(mPackageManager));
holder.packageName.setText(info.packageName);
boolean isSystemApp = (info.flags & ApplicationInfo.FLAG_SYSTEM) != 0;
holder.isSystemApp.setText(isSystemApp ? getString(R.string.system) : getString(R.string.user));
if (item.size != -1L)
holder.size.setText(Formatter.formatFileSize(getActivity(), item.size));
return view;
}
#Override
public int getPositionForSection(int section) {
for (int i = 0; i < this.getCount(); i++) {
String item = mItemList.get(i).label;
if (item.charAt(0) == sections.charAt(section))
return i;
}
return 0;
}
#Override
public int getSectionForPosition(int i) {
return 0;
}
#Override
public Object[] getSections() {
String[] sectionsArr = new String[sections.length()];
for (int i = 0; i < sections.length(); i++)
sectionsArr[i] = "" + sections.charAt(i);
return sectionsArr;
}
class IconAsyncTask extends AsyncTask<Void, Integer, Drawable> {
ImageView imageView;
ApplicationInfo info;
IconAsyncTask(ImageView imageView, ApplicationInfo info) {
this.imageView = imageView;
this.info = info;
}
#Override
protected void onPreExecute() {
super.onPreExecute();
imageView.setVisibility(View.INVISIBLE);
}
#Override
protected Drawable doInBackground(Void... voids) {
if (!isCancelled())
return info.loadIcon(mPackageManager);
return null;
}
#Override
protected void onPostExecute(Drawable drawable) {
super.onPostExecute(drawable);
imageView.setImageDrawable(drawable);
imageView.setVisibility(View.VISIBLE);
}
}
}
class SpinnerAdapter extends BaseAdapter {
private Context mContext;
private int mLayoutResId;
private String[] mItems;
public SpinnerAdapter(Context themedContext, int arrayResId, int layoutResId) {
mContext = themedContext;
mItems = themedContext.getResources().getStringArray(arrayResId);
mLayoutResId = layoutResId;
}
#Override
public int getCount() {
return mItems.length;
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
//It make no sense to implement recycled view system because there is only 5 items in list
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = View.inflate(mContext, mLayoutResId, null);
if (view instanceof TextView)
((TextView) view).setText(mItems[i]);
return view;
}
/**
* Set sort_by_size item disabled if all items haven't retrieved them size.
*/
#Override
public boolean isEnabled(int position) {
return position != SORT_SIZE || mItemList != null && mOnSizeFinishedItemCount == mItemList.size();
}
}
class Async extends AsyncTask<Void, Async.Progress, List<Item>> {
class Progress {
String label;
int totalSize;
}
#Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog.show();
}
#Override
protected List<Item> doInBackground(Void... voids) {
List<ApplicationInfo> applicationInfos = mPackageManager.getInstalledApplications(PackageManager.GET_META_DATA);
Progress progress = new Progress();
progress.totalSize = applicationInfos.size();
List<Item> itemList = new ArrayList<Item>(applicationInfos.size());
mOnSizeFinishedItemCount = 0;
for (ApplicationInfo applicationInfo : applicationInfos) {
if (isCancelled())
break;
Item item = new Item();
item.applicationInfo = applicationInfo;
String label = applicationInfo.loadLabel(mPackageManager).toString();
item.label = label;
try {
item.date = mPackageManager.getPackageInfo(applicationInfo.packageName, 0).firstInstallTime;
} catch (PackageManager.NameNotFoundException e) {
item.date = 0L;
}
itemList.add(item);
getItemSize(item);
progress.label = label;
publishProgress(progress);
}
sortApplicationList(itemList, mSortBy);
return itemList;
}
private void getItemSize(final Item item) {
try {
Method getPackageSizeInfo = mPackageManager.getClass().getMethod(
"getPackageSizeInfo", String.class, IPackageStatsObserver.class);
getPackageSizeInfo.invoke(mPackageManager, item.applicationInfo.packageName, new IPackageStatsObserver.Stub() {
#Override
public void onGetStatsCompleted(final PackageStats pStats, boolean succeeded)
throws RemoteException {
if (succeeded)
item.size = pStats.codeSize + pStats.cacheSize + pStats.dataSize
+ pStats.externalCodeSize + pStats.externalCacheSize + pStats.externalDataSize
+ pStats.externalMediaSize + pStats.externalObbSize;
else
item.size = -1L;
onItemFinishedSizeProcess();
}
});
} catch (NoSuchMethodException e) {
e.printStackTrace();
onItemFinishedSizeProcess();
} catch (IllegalAccessException e) {
e.printStackTrace();
onItemFinishedSizeProcess();
} catch (InvocationTargetException e) {
e.printStackTrace();
onItemFinishedSizeProcess();
}
}
#Override
protected void onProgressUpdate(Progress... values) {
super.onProgressUpdate(values);
Progress progress = values[0];
mProgressDialog.setMessage(progress.label);
if (mProgressDialog.getMax() == 100)
mProgressDialog.setMax(progress.totalSize);
mProgressDialog.incrementProgressBy(1);
}
#Override
protected void onPostExecute(List<Item> list) {
super.onPostExecute(list);
mProgressDialog.hide();
onTaskEnded(list);
}
#Override
protected void onCancelled(List<Item> list) {
super.onCancelled(list);
mProgressDialog.hide();
}
}
}
In the xml that you have posted,replace the TAG <Fragment> with <LinearLayout> or <RelativeLayout> and inside this you simply show what you want to display in you UI
Make sure you extend Fragment class in you fragment activity
Could you post the code for your fragment? The problem could be with your import statements. You have 'import android.support.v4.app.FragmentActivity'. Do you have 'import android.support.v4.app.Fragment' in your fragment file?

Categories

Resources