ListView没有显示整个ArrayList

时间:2017-12-17 12:15:48

标签: android listview arraylist

我创建了admin_content.xml布局并将ListView放在该布局中。还有一个按钮可以将新列表项添加到该ListView。这个布局的java文件是AdminContent.java,我在这个文件中创建了我的适配器。当我点击"添加"按钮它转到add_school_info.xml布局,我从Bundle类获取此布局的信息,并将其发送到我的AdminContent.java文件。但是,当我添加新的学校信息时,它只显示最后一个条目。以下是我到目前为止所做的事情:

admin_content.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/adminTab"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <!-- Informations -->
    <ListView
        android:id="@+id/infoList"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_margin="10dp"
        android:layout_weight="6">

    </ListView>

    <!-- Buttons -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">

        <Button
            android:id="@+id/addAdmin"
            android:text="@string/btn_add"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textColor="#fff"
            android:background="@color/success" />
        <Button
            android:id="@+id/updateAdmin"
            android:text="@string/btn_update"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textColor="#fff"
            android:background="@color/primary" />
        <Button
            android:id="@+id/deleteAdmin"
            android:text="@string/btn_search"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textColor="#fff"
            android:background="@color/info" />
        <Button
            android:id="@+id/cancelAdmin"
            android:text="@string/btn_delete"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textColor="#fff"
            android:background="@color/danger" />
    </LinearLayout>
    <!-- #END Buttons -->

</LinearLayout>

AdminContent.java

package com.example.android.students;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import java.util.ArrayList;


public class AdminContent extends Activity {

    private Button addAdmin, cancelAdmin;
    private ArrayList<SchoolInfos> myArrayList = new ArrayList<SchoolInfos>();
    private ListView infoList;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.admin_content);
        addClickListener();

        Bundle bundle = getIntent().getExtras();
        String faculty = bundle.getString("Faculty");
        String department = bundle.getString("Department");
        String advisor = bundle.getString("Advisor");
        myArrayList.add(new SchoolInfos(faculty, department, advisor));


        infoList = (ListView) findViewById(R.id.infoList);
        ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, myArrayList);
        infoList.setAdapter(adapter);



    }

    public void addClickListener() {
        final Context context = this;
        addAdmin = (Button) findViewById(R.id.addAdmin);
        addAdmin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, AdminTab.class);
                startActivity(intent);
            }
        });

        cancelAdmin = (Button) findViewById(R.id.cancelAdmin);
        cancelAdmin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, MainActivity.class);
                startActivity(intent);
            }
        });

    }
}

add_school_info.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/add_school_info"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <!-- Registration Form -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="6"
        android:paddingHorizontal="20dp"
        android:orientation="vertical">

        <!-- Faculty -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal">

            <TextView
                android:text="@string/txt_faculty"
                android:textStyle="bold"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center_vertical"/>
            <EditText
                android:id="@+id/editFaculty"
                android:text="Hey!"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"/>
        </LinearLayout>

        <!-- Last Name -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal">

            <TextView
                android:text="@string/txt_department"
                android:textStyle="bold"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center_vertical"/>
            <EditText
                android:id="@+id/editDepartment"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"/>
        </LinearLayout>

        <!-- Gender -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal">

            <TextView
                android:text="@string/txt_advisor"
                android:textStyle="bold"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center_vertical"/>
            <EditText
                android:id="@+id/editAdvisor"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"/>
        </LinearLayout>

    </LinearLayout>

    <!-- Buttons -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <Button
                android:id="@+id/addInfo"
                android:text="@string/btn_add"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:textColor="#fff"
                android:background="@color/success"/>
            <Button
                android:id="@+id/clearInfo"
                android:text="@string/btn_clear"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:textColor="#333"
                android:background="@color/warning"/>
            <Button
                android:id="@+id/cancelInfo"
                android:text="@string/btn_cancel"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:textColor="#fff"
                android:background="@color/danger"/>
        </LinearLayout>
    </LinearLayout>
    <!-- #END Buttons -->
</LinearLayout>

AdminTab.java :此文件从EditText字段获取数据,并通过Bundle类将其发送到AdminContent.java文件。

package com.example.android.students;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class AdminTab extends Activity {

    private EditText editFaculty, editDepartment, editAdvisor;
    private Button clearInfo, cancelInfo, addInfo;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_school_info);

        addClickListener();
    }

    public void addClickListener() {
        final Context context = this;

        addInfo = (Button) findViewById(R.id.addInfo);
        clearInfo = (Button) findViewById(R.id.clearInfo);
        cancelInfo = (Button) findViewById(R.id.cancelInfo);

        editFaculty = (EditText) findViewById(R.id.editFaculty);
        editDepartment = (EditText) findViewById(R.id.editDepartment);
        editAdvisor = (EditText) findViewById(R.id.editAdvisor);

        addInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, AdminContent.class);
                String faculty = editFaculty.getText().toString();
                String department = editDepartment.getText().toString();
                String advisor = editAdvisor.getText().toString();

                Bundle bundle = new Bundle();
                bundle.putString("Faculty", faculty);
                bundle.putString("Department", department);
                bundle.putString("Advisor", advisor);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });

        clearInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                editFaculty.setText(null);
                editDepartment.setText(null);
                editAdvisor.setText(null);
            }
        });

        cancelInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, MainActivity.class);
                startActivity(intent);
            }
        });
    }
}

最后,这是我的SchoolInfo.java类,它将数据放入我的ArrayList。

package com.example.android.students;


public class SchoolInfos {
    private String faculty, department, advisor;
    public SchoolInfos() {

    }
    public SchoolInfos(String faculty, String department, String advisor) {
        this.faculty = faculty;
        this.department = department;
        this.advisor = advisor;
    }

    public String getFaculty() {
        return faculty;
    }

    public void setFaculty(String faculty) {
        this.faculty = faculty;
    }

    public String getDepartment() {
        return department;
    }

    public void setDepartment(String department) {
        this.department = department;
    }

    public String getAdvisor() {
        return advisor;
    }

    public void setAdvisor(String advisor) {
        this.advisor = advisor;
    }

    @Override
    public String toString() {
        return "SchoolInfos{" +
                "faculty='" + faculty + '\'' +
                ", department='" + department + '\'' +
                ", advisor='" + advisor + '\'' +
                '}';
    }
}

问题是,当我添加一个带有add_school_info.xml布局的新条目时,它实际上并没有显示我的整个ArrayList。它只显示最后一个条目。

1 个答案:

答案 0 :(得分:1)

添加此值&amp; AdminContent.Java

中的方法
static final int PICK_ADMIN_DATA = 1;  // The request code
ArrayAdapter adapter; // declare globally 

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

if (requestCode == PICK_ADMIN_DATA) {
    // Make sure the request was successful
    if (resultCode == RESULT_OK) {

    }
}
}

更改此方法

 addAdmin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(context, AdminTab.class);
            startActivity(intent);
        }
    });

 addAdmin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(context, AdminTab.class);
            startActivityForResult(intent, PICK_ADMIN_DATA);
        }
    });

AdminTab.Java 中将结果设置为意图。

addInfo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

Intent intent = new Intent();
String faculty = editFaculty.getText().toString();
String department = editDepartment.getText().toString();
String advisor = editAdvisor.getText().toString();
Bundle bundle = new Bundle();
bundle.putString("Faculty", faculty);
bundle.putString("Department", department);
bundle.putString("Advisor", advisor);
intent.putExtras(bundle);
setResult(Activity.RESULT_OK,intent);
finish();
}
}

然后从 AdminContent.Java OnActivityResult方法获得结果

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

if (requestCode == PICK_ADMIN_DATA) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {

    Bundle bundle = data.getExtras();
    String faculty = bundle.getString("Faculty");
    String department = bundle.getString("Department");
    String advisor = bundle.getString("Advisor");
    myArrayList.add(new SchoolInfos(faculty, department, advisor));
    adapter.notifyDataSetChanged();
}
}
}

希望它有所帮助。!