简体   繁体   中英

Adding views to another line (another LinearLayout)?

So I'm creating a small application, and thanks to this site, I've progressed on it a lot more. The idea of it is that I will just have a button, and when it is clicked, it will add a new row with two "EditText" and one "CheckBox". I've somewhat accomplished this through dynamically adding these with a custom ListView and a custom adapter. I'm still iffy on custom ListView's and adapters.

Anyways, I've gotten it to add the two EditText and one CheckBox when the button is clicked, but it will not add any more after this (this is a separate problem though). The problem I'm having, is that it will add them on the same row as the button. This is where I get lost. I'm not sure how to get this custom ListView to display on a new row, rather than the same row as the button.

在此处输入图片说明

Now, here is my MainActivity code:

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

Here is my "MyListArrayAdapter" class (my custom adapter):

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.view.LayoutInflater;
import android.widget.CheckBox;
import android.widget.EditText;

import java.util.List;

 /**
 * Created by Thomas on 5/21/2016.
 */
public class MyListArrayAdapter extends BaseAdapter {

    LayoutInflater inflater;
    List<CustomRow> rows;

    public MyListArrayAdapter(Activity context, List rows){
        super();

        this.rows = rows;
        this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }


    @Override
    public int getCount() {
        return rows.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        CustomRow row = rows.get(position);

        View v = convertView;

        if (convertView==null) {
            v = inflater.inflate(R.layout.new_course_row, null);
        }

        EditText course = (EditText) v.findViewById(R.id.course);
        EditText credits = (EditText) v.findViewById(R.id.credits);
        CheckBox checkBox = (CheckBox) v.findViewById(R.id.checkBox);

        course.setHint("Enter Course");
        credits.setHint("Enter Credits");
        checkBox.setText("Check if complete");
        course.setMaxHeight(150);
        credits.setMaxHeight(150);
        checkBox.setMaxHeight(150);

        return v;
    }
}

My "CustomRow" class (the custom class with the two EditText and one CheckBox):

import android.content.Context;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;

 /**
 * Created by Thomas on 5/21/2016.
 */
public class CustomRow extends LinearLayout{


        public CustomRow(Context context) {
        super(context);

        setOrientation(LinearLayout.HORIZONTAL);

        EditText course = new EditText(context);
        EditText credits = new EditText(context);
        CheckBox checkBox = new CheckBox(context);

        addView(course);
        addView(credits);
        addView(checkBox);
    }
}

My "new_course_row.xml" file:

<?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>

Lastly, my "activity_main.xml":

<?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>

Thank you in advance everyone! I've been stuck here for a few days. Not looking for anyone to write the app for me, just looking for some direction.

make sure that after adding a button you have called these two methods in your adapter to update the list

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

If your code is working properly and adding row on same row, then please check below solution.

As you have write below line ,

List<CustomRow> rows = new ArrayList<CustomRow>();

in OnButton CLick event, so whenever button will be clicked, it will initialise rows again and remove all previous records from it, so just write above line at top or in onCreate() method after setContentView, then you will get all rows on click.

Hope this will help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM