在Android中使用linearlayouts以编程方式创建行

在Android中使用linearlayouts以编程方式创建行,android,android-linearlayout,Android,Android Linearlayout,我可能是走错了路,但我的代码几乎就在那里,所以我不认为我离得太远。我正在尝试以编程方式创建水平线性布局,其中包含两个垂直线性布局。我已经在xml中创建了它来测试它,但是当我从db加载数据时,我需要动态地创建行 以下是我的xml的外观以及我试图通过编程方式创建的内容: <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" androi

我可能是走错了路,但我的代码几乎就在那里,所以我不认为我离得太远。我正在尝试以编程方式创建水平线性布局,其中包含两个垂直线性布局。我已经在xml中创建了它来测试它,但是当我从db加载数据时,我需要动态地创建行

以下是我的xml的外观以及我试图通过编程方式创建的内容:

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

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:layout_marginLeft="25dip"
            android:layout_marginRight="10dip"
            android:background="@drawable/rounded_edges_white">

            <ImageView
                android:layout_width="120dp"
                android:layout_height="80dp"
                android:id="@+id/imageView3"
                android:contentDescription="personIcon"
                android:src="@drawable/person_icon"
                android:layout_gravity="center" />
            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="#f1f1f1"
                android:layout_marginBottom="10dip" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Danielle"
                android:id="@+id/textView81"
                android:layout_gravity="center_horizontal" />
        </LinearLayout>

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="fill_parent"
            android:layout_marginLeft="10dip"
            android:layout_marginRight="25dip"
            android:background="@drawable/rounded_edges_white">

            <ImageView
                android:layout_width="120dp"
                android:layout_height="80dp"
                android:id="@+id/imageView2"
                android:contentDescription="personIcon"
                android:src="@drawable/person_icon"
                android:layout_gravity="center" />
            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="#f1f1f1"
                android:layout_marginBottom="10dip" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Danielle"
                android:id="@+id/textView8"
                android:layout_gravity="center_horizontal" />
        </LinearLayout>
    </LinearLayout>
因此,它击中了所有正确的位置,并表示它正在创建行并添加到父级。这是最接近我正在做的事情

但他并没有真正动态地添加额外的行。如果他过去的话,他有第二次和第三次被解雇

这就是我的目标(根据需要重复多次):

我在数据库中有4个项目,但只显示第1行和2个项目


谢谢

这与您的代码有关

//add to parent view
if(count == 1){
  Log.d("add to parent", "true");
     parentLayout.addView(rowLayout);
  }
 count++;
 totalCount++;
你应该把它改成

if(count % 2 == 1) {

这将每隔2项添加一行

实际上,即使您从数据库中提取数据,也不需要动态创建布局。您可以使用自定义listview非常整洁地显示数据库数据。我很感兴趣,我该如何做?您可以查阅使用自定义对象创建自定义listview的教程。因为您希望每行有2列,所以我建议您使用GridView并将列计数指定为2。制作自定义GridView与制作自定义ListView非常相似。稍后我将尝试编写一个示例,我只需要检查我的代码。我似乎在查找代码时遇到问题。:/你可以试着用谷歌搜索一下如何自定义gridView,我相信你会找到一些例子,其中使用了自定义对象(反映数据库条目)以及自定义gridView适配器。在试图弄清楚这一点的时候,我看到了一些gridView的东西,当我将它改为(count%=1)时,我会看看我是否能弄清楚它是否表示需要布尔值。我的日志文件显示它每2行创建一行。对不起,如果(计数%2==1)与我之前的操作相同,那么该行应该是if(计数%2==1)。不管是哪种方式,它都会进入第二行的代码,第二行就是不显示。我在里面放了一个log.d以确保它在if语句中。我觉得它是在添加它,但是项目是不可见的或者只是没有显示??如果(count==2)count=0,您应该删除该行;但这不是问题所在。if(计数%2==1)中的代码正在正确的时间运行。请看上面我的日志。Log.d(“添加到父项”、“真实”);按正确的顺序运行两次。我删除了if(count==2)count=0;当它已经有一个父项时,它在添加子项时出错。
            |---------------|   |---------------|
            |               |   |               |
            |               |   |               |
            |               |   |               |
            |               |   |               |
            |---------------|   |---------------|

            |---------------|   |---------------|
            |               |   |               |
            |               |   |               |
            |               |   |               |
            |               |   |               |
            |---------------|   |---------------|
//add to parent view
if(count == 1){
  Log.d("add to parent", "true");
     parentLayout.addView(rowLayout);
  }
 count++;
 totalCount++;
if(count % 2 == 1) {