// this is my QuestionFragment_Activity class where i will get questions from server
public class QuestionFragment_Activity extends Fragment implements FragmentObserver {
private static final String CATEGORY_ID = "question_fragment_category_id";
private static final String QUESTIONS = "question_fragment_questions";
private RecyclerView recyclerView;
private QuestionAdapter adapter;
private JSONArray questions = null;
private int categoryId;
private Button button;
public QuestionFragment_Activity() { }
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
try {
questions = new JSONArray(savedInstanceState.getSerializable(QUESTIONS));
} catch (JSONException je) { }
}
if (questions == null) {
categoryId = (int) getArguments().getSerializable(CATEGORY_ID);
try {
// this is request to the server
questions = Resource.getInstance().getQuestions(categoryId);
JSONObject request = new JSONObject();
request.put("_module", "Category");
JSONObject payload = new JSONObject();
payload.put("_task", "getquestions");
payload.put("_uhid",2);
payload.put("_pid", categoryId);
request.put("_payload", payload);
new Service(this).sendRequest(request);
} catch (JSONException je) {
}
//questions = Resource.getInstance().getQuestions(categoryId);
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if (questions != null)
outState.putString(QUESTIONS, questions.toString());
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_question, container, false);
button = (Button) view.findViewById(R.id.button);
recyclerView = (RecyclerView) view.findViewById(R.id.question_recycler);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getContext(),"in side click",Toast.LENGTH_SHORT).show();
}
});
updateUI();
return view;
}
@Override
public void onResume() {
super.onResume();
updateUI();
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public void onSuccess(JSONObject response) {
try {
questions = response.getJSONArray("_questions");
updateUI();
} catch (JSONException je) { }
}
@Override
public Context getContext() {
return getActivity();
}
@Override
public void onFailure(String message) {
}
public static QuestionFragment_Activity newInstance(int categoryId) {
Bundle args = new Bundle();
args.putSerializable(CATEGORY_ID, categoryId);
QuestionFragment_Activity questionFragment = new QuestionFragment_Activity();
questionFragment.setArguments(args);
return questionFragment;
}
public void updateUI() {
if(adapter == null) {
if(questions != null) {
adapter = new QuestionAdapter(questions);
recyclerView.setAdapter(adapter);
recyclerView.setItemViewCacheSize(questions.length());
}
}
}
// this is my QuestionHolder class
private class QuestionHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private JSONObject question;
private CheckBox checkBox;
private TextView question_txt;
public QuestionHolder(final LayoutInflater inflater, final ViewGroup parent) {
super(inflater.inflate(R.layout.question_item, parent, false));
checkBox = (CheckBox) itemView.findViewById(R.id.question_checkbox);
question_txt = (TextView) itemView.findViewById(R.id.question);
itemView.setOnClickListener(this);
}
public void bind(JSONObject question) {
this.question = question;
try {
question_txt.setText(question.getString("_question"));
} catch (JSONException je) { }
}
@Override
public void onClick(View v) {
}
}
// this my QuestionAdapter Class
private class QuestionAdapter extends RecyclerView.Adapter<QuestionHolder> {
SparseBooleanArray sparseBooleanArray = new SparseBooleanArray();
private JSONArray questions;
public QuestionAdapter(JSONArray questions) {
this.questions = questions;
}
@Override
public QuestionHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new QuestionHolder(LayoutInflater.from(getActivity()), parent);
}
// this is my onBindViewHolder where I am using one SparseBooleanArray for selecting
@Override
public void onBindViewHolder(final QuestionHolder holder, final int position) {
try {
final JSONObject question = questions.getJSONObject(position);
holder.checkBox.setOnCheckedChangeListener(null);
holder.checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
sparseBooleanArray.put(position,true);
Toast.makeText(getActivity(),""+question.get("_id"), Toast.LENGTH_SHORT).show();
} catch (JSONException e) { }
/* int pos= holder.getAdapterPosition();
final boolean newvalue = !holder.checkBox.isChecked();
sparseBooleanArray.put(pos,newvalue);
getselectedItemspositions();
// sparseBooleanArray.put(pos,true);
Toast.makeText(getContext(),"Item favoued"+getselectedItemspositions(),Toast.LENGTH_LONG).show();*/
//Toast.makeText(getContext(),"Item favoued"+v,Toast.LENGTH_LONG).show();
// sparseBooleanArray.put(position,true);
}
});
holder.bind(question);
} catch (JSONException je) { }
}
@Override
public int getItemCount() {
return questions.length();
}
}
}
Aucun commentaire:
Enregistrer un commentaire