我已经编写了在android studio上显示ExpandableListView的代码。 这是ExplandableListView的代码:
public class Recipes extends AppCompatActivity implementsView.OnClickListener {
ExpandableListView exp;
EditText t;
Button b;
HashMap<String, List<String>> Movies_category;
List<String> Movies_list;
ExpandableListView Exp_list;
MoviesAdapter adapter;
protected void onCreate(Bundle savedInstanceState) {
setTheme(R.style.NoActionBar);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recipes);
t = (EditText) findViewById(R.id.t);
b = (Button) findViewById(R.id.b);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recipes);
Exp_list = (ExpandableListView) findViewById(R.id.exp_list);
Movies_category = DataProvider.getInfo();
Movies_list = new ArrayList<String>(Movies_category.keySet());
adapter = new MoviesAdapter(this, Movies_category, Movies_list);
Exp_list.setAdapter(adapter);
t.setOnClickListener(this);
b.setOnClickListener(this);
}
@Override
public void onClick(View v) {
startActivity(new Intent(this, Choose.class));
}
}
这是执行操作的方法:
public class RecipeAda extends BaseExpandableListAdapter{
private Context ctx;
private HashMap<String, List<String>> recipes;
private List<String> ingr;
public RecipeAda(Context ctx,HashMap<String, List<String>>recipes, List<String> ingr)
{
this.ctx=ctx;
this.recipes=recipes;
this.ingr=ingr;
}
@Override
public int getGroupCount() {
return recipes.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return recipes.get(ingr.get(groupPosition)).size();
}
@Override
public Object getGroup(int groupPosition) {
return recipes.get(groupPosition);
}
@Override
public Object getChild(int parent, int child) {
return recipes.get(ingr.get(parent)).get(child);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int parent, int child) {
return child;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int parent, boolean isExpanded, View convertView, ViewGroup parentview) {
String grptit= (String) getGroup(parent);
if (convertView==null)
{
LayoutInflater inf= (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView=inf.inflate(R.layout.parent_layout,parentview,false);
}
TextView pa= (TextView) convertView.findViewById(R.id.textView4);
pa.setTypeface(null, Typeface.BOLD);
pa.setText(grptit);
return convertView;
}
@Override
public View getChildView(int parent, int child, boolean lastChild, View convertView, ViewGroup parentview) {
String childtitle= (String) getChild(parent,child);
if (convertView==null)
{
LayoutInflater inf= (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView=inf.inflate(R.layout.child_layout,parentview,false);
}
TextView ch= (TextView) convertView.findViewById(exp_list);
ch.setText(childtitle);
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}}
它在&#34; groupPosition&#34;显示警告。在函数定义中:
@Override
public Object getGroup(int groupPosition) {
return recipes.get(groupPosition);
}
当它悬停在它上面时,HashMap<String, List<String>> may not contain objects of type 'integer'
说
我该如何纠正?
另外,如何接受用户的内容(字符串)并保存在数据库中?
答案 0 :(得分:1)
要创建HashMap,它需要两件事:一个键和一个类型。要获取类型(在您的情况下为List),您需要传递一个键(在您的情况下为String)。这意味着如果您想使用当前代码.get
,则必须传递一个字符串。如果要使用整数,只需将hashmap更改为:
private HashMap<Integer, List<String>> recipes;
或者,如果您想使用带数字的字符串
,请Integer.toString(id);
另外,我如何接受用户的内容
取决于您的意思&#34;接受内容&#34;。如果您只想将一个String传递给hashmap,那么您可以执行.put(id, "some string");
并保存?