0

I have created admin_content.xml layout and put ListView in that layout. There is also a button to add new list item to that ListView. This layout's java file is AdminContent.java and I created my adapter inside this file. When I click to "Add" button it goes to add_school_info.xml layout and I get the information from this layout with Bundle class and send it to my AdminContent.java file. However, when I add new school info, it only shows last entry. Here is what I have done so far:

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: This file is getting data from EditText fields and send it to my AdminContent.java file via Bundle class.

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);
            }
        });
    }
}

Lastly, this is my SchoolInfo.java class which is to put data to my 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 + '\'' +
                '}';
    }
}

The problem is that, it seems when I add a new entry with add_school_info.xml layout, it actually doesn't show my whole ArrayList. it only shows last entry.

1
  • You are looping the activity each time addadmin called better you can use startactivityforresult instead of start activity. Problems occurs for each time new instance of activity is called. Commented Dec 17, 2017 at 12:58

1 Answer 1

1

Add this value & method in 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) {

    }
}
}

Change this method

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

To

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

In AdminTab.Java set result to intent.

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();
}
}

Then get result from AdminContent.Java OnActivityResult method

@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();
}
}
}

Hope it helps.!

Sign up to request clarification or add additional context in comments.

3 Comments

I didn't get any error but when I click "Add" button in my add_school_info.xml file, it didn't do anything.
add above set result to intent code to add button. check updated answer.
Welcome glad to help.! Happy coding.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.