Custom ListActivity с динамическими представлениями для каждой строки - PullRequest
3 голосов
/ 11 октября 2011

Я хочу создать собственную строку для ListActivity, каждая строка содержит Title и List Of SubTitles. У меня вопрос : как лучше всего создать эти SubTitles?Я не могу добавить эти субтитры в row.xml, так как они должны быть динамическими в зависимости от каждой строки.

Я пытаюсь написать это:

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.textView1);
            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;
    }
}

макет строки:

<?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>

Это понятно?

Спасибо.

Ответы [ 2 ]

1 голос
/ 11 октября 2011

View v - это внешний элемент в вашем row.xml, LinearLayout.

    LinearLayout l = (LinearLayout)v;

, затем запустите l.addView (...) для этого макета.

0 голосов
/ 11 октября 2011

Вы можете попробовать добавить еще один элемент TextView в макет строки.

Если субтитров нет, то текст из второго TextView будет emtpy, в противном случае вы получите субтитр (сохраните его в строковой переменной) и установите новый TextView с этим текстом.

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);
}
...