2011-09-16 3 views
0

У меня есть основной вид деятельности, как:Android: ListView с проблемой заголовка TextView

public class TestActivity extends Activity{ 
    List<String> users; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.usersview); 
     showList(); 

    } 
    private void showList() { 
     users=new ArrayList<String>(); 
     users.add("User1"); 
     users.add("User2"); 
     users.add("User3"); 

     ListView lv=(ListView)findViewById(R.id.listv); 
//  TextView tv=(TextView)findViewById(R.id.welcomeMsg); 
//  lv.addHeaderView(tv); 
     lv.setAdapter(new ListArrAdapter(this, R.layout.userlist, users)); 
    } 

ArrayAdapter:

public class ListArrAdapter extends ArrayAdapter<String> { 
    private List<String> users; 
    private Context context; 
    private int listlayout; 

    public ListArrAdapter(Context context, int textViewResourceId, 
      List<String> users) { 
     super(context, textViewResourceId, users); 
     this.users = users; 
     listlayout = textViewResourceId; 
     this.context = context; 
    } 
    @Override 
    public View getView(int position, View convertView, ViewGroup viewGroup) { 
     String userName = users.get(position); 
     if (null == convertView) { 
      LayoutInflater inflater = (LayoutInflater) context 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = inflater.inflate(listlayout, null); 
     } 
     TextView firstName = (TextView) convertView 
       .findViewById(R.id.firstNameTv); 
     firstName.setText(userName); 
     return convertView; 
    } 
} 

usersview расположение:

<?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="match_parent" 
    android:orientation="vertical"> 
    <TextView 
     android:text="@string/header" 
     android:id="@+id/welcomeMsg" 
     android:textSize="30dp" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"></TextView> 
    <ListView 
     android:layout_height="wrap_content" 
     android:id="@+id/listv" 
     android:layout_width="match_parent"></ListView> 
</LinearLayout> 

список пользователей раскладка для каждого детали:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#EEFFEE" 
    android:orientation="vertical"> 
    <TextView 
     android:id="@+id/firstNameTv" 
     android:textSize="20dp" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"></TextView> 
</LinearLayout> 

Моя проблема:

При запуске этого кода отображается только приветственное сообщение (элементы в списке не отображаются).

, когда я раскомментировать строки

TextView tv=(TextView)findViewById(R.id.welcomeMsg); 
    lv.addHeaderView(tv); 

в TestActivity, я получаю следующее исключение

09-16 19:45:10.680: ERROR/AndroidRuntime(31044): Caused by: java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams 

в строке

lv.setAdapter(new ListArrAdapter(this, R.layout.userlist, users)); 

Я хочу, чтобы отобразить список пользователей с сообщением наверху. Но я получил эту проблему. Где проблема и каково решение.

+0

Исключение похоже на «match_parent». Попробуйте использовать "wrap_content". Кроме того, вместо «fill_parent» используйте «wrap_content» в строке xml – Arslan

+0

, я попробовал это. до сих пор нет решения. – gtiwari333

ответ

1

В макете UserView -

<TextView 
    android:text="@string/header" 
    android:id="@+id/welcomeMsg" 
    android:textSize="30dp" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"></TextView> 

Изменить макеты для wrap_content.

Я предполагаю, что текстовое покрытие покрывает весь экран. поэтому список остается невидимым.

Для добавления заголовка к вашему ListView -

TextView tv = new TextView(this); 
tv.setText("Header Text"); 
listView.addHeaderView(tv); 
+0

Я попытался изменить макет. но не решение. в текстовом представлении заголовка по-прежнему отображается «@ + id/welcomeMsg» даже после того, как я сделал: TextView tv = новый TextView (this); tv.setText («Текст заголовка»); – gtiwari333

0

Это работает для меня. Надеюсь, поможет.

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" 
    > 

    <TextView 
    android:layout_width="match_parent" 
    android:layout_height="50dp" 
    android:layout_gravity="top" 
    /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="horizontal" 
    > 

    <ListView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_gravity="top" 
     android:layout_marginTop="50dp" 
    /> 
    </LinearLayout> 
</FrameLayout> 
Смежные вопросы