簡體   English   中英

Android ExpandableListView中每行兩個TextView

[英]Two TextViews per row in Android ExpandableListView

我的應用程序中有一個可擴展的文本視圖。 我可以在一個文本視圖中顯示所有信息,但我確實希望其中一行文本能夠以粗體顯示,所以我想最好的方法是使用第二個文本視圖。然后在其中設置它。 我看了很多示例,但我不太清楚如何將其放入。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 child view方法

@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,但我真正想做的是設置第二個視圖,該視圖僅顯示名稱String name = eachObject.getString("name"); 進入MainActivity.java。

對於如何設置第二個文本視圖的任何幫助將不勝感激,因為我似乎無法弄清楚如何將其與已有的視圖結合起來。

清單項目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" />

將另一個文本視圖添加到布局文件exp_list_item

在您的主要活動中,您已經將所需的字符串添加到適配器中。 因此,在適配器中,拆分字符串,然后將名稱分配給一個textView,然后將字符串的其余部分分配給另一個。

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM