gpt4 book ai didi

android - 如何使用 getter 和 setter 方法在可扩展 ListView 上获取组和子项

转载 作者:行者123 更新时间:2023-11-30 01:01:15 25 4
gpt4 key购买 nike

大家好,我想在 ExpandableListViewsetOnGroupClickListenersetOnChildClickListener 上获取项目,但没有获取它的项目。

这是我的代码:

组.java

public class Group {

private String Name;
private ArrayList<Child> Items;

public String getName() {
return Name;
}

public void setName(String name) {
Name = name;
}

public ArrayList<Child> getItems() {
return Items;
}

public void setItems(ArrayList<Child> items) {
Items = items;
}
}

子类.java

public class Child {
private String Name;

public String getName() {
return Name;
}

public void setName(String name) {
Name = name;
}
}

扩展列表适配器.java

public class ExpandListAdapter extends BaseExpandableListAdapter {

private Context context;
private ArrayList<Group> groups;

public ExpandListAdapter(Context context, ArrayList<Group> groups) {
this.context = context;
this.groups = groups;
}

@Override
public int getGroupCount() {
return groups.size();
}

@Override
public int getChildrenCount(int groupPosition) {
ArrayList<Child> chList = groups.get(groupPosition).getItems();
return chList.size();
}

@Override
public Object getGroup(int groupPosition) {
return groups.get(groupPosition);
}

@Override
public Object getChild(int groupPosition, int childPosition) {
ArrayList<Child> chList = groups.get(groupPosition).getItems();
return chList.get(childPosition);
}

@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}

@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}

@Override
public boolean hasStableIds() {
return false;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
Group group = (Group) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater inf = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = inf.inflate(R.layout.group_item, null);
}
TextView tv = (TextView) convertView.findViewById(R.id.group_name);
tv.setText(group.getName());
return convertView;
}

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
Child child = (Child) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.child_item, null);
}

TextView tv = (TextView) convertView.findViewById(R.id.country_name);

tv.setText(child.getName().toString());

return convertView;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}

主 Activity .java

public class MainActivity extends AppCompatActivity {

String url = "http://ec2-52-40-135-171.us-west-2.compute.amazonaws.com/shop-cart/backend/web/api/category";

private ExpandListAdapter ExpAdapter;
private ExpandableListView ExpandList;

ArrayList<Group> list;
ArrayList<Child> ch_list;
Group gru;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ExpandList = (ExpandableListView) findViewById(R.id.exp_list);

list = new ArrayList<Group>();


makejsonobjreq();


ExpandList.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView expandableListView, View view, int i, long l) {

Toast.makeText(MainActivity.this, list.get(i) + "", Toast.LENGTH_SHORT).show();

return false;
}
});

ExpandList.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView expandableListView, View view, int i, int i1, long l) {

Toast.makeText(MainActivity.this, "" + ExpAdapter.getChild(i, i1), Toast.LENGTH_SHORT).show();

return false;
}
});
}

private void makejsonobjreq() {

JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.e("RES", String.valueOf(response));


try {
JSONObject jo = response.getJSONObject("data");
Log.e("data RES", jo.toString());

Log.e("dataLENGTH", String.valueOf(jo.length()));

//CHNGE NEEDED ==> REPLACE 2 WITH jo.length();
for (int i = 0; i < 2; i++) {

gru = new Group();
gru.setName(jo.getJSONObject(String.valueOf(i + 1)).getJSONArray("parent").getJSONObject(0).getString("name"));

Log.e("gru", gru.getName());
ch_list = new ArrayList<Child>();

JSONArray ja = jo.getJSONObject(String.valueOf(i + 1)).getJSONArray("child");
Log.e("child ARRAY", String.valueOf(ja));
Log.e("child LENGTH", String.valueOf(ja.length()));

for (int j = 0; j < ja.length(); j++) {

Child ch = new Child();
ch.setName(ja.getJSONObject(j).getString("name"));
ch_list.add(ch);

Log.e("CHILD NAME", "VAL OF J " + j + " " + ja.getJSONObject(j).getString("name"));
}
gru.setItems(ch_list);
list.add(gru);
}

ExpAdapter = new ExpandListAdapter(MainActivity.this, list);
ExpandList.setAdapter(ExpAdapter);

} catch (JSONException e) {
e.printStackTrace();
}

}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {

}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(jsonObjectRequest);
}
}

最佳答案

将您的项目作为标签放入 getChildViewgetGroupView 中,如下所示:

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
Child child = (Child) getChild(groupPosition, childPosition);
// keep your code
// set item as a tag
convertView.setTag(child);
return convertView;
}

然后你可以在回调中获取项目

ExpandList.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView expandableListView, View view, int i, int i1, long l) {
// get tag from view, it will be better if your check the object type.
Child child = (Child)view.getTag();
return false;
}
});

关于android - 如何使用 getter 和 setter 方法在可扩展 ListView 上获取组和子项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39362383/

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