は私のような主な活性を有する:アンドロイド:の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>
私の問題は
このコードを実行すると、ウェルカムメッセージだけが表示されます(リスト内の項目は表示されません)。
私はTestActivityで
TextView tv=(TextView)findViewById(R.id.welcomeMsg);
lv.addHeaderView(tv);
行のコメントを解除するとき、私は例外
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));
次私はメッセージでユーザーのリストを表示したい取得 - 頭の上に。しかし、私はこれらの問題があります。どこに問題があり、解決策は何か。
「match_parent」のような例外があります。 "wrap_content"を使用してください。また、 "fill_parent"の代わりに、行xmlに "wrap_content"を使用してください。 – Arslan
私はそれを試みました。まだ解決策はありません。 – gtiwari333