Saraschandraa
Saraschandraa

Reputation: 496

Converting String to Arraylist<string> in Android

I am trying to enter data dynamically in the Listview. I am using a second class file as dialog to get the data and passing the data through Intent. The data passed is in the form of string while the data in the arrayadapter that is to be displayed in the Listview is in the form of Arraylist. Please help me to solve this.

Thanks in advance.

I am attaching the code below

MainActivity.class

package com.example.listview;

import java.util.ArrayList;

import android.R.string;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.Intent;
import android.text.method.DialerKeyListener;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class MainActivity extends Activity 
{


    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final ListView lv = (ListView) findViewById(R.id.listView1);
        final Button btn = (Button) findViewById(R.id.button1);
        final EditText input = (EditText) findViewById(R.id.editText1);

        btn.setOnClickListener(new OnClickListener() 
        {

            @Override
            public void onClick(View v) 
            {

//              array.add(input.getText().toString());
//              adapter.notifyDataSetChanged();
                Intent i = new Intent(MainActivity.this,adddialog.class);
                startActivity(i);

            }
        });
        ArrayList<String> array = new ArrayList<String>();
        final ArrayAdapter<String> adapter = new ArrayAdapter<String> (MainActivity.this,android.R.layout.simple_list_item_1,array); 
        lv.setAdapter(adapter);

        Intent i2 = getIntent();
        String n = i2.getStringExtra("name");
        array.add(n);
        adapter.notifyDataSetChanged();


    }






    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

Dialog for adding

Dialog.class

package com.example.listview;

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

public class adddialog extends Activity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.adddialog);
        final EditText in = (EditText) findViewById(R.id.editText1);
        final Button btn1 = (Button) findViewById(R.id.enter);

        btn1.setOnClickListener(new OnClickListener() 
        {

            @Override
            public void onClick(View arg0) 
            {

                Intent i1 = new Intent(adddialog.this,MainActivity.class);
                i1.putExtra("name", in.getText().toString());
                startActivity(i1);
            }
        });
    }
}

Upvotes: 2

Views: 1151

Answers (1)

MDrabic
MDrabic

Reputation: 917

Start Activities and Getting Results

There is a function in Android that already supports the idea of getting results from an Activity. An explanation on how to do this can be found in the section titled Starting Activities and Getting Results here. There is also a code sample.

Sometimes you want to get a result back from an activity when it ends. For example, you may start an activity that lets the user pick a person in a list of contacts; when it ends, it returns the person that was selected. To do this, you call the startActivityForResult(Intent, int) version with a second integer parameter identifying the call. The result will come back through your onActivityResult(int, int, Intent) method.

When an activity exits, it can call setResult(int) to return data back to its parent. It must always supply a result code, which can be the standard results RESULT_CANCELED, RESULT_OK, or any custom values starting at RESULT_FIRST_USER. In addition, it can optionally return back an Intent containing any additional data it wants. All of this information appears back on the parent's Activity.onActivityResult(), along with the integer identifier it originally supplied.

Summary

To sum it up, you should use startActivityForResult(i1) and the implement the onActivityResult() method in MainActivity.java to get the data the user entered in the EditText. The onActivityResult() implementation would look something like this:

    // Get the result of the addDialog activity.
    protected void onActivityResult(int reqCode, int resultCode, Intent data) {
        String n = data.getStringExtra("name");
        myArrayAdapter.add(n);
        myArrayAdapter.notifyDataSetChanged();
    }

Upvotes: 1

Related Questions