i use RecyclerViewAdapter for my json adaper.i make some radiobutton and check box and edittext in RecyclerViewAdapter programically but when i check some radio button or type in edittext and scroll more than 1 screen when i come back all my fill content are removed;
public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> {
public MyRecyclerViewAdapter(Context context, ArrayList<HashMap<String, Object>> hash_all) {
}
#Override
public int getItemCount() {
return hash_all.size();
}
#SuppressLint("SetTextI18n")
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
hash_all.size();
hash_get = hash_all.get(position);
LinearLayoutCompat.LayoutParams params = new LinearLayoutCompat.LayoutParams(
LinearLayoutCompat.LayoutParams.MATCH_PARENT,
LinearLayoutCompat.LayoutParams.WRAP_CONTENT
);
params.setMargins(0, 0, 0, 0);
String s = hash_get.get("ModelShowOption").toString();
String toggle = "";
String direc = "";
String type = "";
String model = "";
String rows = "";
String resize = "";
String[] split = s.split("\\|");
String[] split2 = new String[0];
for (int i = 0; i < split.length; i++) {
String s2 = split[i];
split2 = s2.split(":");
if (split2[0].equals("isToggleButton")){
toggle=split2[1];
}else if (split2[0].equals("type")){
type=split2[1];
}else if (split2[0].equals("direction")){
direc=split2[1];
}else if (split2[0].equals("model")){
model=split2[1];
}else if (split2[0].equals("rows")){
rows=split2[1];
}else if (split2[0].equals("resize")){
resize=split2[1];
}
}
String title = hash_get.get("title").toString();
String myValue =hash_get.get("MyValue").toString();
String[] splitValue = myValue.split("\\|");
RadioGroup[] rg;
RadioButton[] rb;
if (toggle.equals("true") && holder.row_list_title.getText().equals("")){
holder.row_list_placeholder.setVisibility(View.GONE);
holder.row_list_buttn.setVisibility(View.GONE);
String multiValue = hash_get.get("MultiValue").toString();
String[] splitMultiValue = multiValue.split(":");
if (splitMultiValue[1].equals("1")){
int p = 0;
rg = new RadioGroup[1];
rb = new RadioButton[splitValue.length];
rg[0] = new RadioGroup(holder.row_list_placeholder.getContext());
rg[0].setOrientation(RadioGroup.VERTICAL);
for (int j = 0; j < splitValue.length; j++) {
String myValue2 = splitValue[j];
String[] splitValue2 = myValue2.split("=");
rb[j] = new RadioButton(getApplicationContext());
rg[p].addView(rb[j]);
rb[j].setId(j);
rb[j].setText(splitValue2[0]);
}
holder.linearLayoutCheckBox.addView(rg[p]);
p++;
holder.row_list_title.setText(title);
if (rg[0].getCheckedRadioButtonId() == -1) {
} else {
for (int r = 0; r < splitValue.length; r++) {
if (rb[r].isChecked()) {
RadioButton id = (RadioButton) findViewById(rb[r].getId());
String radioText = id.getText().toString();
}
}
}
}else{
holder.row_list_title.setText(title);
CheckBox[] ch;
final int[] checkd = {0};
List<String> checkvl = new ArrayList<>();
ch = new CheckBox[splitValue.length];
for (int j = 0; j < splitValue.length; j++) {
String myValue2 = splitValue[j];
String[] splitValue2 = myValue2.split("=");
ch[j] = new CheckBox(getApplicationContext());
holder.linearLayoutCheckBox.addView(ch[j]);
ch[j].setId(j);
ch[j].setText(splitValue2[0]);
}
for (int r = 0; r < splitValue.length; r++) {
int finalR = r;
int finalR1 = r;
ch[r].setOnCheckedChangeListener((buttonView, isChecked) -> {
if (isChecked) {
if (checkd[0] < Integer.parseInt(splitMultiValue[1])){
//checkvl.add(ch[finalR1].getText().toString());
//hash_all_string.get(position).remove("value");
//hash_all_string.get(position).put("value", String.valueOf(checkvl));
//CheckBox id = findViewById(ch[finalR].getId());
//String CheckBoxText = id.getText().toString();
//Log.e("CheckBoxString", CheckBoxText);
checkd[0] = checkd[0] +1;
}else{
ch[finalR].setChecked(false);
}
} else {
ch[finalR].setChecked(false);
checkd[0] = checkd[0] -1;
//checkvl.remove(ch[finalR1].getText().toString());
//hash_all_string.get(position).remove("value");
//hash_all_string.get(position).put("value", String.valueOf(checkvl));
}
});
}
}
}else{
if (myValue.equals("NULL")){
holder.row_list_title.setText(title);
holder.row_list_placeholder.setVisibility(View.VISIBLE);
holder.row_list_buttn.setVisibility(View.GONE);
holder.row_list_placeholder.setHint(hash_get.get("placeholder").toString());
if (type.equals("Text")){
holder.row_list_placeholder.setInputType(InputType.TYPE_CLASS_TEXT);
}else if(type.equals("Int")){
holder.row_list_placeholder.setInputType(InputType.TYPE_CLASS_NUMBER);
}else if(type.equals("Float")){
holder.row_list_placeholder.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
}
}else {
holder.row_list_placeholder.setVisibility(View.GONE);
holder.row_list_buttn.setVisibility(View.VISIBLE);
holder.row_list_title.setText(title);
int selct = 0;
for (int j = 0; j < hash_all2.size(); j++) {
if (hash_all2.get(j).get("selected").equals("true")) {
selct = selct+1;
}
}
if (selct>0){
holder.row_list_buttn.setBackgroundColor(getResources().getColor(R.color.purple_500));
holder.row_list_buttn.setText("انتخاب شد");
}else{
holder.row_list_buttn.setText("انتخاب کنید");
holder.row_list_buttn.setBackgroundColor(getResources().getColor(R.color.teal_200));
}
String multiValue = hash_get.get("MultiValue").toString();
String[] splitMultiValue = multiValue.split(":");
holder.row_list_buttn.setOnClickListener(v -> {
if (hash_all2.size() == 0) {
for (int j = 0; j < splitValue.length; j++) {
HashMap<String, String> hash_add2 = new HashMap<>();
String myValue2 = splitValue[j];
String[] splitValue2 = myValue2.split("=");
hash_add2.put("Id", String.valueOf(j));
hash_add2.put("name", String.valueOf(splitValue2[0]));
hash_add2.put("value", String.valueOf(splitValue2[1]));
hash_add2.put("selected", "false");
hash_add2.put("max", splitMultiValue[1]);
hash_all2.add(hash_add2);
}
adapt.notifyDataSetChanged();
}
alertDialogSelect(position);
});
}
}
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.row_form, parent, false);
return new ViewHolder(view);
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView row_list_title;
EditText row_list_placeholder;
LinearLayout linearLayoutCheckBox;
Button row_list_buttn;
ViewHolder(View itemView) {
super(itemView);
row_list_title = itemView.findViewById(R.id.row_list_title);
row_list_placeholder = itemView.findViewById(R.id.row_list_placeholder);
linearLayoutCheckBox = itemView.findViewById(R.id.mainrow);
row_list_buttn = itemView.findViewById(R.id.row_list_buttn);
}
}
}
please help.i tryed many code from stack but not working
holder.setIsRecyclable(false);
setHasStableIds(true);
this is not working for me
In RecyclerView when scrolling state of view removed and creating new view by scroll,
you must change your data when radioButton or edittext content changed, and set new data in your model.
I am getting incorrect id for child item click.
Fragment Class
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View itemView = inflater.inflate(R.layout.fragment_checklist_groups_description, container, false);
fragmentManager = getActivity().getSupportFragmentManager();
mLinearListView = (LinearLayout) itemView.findViewById(R.id.linear_ListView);
//=========================================================================
for (int i = 0; i < 2; i++) {
V_ChecklistParentItemModel v_checklistParentItemModel = new V_ChecklistParentItemModel();
v_checklistParentItemModel.setParentGroupID("" + (i + 1));
v_checklistParentItemModel.setParentGroupName("Group Name " + i);
if (i == 0) {
v_checklistParentItemModel.setHasSubGroup(false);
ArrayList<V_ChecklistChildItemModel> tempV_checklistChildItemModelArrayList = new ArrayList<>();
for (int j = 0; j < 3; j++) {
V_ChecklistChildItemModel v_checklistChildItemModel = new V_ChecklistChildItemModel();
v_checklistChildItemModel.setChildItemQuestionID("" + (j + 1));
v_checklistChildItemModel.setChildItemQuestionName("Description of Question " + (j + 1));
v_checklistChildItemModel.setChildQuestionID("" + index);
index++;
tempV_checklistChildItemModelArrayList.add(v_checklistChildItemModel);
}
v_checklistParentItemModel.setV_checklistChildItemModelArrayList(tempV_checklistChildItemModelArrayList);
} else {
v_checklistParentItemModel.setHasSubGroup(true);
ArrayList<V_ChecklistSubGroupModel> tempV_checklistSubGroupModelArrayList = new ArrayList<>();
for (int j = 0; j < 2; j++) {
V_ChecklistSubGroupModel v_checklistSubGroupModel = new V_ChecklistSubGroupModel();
v_checklistSubGroupModel.setSubGroupID("" + (j + 1));
if (j == 0) {
v_checklistSubGroupModel.setSubGroupName("Sub Group Name 2a");
ArrayList<V_ChecklistChildItemModel> tempV_checklistChildItemModelArrayList = new ArrayList<>();
for (int k = 0; k < 2; k++) {
V_ChecklistChildItemModel v_checklistChildItemModel = new V_ChecklistChildItemModel();
v_checklistChildItemModel.setChildItemQuestionID("" + (k + 1));
v_checklistChildItemModel.setChildItemQuestionName("Description of Question " + (k + 1));
v_checklistChildItemModel.setChildQuestionID("" + index);
index++;
tempV_checklistChildItemModelArrayList.add(v_checklistChildItemModel);
}
v_checklistSubGroupModel.setV_checklistChildItemModelArrayList(tempV_checklistChildItemModelArrayList);
} else {
v_checklistSubGroupModel.setSubGroupName("Sub Group Name 2b");
ArrayList<V_ChecklistChildItemModel> tempV_checklistChildItemModelArrayList = new ArrayList<>();
for (int k = 0; k < 3; k++) {
V_ChecklistChildItemModel v_checklistChildItemModel = new V_ChecklistChildItemModel();
v_checklistChildItemModel.setChildItemQuestionID("" + (k + 1));
v_checklistChildItemModel.setChildItemQuestionName("Description of Question " + (k + 1));
v_checklistChildItemModel.setChildQuestionID("" + index);
index++;
tempV_checklistChildItemModelArrayList.add(v_checklistChildItemModel);
}
v_checklistSubGroupModel.setV_checklistChildItemModelArrayList(tempV_checklistChildItemModelArrayList);
}
tempV_checklistSubGroupModelArrayList.add(v_checklistSubGroupModel);
}
v_checklistParentItemModel.setV_checklistSubGroupModelArrayList(tempV_checklistSubGroupModelArrayList);
}
v_checklistParentItemModelArrayList.add(v_checklistParentItemModel);
}
//=========================================================================
//Adds data into first row
for (int i = 0; i < v_checklistParentItemModelArrayList.size(); i++) {
Log.v("I : ", " " + i);
LayoutInflater listInflater = null;
listInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View mLinearView = listInflater.inflate(R.layout.custom_cardview_checklist_groups_description_main_parent_item, null);
final TextView mProductName = (TextView) mLinearView.findViewById(R.id.textViewName);
final RelativeLayout mImageArrowFirst = (RelativeLayout) mLinearView.findViewById(R.id.rlFirstArrow);
final LinearLayout mLinearScrollSecond = (LinearLayout) mLinearView.findViewById(R.id.linear_scroll);
//checkes if menu is already opened or not
if (isFirstViewClick == false) {
mLinearScrollSecond.setVisibility(View.GONE);
mImageArrowFirst.setBackgroundResource(R.drawable.next_disable_icon);
} else {
mLinearScrollSecond.setVisibility(View.VISIBLE);
mImageArrowFirst.setBackgroundResource(R.drawable.arw_down);
}
//Handles onclick effect on list item
mImageArrowFirst.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (isFirstViewClick == false) {
isFirstViewClick = true;
mImageArrowFirst.setBackgroundResource(R.drawable.arw_down);
mLinearScrollSecond.setVisibility(View.VISIBLE);
} else {
isFirstViewClick = false;
mImageArrowFirst.setBackgroundResource(R.drawable.next_disable_icon);
mLinearScrollSecond.setVisibility(View.GONE);
}
return false;
}
});
final String name = v_checklistParentItemModelArrayList.get(i).getParentGroupName();
mProductName.setText(name);
if (v_checklistParentItemModelArrayList.get(i).isHasSubGroup()) {
//Adds data into second row
for (int j = 0; j < v_checklistParentItemModelArrayList.get(i).getV_checklistSubGroupModelArrayList().size(); j++) {
Log.v("J : ", " " + j);
LayoutInflater inflater2 = null;
inflater2 = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View mLinearView2 = inflater2.inflate(R.layout.custom_cardview_checklist_groups_description_child_parent_item, null);
TextView mSubItemName = (TextView) mLinearView2.findViewById(R.id.textViewTitle);
final RelativeLayout mLinearSecondArrow = (RelativeLayout) mLinearView2.findViewById(R.id.linearSecond);
final RelativeLayout mImageArrowSecond = (RelativeLayout) mLinearView2.findViewById(R.id.rlSecondArrow);
final LinearLayout mLinearScrollThird = (LinearLayout) mLinearView2.findViewById(R.id.linear_scroll_third);
final LinearLayout linearLayoutMain = (LinearLayout) mLinearView2.findViewById(R.id.llMain);
if (i == 0) {
mLinearSecondArrow.setBackgroundColor(getResources().getColor(R.color.main_parent));
mImageArrowSecond.setVisibility(View.GONE);
linearLayoutMain.setPadding(20, 0, 0, 0);
} else {
mLinearSecondArrow.setBackgroundColor(getResources().getColor(R.color.child_parent));
mImageArrowSecond.setVisibility(View.VISIBLE);
if (i == 1) {
linearLayoutMain.setPadding(20, 8, 0, 0);
} else {
linearLayoutMain.setPadding(15, 8, 0, 0);
}
}
//checkes if menu is already opened or not
if (isSecondViewClick == false) {
mLinearScrollThird.setVisibility(View.GONE);
mImageArrowSecond.setBackgroundResource(R.drawable.next_disable_icon);
} else {
mLinearScrollThird.setVisibility(View.VISIBLE);
mImageArrowSecond.setBackgroundResource(R.drawable.arw_down);
}
//Handles onclick effect on list item
if (i == 0) {
final int finalI1 = i;
final int finalJ1 = j;
mLinearSecondArrow.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
Bundle bundle = new Bundle();
bundle.putString("groupName", v_checklistParentItemModelArrayList.get(finalI1).getParentGroupName());
bundle.putInt("itemCount", 3);
bundle.putSerializable("alldata", checklist_groupNamePojoArrayList);
pageNumber = 0;
bundle.putInt("pageNumber", pageNumber);
ChecklistGroupsQuestionsMainDialogFragment checklistGroupsQuestionsMainDialogFragment = new ChecklistGroupsQuestionsMainDialogFragment();
checklistGroupsQuestionsMainDialogFragment.setArguments(bundle);
checklistGroupsQuestionsMainDialogFragment.setTargetFragment(current, 101);
checklistGroupsQuestionsMainDialogFragment.show(fragmentManager, getResources().getString(R.string.sd_project_list_screen_name));
return false;
}
});
} else {
mImageArrowSecond.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (isSecondViewClick == false) {
isSecondViewClick = true;
mImageArrowSecond.setBackgroundResource(R.drawable.arw_down);
mLinearScrollThird.setVisibility(View.VISIBLE);
} else {
isSecondViewClick = false;
mImageArrowSecond.setBackgroundResource(R.drawable.next_disable_icon);
mLinearScrollThird.setVisibility(View.GONE);
}
return false;
}
});
}
final String catName = v_checklistParentItemModelArrayList.get(i).getV_checklistSubGroupModelArrayList().get(j).getSubGroupName();
mSubItemName.setText(catName);
//Adds items in subcategories
for (int k = 0; k < v_checklistParentItemModelArrayList.get(i).getV_checklistSubGroupModelArrayList().get(j).getV_checklistChildItemModelArrayList().size(); k++) {
Log.v("K : ", " " + k);
LayoutInflater inflater3 = null;
inflater3 = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View mLinearView3 = inflater3.inflate(R.layout.custom_cardview_checklist_groups_description_child_item, null);
TextView mItemName = (TextView) mLinearView3.findViewById(R.id.textViewItemName);
final String itemName = v_checklistParentItemModelArrayList.get(i).getV_checklistSubGroupModelArrayList().get(j).getV_checklistChildItemModelArrayList().get(k).getChildItemQuestionName();
mItemName.setText(itemName);
mLinearScrollThird.addView(mLinearView3);
final int finalI = i;
final int finalJ = j;
final int finalK = k;
mLinearScrollThird.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
Bundle bundle = new Bundle();
bundle.putString("groupName", v_checklistParentItemModelArrayList.get(finalI).getParentGroupName());
bundle.putInt("itemCount", v_checklistParentItemModelArrayList.get(finalI).getV_checklistSubGroupModelArrayList().get(finalJ).getV_checklistChildItemModelArrayList().size());
// bundle.putSerializable("questionData", group2bQuestionList);
bundle.putSerializable("alldata", checklist_groupNamePojoArrayList);
pageNumber = finalI + finalJ + finalK + 1;
bundle.putInt("pageNumber", pageNumber);
Log.v("====ugugugu==", "===ijhbjhbjh===");
Toast.makeText(getActivity(), "Page No. " + v_checklistParentItemModelArrayList.get(finalI).getV_checklistSubGroupModelArrayList().get(finalJ).getV_checklistChildItemModelArrayList().get(finalK).getChildQuestionID(), Toast.LENGTH_LONG).show();
ChecklistGroupsQuestionsMainDialogFragment checklistGroupsQuestionsMainDialogFragment = new ChecklistGroupsQuestionsMainDialogFragment();
checklistGroupsQuestionsMainDialogFragment.setArguments(bundle);
checklistGroupsQuestionsMainDialogFragment.setTargetFragment(current, 101);
checklistGroupsQuestionsMainDialogFragment.show(fragmentManager, getResources().getString(R.string.check_list_groups_question_screen_name));
return false;
}
});
}
mLinearScrollSecond.addView(mLinearView2);
}
mLinearListView.addView(mLinearView);
} else {
//Adds items in subcategories
for (int k = 0; k < v_checklistParentItemModelArrayList.get(i).getV_checklistChildItemModelArrayList().size(); k++) {
Log.v("K : ", " " + k);
LayoutInflater inflater3 = null;
inflater3 = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View mLinearView3 = inflater3.inflate(R.layout.custom_cardview_checklist_groups_description_child_item, null);
final LinearLayout mLinearScrollThird = (LinearLayout) mLinearView3.findViewById(R.id.linear_scroll_third);
TextView mItemName = (TextView) mLinearView3.findViewById(R.id.textViewItemName);
final String itemName = v_checklistParentItemModelArrayList.get(i).getV_checklistChildItemModelArrayList().get(k).getChildItemQuestionName();
mItemName.setText(itemName);
final int finalI = i;
final int finalK = k;
mLinearScrollThird.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
Bundle bundle = new Bundle();
bundle.putString("groupName", v_checklistParentItemModelArrayList.get(finalI).getParentGroupName());
bundle.putInt("itemCount", v_checklistParentItemModelArrayList.get(finalI).getV_checklistChildItemModelArrayList().size());
bundle.putSerializable("alldata", checklist_groupNamePojoArrayList);
bundle.putInt("pageNumber", 1);
Toast.makeText(getActivity(), "Page No. " + v_checklistParentItemModelArrayList.get(finalI).getV_checklistChildItemModelArrayList().get(finalK).getChildQuestionID(), Toast.LENGTH_LONG).show();
ChecklistGroupsQuestionsMainDialogFragment checklistGroupsQuestionsMainDialogFragment = new ChecklistGroupsQuestionsMainDialogFragment();
checklistGroupsQuestionsMainDialogFragment.setArguments(bundle);
checklistGroupsQuestionsMainDialogFragment.setTargetFragment(current, 101);
checklistGroupsQuestionsMainDialogFragment.show(fragmentManager, getResources().getString(R.string.check_list_groups_question_screen_name));
return false;
}
});
mLinearScrollSecond.addView(mLinearView3);
}
mLinearListView.addView(mLinearView);
}
}
//==============================================================================
return itemView;
}
I have assigned Child Question ID using setChildQuestionID(). For Group Name 0, I get 0, 1, 2 on each child click. However, for Group Name 1, I get 4 for Sub Group Name 2a & 7 for Sub Group Name 2b for each of there child item click. Ideally, it should return whatever I set for them.
Do you mean that you want to get 0, 1, 2 for child clicks in group name 1? As far as I can tell, you're going to have to re-set the value of index to 0 to get that. At least, assuming you've defined index = 0 somewhere outside your outermost for-loop (I can't see where in your code you've set the value of index). Right now in Group 0, you have:
for (int k = 0; k < 2; k++) {
V_ChecklistChildItemModel v_checklistChildItemModel = new V_ChecklistChildItemModel();
v_checklistChildItemModel.setChildItemQuestionID("" + (k + 1));
v_checklistChildItemModel.setChildItemQuestionName("Description of Question " + (k + 1));
v_checklistChildItemModel.setChildQuestionID("" + index);
index++;
If index was 0 to start with, the value of index is now greater than 0. You don't set it back to 0, so when you get to the next cycle through your top-level for loop, the value of index is already > 0. If you want to start over at index = 0 in Group 1, you'll have to set the value of index back to 0.
In other words, you are returning exactly what you set for the questionIDs, but I think you're setting a number that you didn't want to set.
How to call AsyncTask class in the fragment class, when i call is not go in the AsyncTask it will return null view what i do ? And my view is created in postExecute afetr i got all the data from doInbackground.
public class ShowResultsFragment extends android.support.v4.app.Fragment {
Context context;
ArrayList<Semester> semesterArrayList;
LayoutInflater inflaterView;
ViewGroup containerView;
View newView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
this.inflaterView = inflater;
this.containerView = container;
context = container.getContext();
new StudentResultAsyncTask().execute();
return newView;
}
public class StudentResultAsyncTask extends AsyncTask {
JSONObject jsonObject = null;
ProgressDialog pDialog;
String jsondata = "{\"1\" :[{\"subName\":\"EG\",\"subCode\":\"1009\",\"subCredit\":\"5\",\"subGrade\":\"AA\"}," +
"{\"subName\":\"ES\",\"subCode\":\"2009\",\"subCredit\":\"6\",\"subGrade\":\"DD\"}," +
"{\"subName\":\"EME\",\"subCode\":\"3009\",\"subCredit\":\"6\",\"subGrade\":\"FF\"}," +
"{\"subName\":\"EEE\",\"subCode\":\"4009\",\"subCredit\":\"7\",\"subGrade\":\"BC\"}," +
"{\"subName\":\"MOS\",\"subCode\":\"5009\",\"subCredit\":\"6\",\"subGrade\":\"FF\"}," +
"{\"subName\":\"M-1\",\"subCode\":\"1009\",\"subCredit\":\"6\",\"subGrade\":\"CD\"}," +
"{\"subName\":\"EE\",\"subCode\":\"45090\",\"subCredit\":\"4\",\"subGrade\":\"AB\"}," +
"{\"subName\":\"ECE\",\"subCode\":\"10090\",\"subCredit\":\"5\",\"subGrade\":\"FF\"}," +
"{\"SPI\":\"1.4\",\"CPI\":\"4.67\"}]," +
"\"2\":[{\"subName\":\"M-2\",\"subCode\":\"111009\",\"subCredit\":\"6\",\"subGrade\":\"DD\"}," +
"{\"subName\":\"Workshop\",\"subCode\":\"1009\",\"subCredit\":\"6\",\"subGrade\":\"AB\"}," +
"{\"subName\":\"CPU\",\"subCode\":\"1009\",\"subCredit\":\"6\",\"subGrade\":\"FF\"}," +
"{\"subName\":\"EG\",\"subCode\":\"1009\",\"subCredit\":\"6\",\"subGrade\":\"CC\"}," +
"{\"subName\":\"M-4\",\"subCode\":\"1009\",\"subCredit\":\"6\",\"subGrade\":\"CD\"}," +
"{\"subName\":\"AE\",\"subCode\":\"103309\",\"subCredit\":\"6\",\"subGrade\":\"CD\"}," +
"{\"subName\":\"EG-1\",\"subCode\":\"11119\",\"subCredit\":\"6\",\"subGrade\":\"CD\"}," +
"{\"subName\":\"AE\",\"subCode\":\"8876\",\"subCredit\":\"6\",\"subGrade\":\"FF\"}," +
"{\"SPI\":\"2.5\",\"CPI\":\"6.67\"}]," +
"\"3\":[{\"subName\":\"M-3\",\"subCode\":\"111009\",\"subCredit\":\"6\",\"subGrade\":\"AA\"}," +
"{\"subName\":\"BE\",\"subCode\":\"1009\",\"subCredit\":\"6\",\"subGrade\":\"AB\"}," +
"{\"subName\":\"DLD\",\"subCode\":\"1009\",\"subCredit\":\"6\",\"subGrade\":\"BB\"}," +
"{\"subName\":\"NCS\",\"subCode\":\"1009\",\"subCredit\":\"6\",\"subGrade\":\"BC\"}," +
"{\"subName\":\"M-4\",\"subCode\":\"1009\",\"subCredit\":\"6\",\"subGrade\":\"CC\"}," +
"{\"subName\":\"AE\",\"subCode\":\"103309\",\"subCredit\":\"6\",\"subGrade\":\"CD\"}," +
"{\"SPI\":\"6.50\",\"CPI\":\"0.67\"}]," +
"\"4\":[{\"subName\":\"AAS\",\"subCode\":\"111009\",\"subCredit\":\"6\",\"subGrade\":\"AB\"}," +
"{\"subName\":\"C++\",\"subCode\":\"1009\",\"subCredit\":\"6\",\"subGrade\":\"AB\"}," +
"{\"subName\":\"Management-1\",\"subCode\":\"1009\",\"subCredit\":\"6\",\"subGrade\":\"AB\"}," +
"{\"subName\":\"EG\",\"subCode\":\"1009\",\"subCredit\":\"6\",\"subGrade\":\"AB\"}," +
"{\"subName\":\"M-4\",\"subCode\":\"1009\",\"subCredit\":\"6\",\"subGrade\":\"AB\"}," +
"{\"subName\":\"AE\",\"subCode\":\"103309\",\"subCredit\":\"6\",\"subGrade\":\"DD\"}," +
"{\"SPI\":\"9.50\",\"CPI\":\"5.67\"}]," +
"\"5\":[{\"subName\":\"WAD\",\"subCode\":\"111022209\",\"subCredit\":\"6\",\"subGrade\":\"FF\"}," +
"{\"subName\":\"AES\",\"subCode\":\"703309\",\"subCredit\":\"6\",\"subGrade\":\"DD\"}," +
"{\"SPI\":\"5.50\",\"CPI\":\"5.67\",\"CGPA\":\"5.67\"}]," +
"\"6\":[{\"subName\":\"SP\",\"subCode\":\"66622209\",\"subCredit\":\"6\",\"subGrade\":\"FF\"}," +
"{\"subName\":\"Parallel\",\"subCode\":\"66622209\",\"subCredit\":\"6\",\"subGrade\":\"AB\"}," +
"{\"subName\":\"Java\",\"subCode\":\"66622209\",\"subCredit\":\"6\",\"subGrade\":\"AB\"}," +
"{\"subName\":\"COA\",\"subCode\":\"66622209\",\"subCredit\":\"6\",\"subGrade\":\"AB\"}," +
"{\"subName\":\"Management\",\"subCode\":\"66622209\",\"subCredit\":\"6\",\"subGrade\":\"FF\"}," +
"{\"subName\":\"TOC\",\"subCode\":\"703309\",\"subCredit\":\"6\",\"subGrade\":\"DD\"}," +
"{\"SPI\":\"5.50\",\"CPI\":\"5.67\",\"CGPA\":\"6.07\"}]," +
"\"7\":[{\"subName\":\"CD\",\"subCode\":\"12522209\",\"subCredit\":\"6\",\"subGrade\":\"AB\"}," +
"{\"subName\":\"WCMP\",\"subCode\":\"12522209\",\"subCredit\":\"6\",\"subGrade\":\"AB\"}," +
"{\"subName\":\"SP\",\"subCode\":\"12522209\",\"subCredit\":\"6\",\"subGrade\":\"AB\"}," +
"{\"subName\":\"Advance Java\",\"subCode\":\"703309\",\"subCredit\":\"6\",\"subGrade\":\"FF\"}," +
"{\"SPI\":\"8.90\",\"CPI\":\"6.09\",\"CGPA\":\"7.77\"}]," +
"\"8\":[{\"subName\":\"Android\",\"subCode\":\77022209\",\"subCredit\":\"6\",\"subGrade\":\"AB\"}," +
"{\"subName\":\"PP\",\"subCode\":2209\",\"subCredit\":\"6\",\"subGrade\":\"BB\"}," +
"{\"subName\":\"DS\",\"subCode\":\"7309\",\"subCredit\":\"6\",\"subGrade\":\"DD\"}," +
"{\"SPI\":\"5.70\",\"CPI\":\"5.97\",\"CGPA\":\"6.85\"}]}";
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(context);
pDialog.setMessage("Loading...");
pDialog.show();
}
#Override
protected String doInBackground(String... params1) {
String key;
Iterator<String> iter;
int length;
/* List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair(c.getResources().getString(R.string.keyUsername), Utility.getUsername(c)));
JSONParser jp=new JSONParser();
//jp.makeHttpRequest(c.getResources().getString(R.string.server_name)+ c.getResources().getString(R.string.login_file), "POST", params);
*/ //Todo : delete comment when data is come from server.
try {
jsonObject = new JSONObject(jsondata);
} catch (JSONException e) {
e.printStackTrace();
}
iter = jsonObject.keys();
semesterArrayList = new ArrayList<>();
while (iter.hasNext()) {
key = iter.next();
try {
JSONArray jsonArray = jsonObject.getJSONArray(key);
length = jsonArray.length();
ArrayList<SubjectDetails> saveSubDetailsArrayList = new ArrayList<>();
Semester semDetail = new Semester();
semDetail.setSemester(key);
for (int i = 0; i < length; i++) {
JSONObject jsonObject1 = jsonArray.getJSONObject(i);
SubjectDetails subDetails = new SubjectDetails();
if (i == length - 1) {
Float spi = Float.valueOf(jsonObject1.getString(getResources().getString(R.string.server_key_result_SPI)));
Float cpi = Float.valueOf(jsonObject1.getString(getResources().getString(R.string.server_key_result_CPI)));
semDetail.setSpi(spi);
semDetail.setCpi(cpi);
if (key.equals("5") || key.equals("6") || key.equals("7") || key.equals("8")) {
Float cgpi = Float.valueOf(jsonObject1.getString(getResources().getString(R.string.server_key_result_CGPA)));
semDetail.setCgpi(cgpi);
}
} else {
String subname = jsonObject1.getString(getResources().getString(R.string.server_key_result_subName));
String subcode = jsonObject1.getString(getResources().getString(R.string.server_key_result_subCode));
String subcredit = jsonObject1.getString(getResources().getString(R.string.server_key_result_subCredit));
String subgrade = jsonObject1.getString(getResources().getString(R.string.server_key_result_subGrade));
subDetails.setSubname(subname);
subDetails.setSubcode(subcode);
subDetails.setSubcredit(subcredit);
subDetails.setSubgrade(subgrade);
saveSubDetailsArrayList.add(subDetails);
semDetail.setGetSubjectDetails(saveSubDetailsArrayList);
}
}
semesterArrayList.add(semDetail);
} catch (JSONException e) {
e.printStackTrace();
}
}
return null;
}
#Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
ListView lv = (ListView) newView.findViewById(R.id.listview);
lv.setAdapter(new ShowResultAdapter(semesterArrayList, context));
pDialog.dismiss();
}
}
and this is my Adapter class...
public class ShowResultAdapter extends BaseAdapter {
ArrayList<Semester> semesterArrayList;
Context c;
LinearLayout linearHeader;
ShowResultAdapter(ArrayList<Semester> saveDetails, Context context)
{
semesterArrayList = saveDetails;
c = context;
}
#Override
public int getCount() {
return semesterArrayList.size();
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
LinearLayout linearSub;
LinearLayout linearSpiCpiCgpa;
LayoutInflater inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.layout_result_adapter, null);
LinearLayout linearLayout = (LinearLayout) view.findViewById(R.id.linear);
TextView sem = new TextView(c);
sem.setGravity(Gravity.CENTER_HORIZONTAL);
sem.setTextSize(20);
sem.setPadding(0, 0, 0, 10);
sem.setTextColor(Color.WHITE);
sem.setText(c.getResources().getString(R.string.result_Semester) + " " + ":" + " " + semesterArrayList.get(position).getSemester());
sem.setPaintFlags(Paint.UNDERLINE_TEXT_FLAG);
linearLayout.addView(sem);
linearHeader = new LinearLayout(c);
linearHeader.setOrientation(LinearLayout.HORIZONTAL);
linearHeader.setWeightSum(4);
TextView name = new TextView(c);
setHeader(name, c.getResources().getString(R.string.server_key_result_subName));
linearHeader.addView(name);
TextView code = new TextView(c);
setHeader(code, c.getResources().getString(R.string.server_key_result_subCode));
linearHeader.addView(code);
TextView credit = new TextView(c);
setHeader(credit, c.getResources().getString(R.string.server_key_result_subCredit));
linearHeader.addView(credit);
TextView grade = new TextView(c);
setHeader(grade, c.getResources().getString(R.string.server_key_result_subGrade));
linearHeader.addView(grade);
linearLayout.addView(linearHeader);
if (semesterArrayList.get(position).getSemester().equals("1") || semesterArrayList.get(position).getSemester().equals("2") || semesterArrayList.get(position).getSemester().equals("3") || semesterArrayList.get(position).getSemester().equals("4")) {
linearSpiCpiCgpa = new LinearLayout(c);
linearSpiCpiCgpa.setOrientation(LinearLayout.HORIZONTAL);
linearSpiCpiCgpa.setWeightSum(4);
for (int i = 0; i < semesterArrayList.get(position).getGetSubjectDetails().size(); i++)
{
linearSub = new LinearLayout(c);
linearSub.setLayoutParams(new TableLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 4));
linearSub.setOrientation(LinearLayout.HORIZONTAL);
TextView subname = new TextView(c);
setSubject(subname);
TextView subcode = new TextView(c);
setSubject(subcode);
TextView subcredit = new TextView(c);
setSubject(subcredit);
TextView subgrade = new TextView(c);
setSubject(subgrade);
subgrade.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
subname.setText(semesterArrayList.get(position).getGetSubjectDetails().get(i).getSubname());
subcode.setText(semesterArrayList.get(position).getGetSubjectDetails().get(i).getSubcode());
subcredit.setText(semesterArrayList.get(position).getGetSubjectDetails().get(i).getSubcredit());
subgrade.setText(semesterArrayList.get(position).getGetSubjectDetails().get(i).getSubgrade());
if (semesterArrayList.get(position).getGetSubjectDetails().get(i).getSubgrade().equals("FF")) {
subgrade.setTextColor(Color.parseColor("#ff0000"));
}
linearSub.addView(subname);
linearSub.addView(subcode);
linearSub.addView(subcredit);
linearSub.addView(subgrade);
linearLayout.addView(linearSub);
}
TextView spi = new TextView(c);
setSpiCpiCgpa(spi);
TextView cpi = new TextView(c);
setSpiCpiCgpa(cpi);
spi.setText(c.getResources().getString(R.string.server_key_result_SPI) + " " + ":" + " " + semesterArrayList.get(position).getSpi());
cpi.setText(c.getResources().getString(R.string.server_key_result_CPI) + " " + ":" + " " + semesterArrayList.get(position).getCpi());
linearSpiCpiCgpa.addView(spi);
linearSpiCpiCgpa.addView(cpi);
linearLayout.addView(linearSpiCpiCgpa);
} else {
linearSpiCpiCgpa = new LinearLayout(c);
linearSpiCpiCgpa.setOrientation(LinearLayout.HORIZONTAL);
linearSpiCpiCgpa.setWeightSum(6);
for (int i = 0; i < semesterArrayList.get(position).getGetSubjectDetails().size(); i++)
{
linearSub = new LinearLayout(c);
linearSub.setOrientation(LinearLayout.HORIZONTAL);
linearSub.setLayoutParams(new TableLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 4));
TextView subname = new TextView(c);
setSubject(subname);
TextView subcode = new TextView(c);
setSubject(subcode);
TextView subcredit = new TextView(c);
setSubject(subcredit);
TextView subgrade = new TextView(c);
setSubject(subgrade);
subname.setText(semesterArrayList.get(position).getGetSubjectDetails().get(i).getSubname());
subcode.setText(semesterArrayList.get(position).getGetSubjectDetails().get(i).getSubcode());
subcredit.setText(semesterArrayList.get(position).getGetSubjectDetails().get(i).getSubcredit());
subgrade.setText(semesterArrayList.get(position).getGetSubjectDetails().get(i).getSubgrade());
linearSub.addView(subname);
linearSub.addView(subcode);
linearSub.addView(subcredit);
linearSub.addView(subgrade);
linearLayout.addView(linearSub);
}
TextView spi = new TextView(c);
setSpiCpiCgpa(spi);
TextView cpi = new TextView(c);
setSpiCpiCgpa(cpi);
TextView cgpi = new TextView(c);
setSpiCpiCgpa(cgpi);
spi.setText(c.getResources().getString(R.string.server_key_result_SPI) + " " + ":" + " " + semesterArrayList.get(position).getSpi());
cpi.setText(c.getResources().getString(R.string.server_key_result_CPI) + " " + ":" + " " + semesterArrayList.get(position).getCpi());
cgpi.setText(c.getResources().getString(R.string.server_key_result_CGPA) + " " + ":" + " " + semesterArrayList.get(position).getCgpi());
linearSpiCpiCgpa.addView(spi);
linearSpiCpiCgpa.addView(cpi);
linearSpiCpiCgpa.addView(cgpi);
linearLayout.addView(linearSpiCpiCgpa);
}
return view;
}
public void setHeader(TextView txtObj, String setText) {
txtObj.setPadding(0, 0, 0, 10);
txtObj.setGravity(Gravity.CENTER_HORIZONTAL);
txtObj.setBackgroundResource(R.drawable.whiteborder);
txtObj.setTextColor(Color.WHITE);
txtObj.setBackgroundColor(Color.parseColor("#80000000"));
txtObj.setTextSize(16);
txtObj.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
txtObj.setLayoutParams(new TableLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1));
txtObj.setText(setText);
}
public void setSubject(TextView txtObj) {
txtObj.setLayoutParams(new TableLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, 1));
txtObj.setPadding(5, 5, 5, 5);
txtObj.setBackgroundResource(R.drawable.resultborder);
txtObj.setTextColor(Color.WHITE);
txtObj.setGravity(Gravity.CENTER);
}
public void setSpiCpiCgpa(TextView txtObj) {
txtObj.setPadding(5, 5, 5, 5);
txtObj.setGravity(Gravity.CENTER_HORIZONTAL);
txtObj.setTextColor(Color.WHITE);
txtObj.setBackgroundResource(R.drawable.resultborder);
txtObj.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
txtObj.setLayoutParams(new TableLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 2));
}
}
You didn't inflate your View. How do you expect it to not return null?
You should inflate the View in onCreateView() rather than in onPostExecute()
Change it like this.
ShowResultAdapter adapter = null;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
this.inflaterView = inflater;
this.containerView = container;
context = container.getContext();
newView = inflaterView.inflate(R.layout.your_layout, null);
lv = (ListView) newView.findViewById(R.id.listview); // Define lv at class level
adapter = new ShowResultAdapter(semesterArrayList, getActivity())
lv.setAdapter(adapter);
new StudentResultAsyncTask().execute();
return newView;
}
In your onPostExecute() method do this
protected void onPostExecute(String s)
{
super.onPostExecute(s);
adapter.notifyDataSetChanged();
pDialog.dismiss();
}
Following from the suggestions made by Rohit5k2, alter your getcount() method in your adapter to respond to null data:
#Override
public int getCount() {
if (semesterArrayList == null) {
return 0;
}
return semesterArrayList.size();
}
When you first call setAdapter() on the ListView in Rohit's answer, the data will be null as it is only populated in onPostExecute(). However, if you make the above adjustment, then all will happen is that the list will be empty until the data is populated.