[英]Android: ListView with TextView header problem
我的主要活动是:
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));
我想在顶部显示一个消息标题的用户列表。 但是我遇到了这些问题。 问题出在哪里,解决方案在哪里。
在您的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。
我的猜测是textview覆盖了整个屏幕。 因此,列表仍然不可见。
要将标题添加到列表视图-
TextView tv = new TextView(this);
tv.setText("Header Text");
listView.addHeaderView(tv);
这对我有用。 希望能帮助到你。
<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>
请参考此。
它使用ListAdaptor和Icon,Textview。 您可以了解未获得预期输出的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.