我有一个可扩展的ListView,每个父视图都有一个子视图。 在子视图中,我有一个子子元素:EditText,TextView和RadioButton。
在输入
后,子元素具有不同的值onclick radiobutton和edittext值
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;
}
}
}
类别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);
}
}
答案 0 :(得分:2)
而不是直接访问视图(通过迭代ViewGroup
的子项,这甚至不会按预期工作,因为ExpandableListView
会回收它的视图以及ListView
所做的事情,你应该把输入的数据存储到某种模型中。
换句话说,当您在列表中显示数据时,它应该由一些信息支持(在最简单的情况下为String[]
)。因此,您的任务是使用某些特定类来支持显示的数据,这些类将保存您需要的信息。例如,您可以将其命名为Option
。
然后,当您在EditText
中修改数据时,将输入的文本保存到与Option
的当前位置对应的ExpandableListView
实例中。当您需要检索输入的信息时,您只需遍历您保存的每个Option
。
答案 1 :(得分:1)
我不完全确定你要做什么,但你可以尝试让每个ViewGroup的孩子都这样:
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
}
}
请记住,ViewGroup对象将是包含所有这些视图的任何对象(LinearLayout等),因此您需要首先获得对它的引用。