开发者

Custom ListActivity with dynamic views for each row

I want to create custom row for ListActivity, each row contains Title and List Of SubTitles . My question is : what is the best way to create these SubTitles? I cannot add these subTitles in row.xml since they should be dynamic depending on each row.

I'm trying to write this :

class Research{
        public String title;
        public ArrayList<String> subTitles;
    }
private class MyAdapter extends ArrayAdapter<Research> {

    public MyAdapter(Context context, int textViewResourceId,ArrayList<Research> items) {
        super(context, textViewResourceId, items);

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.row, null);
        }
        Research o = items.get(position);
        if (o != null) {
            TextView tt = (TextView) v.findViewById(R.id.textVi开发者_如何学Cew1);
            if (tt != null) {
                tt.setText(o.title);
            }
        }

        /// THE PROBLEM STARTS HERE
        for(String subTitle: o.subTitles){
            TextView subTextView = new TextView(ResearchListActivity.this);
            subTextView.setText(subTitle);
            // where to add this view ? I don't see Layout here !
            parent.addView(subTextView);

        }
        //


        return v;
    }
}

the row layout :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:orientation="vertical">
    <!-- Title -->
<TextView android:id="@+id/textView1" android:layout_width="fill_parent"
    android:text="" android:layout_height="wrap_content"
    android:textColor="#ddd" android:gravity="center" android:background="#7DB5C9"></TextView>

</LinearLayout>

Is it clear ?

Thanks .


The View v is the outer element in your row.xml, the LinearLayout.

    LinearLayout l = (LinearLayout)v;

then run l.addView(...) on that layout.


You can try to add another TextView element in your row-layout.

If there is no subtitle, than the text from the second TextView is emtpy, otherwise you get the subtitle (save it in a String variable) and set the new TextView with this text.

String tmp = "";
for(String subTitle: o.subTitles){
    tmp += subTitle;
}

TextView subTextView = (TextView) v.findViewById(R.id.textView2);    

if(tmp.equals("")){
   subTextView.setText("");
} else {   
   subTextView.setText(subTitle);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜