Android ExpandableListView中每行两个TextView

时间:2015-03-19 15:35:10

标签: java android expandablelistview

我的应用程序中有一个可扩展的文本视图。我可以让它在一个文本视图中显示所有信息,但我真的希望其中一行文字是粗体突出的,所以我认为我这样做的最好方法是使用第二个textview,并且然后在其中设置它。我已经看了很多例子,但我无法弄清楚如何把它放进去。 MainActivity -

ExpandableListAdapter listAdapter;
ExpandableListView experienceExpList;
List<String> experienceHeaders;
HashMap<String, List<String>> experienceChild;

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

    experienceExpList = (ExpandableListView) findViewById(R.id.lvExp); //call list view
    prepareListData(); //get the list data function

    listAdapter = new ExpandableListAdapter(this, experienceHeaders, experienceChild); //creates list adapter
    experienceExpList.setAdapter(listAdapter); //set list adapter

    //eventually onclick listener to purchase experience
}

private void prepareListData() {
    experienceHeaders = new ArrayList<String>();
    experienceChild = new HashMap<String, List<String>>();

    // Adding child data
    experienceHeaders.add("Experiences");
    List<String> experiences = new ArrayList<String>();

    try {

        JSONArray mainNode = new JSONArray(loadJSONFromAsset()); // call the connection to json


        if(mainNode != null) //puts the values into an array
        {
            for(int i=0;i<mainNode.length();i++)
            {
                JSONObject eachObject = mainNode.getJSONObject(i);

                String type = eachObject.getString("type");

                if(type.equals("Experience"))
                {
                    String name = eachObject.getString("name");
                    String price = eachObject.getString("price");
                    String description = eachObject.getString("description");

                    String experience = (name + "\n" + price + "\n" + description);

                    experiences.add(experience);
                }
            }
        }
    }
    catch (JSONException e)
    {
        e.printStackTrace();
    }

    experienceChild.put(experienceHeaders.get(0), experiences); // Header, Child data
}

除此之外,我还有我的适配器,它有get子视图方法

@Override
public View getChildView(int groupPosition, final int childPosition,
                         boolean isLastChild, View convertView, ViewGroup parent) {

    final String childText = (String) getChild(groupPosition, childPosition);

    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this._context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.exp_list_item, null);
    }

    TextView txtListChild = (TextView) convertView
            .findViewById(R.id.lblListItem);

    TextView txtListChild2 = (TextView) convertView
            .findViewById(R.id.lblListItem2);

    txtListChild.setText(childText); //want name here
    txtListChild2.setText( ); //price and description here
    return convertView;
}

在我的列表项(子)xml布局中,我有一个文本视图lvlListItem,但我真正想做的是设置第二个,它只显示MainActivity.java中的名称String name = eachObject.getString("name");

任何关于如何设置第二个文本视图的帮助都会受到赞赏,因为我似乎无法想象如何将其与我所拥有的相结合。

列出项目xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="55dip"
android:orientation="vertical" >

<TextView
    android:id="@+id/lblListItem"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textSize="17dip"
    android:paddingTop="5dp"
    android:paddingBottom="5dp"
    android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft" />

<TextView
    android:id="@+id/lblListItem2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textSize="17dip"
    android:paddingTop="5dp"
    android:paddingBottom="5dp"
    android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft" />

1 个答案:

答案 0 :(得分:0)

将另一个文本视图添加到布局文件exp_list_item

在主活动中,您已将所需的字符串添加到适配器。因此,在适配器中,拆分字符串并将名称分配给一个textView,将其余字符串分配给另一个。

String[] data = childText.split("\n");

String name = data[0];
String restOfInfo = data[1] + "\n" + data[2];