我试图在运行时动态地将元素添加到 ListView。这是我的代码。
<?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:id="@+id/category_row"
android:orientation="horizontal" >
<!-- Contents will be added at Runtime -->
</LinearLayout>
我的适配器覆盖的 getView 函数
@Override
public View getView(final int position, final View convertView,
final ViewGroup parent) {
View searchResultsRow = convertView;
LinearLayout linearLayout;
TextView e1,e2,e3,e4;
if (searchResultsRow == null) {
LayoutInflater layoutInflator = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
searchResultsRow = layoutInflator.inflate(
R.layout.categories_row_object, null);
linearLayout = (LinearLayout) searchResultsRow.findViewById(R.id.category_row);
}
e1 = new TextView(context);
e2 = new TextView(context);
e3 = new TextView(context);
e4 = new TextView(context);
e1 = new TextView(context);
e1.setText("Fashion");
linearLayout.addView(e1);
e2 = new TextView(context);
e2.setText("Men");
linearLayout.addView(e2);
e3 = new TextView(context);
e3.setText("Casual Wear");
linearLayout.addView(e3);
e4 = new TextView(context);
e4.setText("Jeans");
linearLayout.addView(e4);
return searchResultsRow;
}
现在,前几行似乎混乱了会发生什么。就像多行已添加到同一行。我究竟做错了什么 ?
亲切的问候,