如何显示自定义ExpandableListView?

时间:2015-05-03 20:05:08

标签: android

我的应用程序的一个页面是名称的ExpandableListView,它应该在单击时下拉,并显示更多信息。我的代码编译但是" setAdapter()"如下所示不显示任何内容。父视图甚至不显示,我看到的只是一个空白页。

            json = new JSONArray(result);
            setContentView(R.layout.activity_doctor_list);
            listView = (ExpandableListView) findViewById(R.id.listView);
            adapter = new ExpandableDoctorAdapter(Doctors.this, json, email, logPrefs);

            for(int i =0; i < json.length(); i++) {
                adapter.getGroupView(i, false, null, null);
                System.out.println("Getting a doctor...");
            }
            listView.setAdapter((android.widget.ExpandableListAdapter) adapter);

这是&#34; getGroupView()&#34;上面调用的函数。我知道每个列表项都会调用adapter.getView(...),但由于某种原因,setAdapter()没有显示任何内容。

public View getGroupView(int position, boolean isExpanded, View convertView, ViewGroup parent) {
    int id = -1;
    String str_id = null;
    if (convertView == null) {
        convertView = LayoutInflater.from(context).inflate(R.layout.doctor_item, parent, false);
    }
    JSONObject jsonObject = null;
    TextView docName = (TextView) convertView.findViewById(R.id.name);
    TextView docDistance = (TextView) convertView.findViewById(R.id.distance);
    ImageButton form = (ImageButton) convertView.findViewById(R.id.form);
    ImageButton bio = (ImageButton) convertView.findViewById(R.id.profile);

    try {
        jsonObject = (JSONObject) content.get(position);
        docName.setText(jsonObject.getString("FirstName") + " " + jsonObject.getString("LastName"));
        docDistance.setText(jsonObject.getString("Distance") + " miles away");
        str_id = jsonObject.getString("DoctorId")  ;
        id = parseInt(str_id);
        convertView.setId(id);
    } catch (JSONException e) {
        e.printStackTrace();
    }

    final String finalStr_id = str_id;
    form.setOnClickListener(new View.OnClickListener() {
        Intent myIntent;
        public void onClick(View view) {
            if (username != null) {
                myIntent = new Intent(context, Form.class);
            } else {
                myIntent = new Intent(context, Login.class);
            }
            myIntent.putExtra("docId", finalStr_id);
            context.startActivity(myIntent);
        }
    });

    bio.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            Intent myIntent = new Intent(context, Bio.class);
            myIntent.putExtra("docId", finalStr_id);
            context.startActivity(myIntent);
        }
    });
    System.out.print("Returning a doctor view...");
    return convertView;
}

我是否滥用了getGroupView(...)函数?我之前将此设置为普通的listView,并且它使用类似的代码结构,但切换到可扩展列表视图不会显示任何内容。

0 个答案:

没有答案