How to get each value of EditBox and RadioButton when I click on an extensible child?

I have an extensible ListView where each parent view has a child view. In the child view, I have a child element: EditText, TextView and RadioButton.

The child element has a different onclick value on the radio object and an edittext value after input

enter image description here

CategoryActivity.java

package com.restaurant.app; public class CategoryActivity extends Activity implements OnClickListener { public static final String TAG_TITLE = "title"; public static final String TAG_PRICE="price"; private static final String TAG_CATEGORY="category"; private static final String TAG_DETAIL="detail"; private static final String TAG_MENU="menu"; private static final String TAG_ID="id"; private static final String TAG_NOME="nome"; private static final String TAG_HOTELID="hotel_id"; private static final String TAG_DESCRIPTION="description"; private static final String TAG_SERVICES="services"; private static final String TAG_URLID="urlid"; private static final String TAG_RESTAURANTID="restaurant_id"; private static final String TAG_USERID="user_id"; private static final String TAG_OFFERTA="offerta"; private static final String TAG_PREZZO="prezzo"; ArrayList<String> resultparent = new ArrayList<String>(); ArrayList<Object> resultchild = new ArrayList<Object>(); ArrayList<HashMap<String,String>> child = new ArrayList<HashMap<String,String>>(); // for childlist item ArrayList<ArrayList<HashMap<String, String>>> childlist = new ArrayList<ArrayList<HashMap<String,String>>>(); ImageView imagehome; ImageView imagemenu; ImageView imagebooking; ImageView imageordernow; ExpandableListView expandablelistview; CategoryList categoryadapter; SimpleExpandableListAdapter adapter; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.booking); imagehome=(ImageView)findViewById(R.id.imagehome); imagehome.setOnClickListener(this); imagemenu=(ImageView)findViewById(R.id.imagemenu); imagemenu.setOnClickListener(this); imageordernow=(ImageView)findViewById(R.id.imageordernow); imageordernow.setOnClickListener(this); expandablelistview = (ExpandableListView)findViewById(R.id.expandablelistview); new CategoryAsynctask().execute(""); } public class CategoryAsynctask extends AsyncTask<String,String,ArrayList<String>> { @Override protected ArrayList<String> doInBackground(String... arg0) { JsonParser jparser = new JsonParser(); String url="http://cssthemeclub.com/demo/saffron/webservices/categorymenu.php?restid=5"; String data=jparser.getJSONFromUrl(url); try { JSONObject jobject=new JSONObject(data); JSONArray jcategory=jobject.getJSONArray(TAG_CATEGORY); Log.e("Category length","--->"+jcategory.length()); for (int i=0;i<jcategory.length();i++) { JSONObject jdetail=jcategory.getJSONObject(i).getJSONObject(TAG_DETAIL); String categoryid=jdetail.get(TAG_ID).toString(); String nome=jdetail.get(TAG_NOME).toString(); String restaurantid=jdetail.get(TAG_RESTAURANTID).toString(); resultparent.add(nome); JSONArray jmenu = new JSONArray(); jmenu=null; jmenu=jcategory.getJSONObject(i).getJSONArray(TAG_MENU); //Log.e("Jmenu","--->"+jmenu); child=new ArrayList<HashMap<String,String>>(); child.clear(); for(int j=0;j<jmenu.length();j++) { String menuid=jmenu.getJSONObject(j).getString(TAG_ID).toString(); String offer=jmenu.getJSONObject(j).getString(TAG_OFFERTA).toString(); String price=jmenu.getJSONObject(j).getString(TAG_PREZZO).toString(); //Log.e("menu","--->"+offer); //Log.e("price","-->"+price); HashMap<String, String> map = new HashMap<String, String>(); map.put(TAG_MENU,menuid); map.put(TAG_PREZZO,price); map.put(TAG_OFFERTA,offer); map.put(TAG_RESTAURANTID,restaurantid); child.add(map); childlist.add(child); } resultchild.add(child); } Log.e("Result Child","--->"+resultchild); } catch (Exception e) { } return resultparent; } @Override protected void onPostExecute(ArrayList<String> result) { super.onPostExecute(result); categoryadapter= new CategoryList(resultparent,resultchild); categoryadapter.setInflater((LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE),CategoryActivity.this); expandablelistview.setAdapter(categoryadapter); } } @Override public void onClick(View v) { switch (v.getId()) { case R.id.imagehome : Intent home = new Intent(CategoryActivity.this,HomeActivity.class); startActivity(home); break; case R.id.imagemenu : Intent menu = new Intent(CategoryActivity.this,MenuActivity.class); startActivity(menu); break; case R.id.imageordernow : Intent ordernow= new Intent(CategoryActivity.this,OrderDetailActivity.class); startActivity(ordernow); break; default : break; } } } 

Category List.java

 public class CategoryList extends BaseExpandableListAdapter { private static final String TAG_OFFERTA="offerta"; private static final String TAG_PREZZO="prezzo"; private static final String TAG_RESTAURANTID="restaurant_id"; private static final String TAG_MENU="menu"; private static final String TAG_ID="id"; public Activity activity; public ArrayList<String> resultparent; public ArrayList<HashMap<String,String>> tempchild; public ArrayList<Object> resultchild=new ArrayList<Object>(); ArrayList<HashMap<String,String>> childlist = new ArrayList<HashMap<String,String>>(); public LayoutInflater inflater = null; String id; String qty; String title; String menuid; String sessionid; String restorantid; String price; public CategoryList(ArrayList<String> grouplist,ArrayList<Object> childItem) { resultparent=grouplist; this.resultchild = childItem; } public void setInflater(LayoutInflater mInflater, Activity act) { this.inflater = mInflater; activity = act; } @Override public Object getChild(int groupPosition, int childPosition) { return null; } @Override public long getChildId(int groupPosition, int childPosition) { return 0; } @Override public View getChildView(int groupPosition, int childPosition,boolean isLastChild, View convertView, ViewGroup parent) { tempchild = (ArrayList<HashMap<String,String>>)resultchild.get(groupPosition); TextView textprice,textmenutitle; final EditText editqty; RadioGroup radiogroupchilli; RadioGroup radiogroupoil; final ImageView imagechilli; final ImageView imagechillitwo; final ImageView imageoil; final ImageView imagechiz; // RadioButton radiochillion,radiochillioff,radiooil,radiochiz; if(convertView == null) { convertView = inflater.inflate(R.layout.redlistchild,null); } radiogroupchilli=(RadioGroup)convertView.findViewById(R.id.radiogroupchilli); radiogroupoil=(RadioGroup)convertView.findViewById(R.id.radiogroupoil); imagechilli=(ImageView)convertView.findViewById(R.id.imagechilli); imagechillitwo=(ImageView)convertView.findViewById(R.id.imagechillitwo); imageoil=(ImageView)convertView.findViewById(R.id.imageoil); imagechiz=(ImageView)convertView.findViewById(R.id.imagechiz); textprice=(TextView)convertView.findViewById(R.id.textprice); textprice.setText(tempchild.get(childPosition).get(TAG_PREZZO).toString()); textmenutitle=(TextView)convertView.findViewById(R.id.textmenutitle); textmenutitle.setText(tempchild.get(childPosition).get(TAG_OFFERTA).toString()); editqty=(EditText)convertView.findViewById(R.id.editqty); convertView.setTag(childPosition); radiogroupchilli.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { switch(checkedId) { case R.id.radiochillion : imagechillitwo.setImageResource(R.drawable.chilitwooff); imagechilli.setImageResource(R.drawable.chilion); break; case R.id.radiochillioff : imagechillitwo.setImageResource(R.drawable.chilitwoon); imagechilli.setImageResource(R.drawable.chilioff); break; } } }); radiogroupoil.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { switch(checkedId) { case R.id.radiooil : imageoil.setImageResource(R.drawable.oilon); imagechiz.setImageResource(R.drawable.chiijoff); break; case R.id.radiochiz : imageoil.setImageResource(R.drawable.oiloff); imagechiz.setImageResource(R.drawable.chijion); break; } } }); return convertView; } @Override public int getChildrenCount(int groupPosition) { return ((ArrayList<String>)resultchild.get(groupPosition)).size(); } @Override public Object getGroup(int groupPosition) { return resultparent.get(groupPosition); } @Override public int getGroupCount() { return resultparent.size(); } @Override public long getGroupId(int groupPosition) { return 0; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { TextView texttitle; final LinearLayout nonvegtitlelistlayout; if(convertView == null) { convertView = inflater.inflate(R.layout.redlistgroup, null); } nonvegtitlelistlayout=(LinearLayout)convertView.findViewById(R.id.nonvegtitlelistlayout); texttitle=(TextView)convertView.findViewById(R.id.texttitle); texttitle.setText(resultparent.get(groupPosition)); return convertView; } @Override public boolean hasStableIds() { return true; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } @Override public void onGroupCollapsed(int groupPosition) { super.onGroupCollapsed(groupPosition); } @Override public void onGroupExpanded(int groupPosition) { super.onGroupExpanded(groupPosition); } } 
+4
source share
2 answers

Instead of directly accessing the view (by repeating with the help of children of the ViewGroup , which will not work as you expect, since the ExpandableListView processes its view, as well as the ListView ), you should store the entered data in some kind of model.

In other words, while you are showing data in a list, it should be backed up with some information ( String[] in the simplest case). Thus, your task is to return the displayed data with a specific class that will contain the necessary information. For example, you can name it Option .

Then, when you edit the data in an EditText , you save the entered text into an Option instance that corresponds to the current position of the ExpandableListView . When you need to get the entered information, you simply iterate over all the Option that you saved.

+2
source

I'm not quite sure what you are trying to do, but you can try to get the children of each viewing group as follows:

 for(int x = 0; x < viewGroup.getChildCount(); x++) { final View v = viewGroup.getChildAt(x); if(v instanceof EditText) { //do logic } else if(v instanceof RadioButton) { //do logic } } 

Keep in mind that the ViewGroup object will contain all these views (LinearLayout, etc.), so you need to get a link to it first.

+1
source

Source: https://habr.com/ru/post/1492234/


All Articles