Java 是否将视图添加到另一条线(另一条线布局)?

Java 是否将视图添加到另一条线(另一条线布局)?,java,android,android-linearlayout,android-custom-view,Java,Android,Android Linearlayout,Android Custom View,所以我正在创建一个小应用程序,多亏了这个网站,我在这方面取得了很大的进步。它的想法是,我将只拥有一个按钮,当它被点击时,它将添加一个新行,其中包含两个“EditText”和一个“CheckBox”。通过使用自定义ListView和自定义适配器动态添加这些,我在某种程度上实现了这一点。我对自定义ListView和适配器仍有疑问 无论如何,我已经让它在点击按钮时添加了两个EditText和一个复选框,但之后它不会再添加任何内容(不过这是一个单独的问题)。我遇到的问题是,它会将它们添加到与按钮相同的行

所以我正在创建一个小应用程序,多亏了这个网站,我在这方面取得了很大的进步。它的想法是,我将只拥有一个按钮,当它被点击时,它将添加一个新行,其中包含两个“EditText”和一个“CheckBox”。通过使用自定义ListView和自定义适配器动态添加这些,我在某种程度上实现了这一点。我对自定义ListView和适配器仍有疑问

无论如何,我已经让它在点击按钮时添加了两个EditText和一个复选框,但之后它不会再添加任何内容(不过这是一个单独的问题)。我遇到的问题是,它会将它们添加到与按钮相同的行中。这就是我迷路的地方。我不知道如何让这个自定义ListView显示在新行上,而不是与按钮显示在同一行上

现在,这是我的主要活动代码:

import android.content.Context;
import android.graphics.Color;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.InputType;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.view.ViewGroup.LayoutParams;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    ArrayList<CheckBox> checkBoxes = new ArrayList<>();
    ArrayList<EditText> courses = new ArrayList<>();
    ArrayList<EditText> credits = new ArrayList<>();

    int numOfCheckBoxes = 0;

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final LinearLayout ll = (LinearLayout) findViewById(R.id.ll);
        ll.setOrientation(LinearLayout.HORIZONTAL);

        Button addCourse = new Button(this);
        addCourse.setLayoutParams(new ActionBar.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        addCourse.setText("Add Course");
        addCourse.setTextColor(Color.parseColor("#FFFFFF"));
        addCourse.setBackgroundColor(Color.parseColor("#FF0000"));
        addCourse.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ListView myListView = new ListView(MainActivity.this);
                List<CustomRow> rows = new ArrayList<CustomRow>();
                rows.add(new CustomRow(MainActivity.this));
                MyListArrayAdapter myAdapter = new MyListArrayAdapter(MainActivity.this, rows);
                myListView.setAdapter(myAdapter);
                ll.addView(myListView);
            }
        });
        ll.addView(addCourse);
    } // End of onCreate

    public void onCheckBoxChecked(View v) {
        for (int i = 0; i < numOfCheckBoxes; i++) {
            if (checkBoxes.get(i).isChecked()) {
                courses.get(i).setEnabled(false);
                courses.get(i).setFocusable(false);
                courses.get(i).setInputType(InputType.TYPE_NULL);
                credits.get(i).setEnabled(false);
                credits.get(i).setFocusable(false);
                credits.get(i).setInputType(InputType.TYPE_NULL);
            } else {
                courses.get(i).setEnabled(true);
                courses.get(i).setFocusable(true);
                courses.get(i).setFocusableInTouchMode(true);
                courses.get(i).setInputType(InputType.TYPE_CLASS_TEXT);
                credits.get(i).setEnabled(true);
                credits.get(i).setFocusableInTouchMode(true);
                credits.get(i).setFocusable(true);
                credits.get(i).setInputType(InputType.TYPE_CLASS_NUMBER);
            } // End of if else
        } // End of for loop
    } // End of onCheckBoxChecked
} // End of MainActivity
我的“new_course_row.xml”文件:


最后,我的“activity_main.xml”:



提前谢谢大家!我被困在这里好几天了。不是找人帮我写应用程序,只是找一些方向。

确保在添加按钮后,您已在适配器中调用这两个方法来更新列表

 BuddyFeedsAdapter.this.notifyDataSetChanged();
            notifyDataSetInvalidated();

如果您的代码工作正常,并且在同一行上添加行,请检查下面的解决方案

正如你在下面一行所写

List<CustomRow> rows = new ArrayList<CustomRow>();
List rows=new ArrayList();
在OnButton CLick事件中,所以无论何时单击按钮,它都将再次初始化行并从中删除所有以前的记录,所以只需在顶部的行上方或setContentView之后的onCreate()方法中写入,然后您将在单击时获得所有行


希望这能对您有所帮助。

我在应用程序中也这样做了,但我没有动态添加视图。我只是把它们放在我的XML布局中,在OnCreate()上,它将可见性设置为View.go,并在按钮onClick上设置为View.VISIBLE。为此,您需要将Listview或RecyclerView与自定义适配器一起使用。这是最简单的方法。
<?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">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/course"
        android:layout_weight="1"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/credits"
        android:layout_weight="1"/>

    <CheckBox
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/checkBox"
        android:onClick="onCheckBoxChecked"/>

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>

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

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="exporian.collegetracker.MainActivity"
        android:id="@+id/ll">

    </LinearLayout>
</ScrollView>
 BuddyFeedsAdapter.this.notifyDataSetChanged();
            notifyDataSetInvalidated();
List<CustomRow> rows = new ArrayList<CustomRow>();