gpt4 book ai didi

android - 单击可扩展子元素时如何获取编辑框和单选按钮的每个值?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:02:50 24 4
gpt4 key购买 nike

我有一个可扩展的 ListView,其中每个父 View 都有一个 subview 。在 subview 中,我有一个子子元素:EditText、TextView 和 RadioButton。

子元素在单选按钮上的点击值和输入后的编辑文本值不同

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);
}

}

最佳答案

而不是直接访问 View (通过遍历 ViewGroup 的子级,这甚至不会像您预期的那样工作,因为 ExpandableListView 回收它的 View 以及 ListView 可以),您应该将输入的数据存储到某种模型中。

换句话说,当您在列表中显示数据时,它应该得到一些信息的支持(在最简单的情况下为 String[])。因此,您的任务是用一些特定的类来支持显示的数据,这些类将保存您需要的信息。例如,您可以将其命名为 Option

然后,当您在 EditText 中编辑数据时,您将输入的文本保存到与 ExpandableListView 当前位置相对应的 Option 实例中.当您需要检索输入的信息时,您只需遍历保存的每个 Option

关于android - 单击可扩展子元素时如何获取编辑框和单选按钮的每个值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17739634/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com