简体   繁体   中英

How do I add to ListView and not replace it?

I'm fairly new to Android and I'm trying to add things to a ListView from another Activity. I am able to add things to the list, but each time I add to the list it replaces what was previously entered.

I have tried using notifyDataSetChanged() as well as invalidateViews() but neither seem to work?

Here are the related files:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    android:weightSum="1"
    android:orientation="vertical">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/add_button"
        android:id="@+id/add_button"
        android:layout_gravity="bottom"
        android:onClick="onGetRecipeClick"/>

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/recipe_name_list"
        android:layout_marginTop="20dp"
        android:background="#000000" />
</LinearLayout>

second_layout.xml

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

        <EditText
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:id="@+id/recipe_name_edit_text"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/enter_recipe_button_text"
            android:id="@+id/save_button"
            android:onClick="onSendRecipeName"/>

</LinearLayout>

MainActivity.java

package com.jamielammas.recipeexample;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends ActionBarActivity {

    public static ListView listView;
    public static ArrayAdapter adapter;

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

    public void onGetRecipeClick(View view) {
        Intent getRecipeScreenIntent;
        getRecipeScreenIntent = new Intent(this, SecondScreen.class);
        final int result = 1;
        getRecipeScreenIntent.putExtra("callingActivity", "MainActivity");
        startActivityForResult(getRecipeScreenIntent, result);
    }

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

        listView = (ListView) findViewById(R.id.recipe_name_list);
        adapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1, SecondScreen.stringArray);
        listView.setAdapter(adapter);

        adapter.notifyDataSetChanged();
    }
}

SecondScreen.java

package com.jamielammas.recipeexample;

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

import java.util.ArrayList;

public class SecondScreen extends Activity {

    public static EditText editText;
    public static ArrayList<String> stringArray;

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

    public void onSendRecipeName(View view) {

        editText = (EditText) findViewById(R.id.recipe_name_edit_text);

        stringArray = new ArrayList<>();
        stringArray.add(SecondScreen.editText.getText().toString());

        Intent goingBack = new Intent();
        setResult(RESULT_OK, goingBack);
        finish();
    }
}

Any help would be greatly appreciated!

You can do something like this:

SecondScreen

 public void onSendRecipeName(View view) {
    editText = (EditText) findViewById(R.id.recipe_name_edit_text);
    Intent goingBack = new Intent();
    goingBack.putExtra("newItem",editText.getText().toString());
    setResult(RESULT_OK, goingBack);
    finish();
}

MainActivity

ArrayList<String> arrayUsedToCreateTheAdapter = new ArrayList<>();

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode == RESULT_OK && data.hasExtra("newItem")){
       arrayUsedToCreateTheAdapter.add(data.getStringExtra("newItem"););
       adapter.notifyDataSetChanged();
    }
}

You are creating a new adapter instance in onActivityResult and the you call listView.setAdapter(adapter); , which resets adapter, that's why you are not adding items to you adapter.

So you in you MainActivity need to move

    listView = (ListView) findViewById(R.id.recipe_name_list);
// notice I changed here so that it is not populated yet
    adapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1); 
    listView.setAdapter(adapter);

to onCreate method.

And in your onActivityResult() you will call adapter.addAll() to adapter.add() to add elements

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