问题
I have an expandable ListView, where every parentview has a child view. In child view I have a sub-child element: EditText, TextView and RadioButton.
Child element have different value onclick on radiobutton and edittext value after entered
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);
}
}
回答1:
Instead of accessing view directly (by iterating through children of ViewGroup
, which won't even work as you expect, since ExpandableListView
recycles it's view as well as ListView
does), you should store entered data into some sort of model.
In other words, while you're displaying data in list, it should be backed by some information (String[]
in simpliest case). So, your task is to back displayed data with some specific class, which will hold information that you need. For example, you can name it Option
.
Then, when you're editing data in EditText
, you saving entered text into instance of Option
that corresponds to current position of ExpandableListView
. When you'll need to retrieve entered information, you just iterate through every Option
that you saved.
回答2:
I'm not entirely sure what you are trying to do, but you could try to get the children of each ViewGroup like this:
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 be whatever is holding all of these views (LinearLayout, etc), so you would need to get a reference to that first.
来源:https://stackoverflow.com/questions/17739634/how-to-get-every-value-of-editbox-and-radiobutton-when-i-click-on-expandable-ch