Trying to send an array from an activity to another to use it with ActivityList

I am creating an android browser. I have a Favorites Page created with a ListActivity which gets its elements from an array named "elements". This is the code for the Favorites Activity:

package com.example.browser3;



import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class Favorite extends ListActivity {

String[] elements={};
ArrayAdapter<String> adapter;
HomePage object=new HomePage();

public void setElements(int position,String element) {
    elements[position]=element;
}

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    elements = getIntent().getStringArrayExtra("name");
    setListAdapter(new ArrayAdapter<String>(Favorite.this ,      android.R.layout.simple_list_item_1 , elements));

}

protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);

    try{
    Class ourClass = Class.forName("com.example.browser3.HomePage");// ce e aici aia porneste la click
    Intent ourIntent = new Intent(Favorite.this , ourClass);
    String s=elements[position];
    ourIntent.putExtra("name", s);
    startActivity(ourIntent);


            }
    catch(ClassNotFoundException e){
        e.printStackTrace();
    }
}
}

The array I am trying to send is from the Settings Activity, getting the data from an EditText, and when clicking the add button, to send the data in the new activity , but to also write it in the file to remain there after you reopen the browser.

So, my question is, why doesn't this work? Can someone help me? But please, be explicit. I watched several tutorials and it seems fine. Code for Settings Activity:

package com.example.browser3;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

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

public class Settings2 extends Activity{

EditText etAdress;
Button bAdd;
Button bRemove;
Button bMenu;


protected void onCreate(Bundle savedInstanceState) {
    setContentView(R.layout.settings2);
    super.onCreate(savedInstanceState);

    bMenu=(Button)findViewById(R.id.bMenu);

    bMenu.setOnClickListener(new View.OnClickListener() {


        public void onClick(View v) {
            clickHome();                
        }
    });

    etAdress=(EditText) findViewById(R.id.etAdress);

    bAdd=(Button) findViewById(R.id.bAdd);
    bAdd.setOnClickListener(new View.OnClickListener() {


        public void onClick(View v) {
            try {
                writeFile("lala.txt",etAdress);
                String[] s={};
                readFile("lala.txt",s);
                Class ourClass = Class.forName("com.example.browser3.Favorite");
                Intent ourIntent = new Intent(Settings2.this , ourClass);
                ourIntent.putExtra("name", s);
                startActivity(ourIntent);
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


        }
    });

    bRemove=(Button) findViewById(R.id.bRemove);
    bRemove.setOnClickListener(new View.OnClickListener() {


        public void onClick(View v) {


        }
    });
}

private void clickHome(){
    startActivity(new Intent("com.example.browser3.MENU"));
}

public void readFile(String fileName,String[] w){
    try {
        InputStream in=openFileInput(fileName);
        if(in!=null){
            InputStreamReader reader= new InputStreamReader(in);
            BufferedReader buffreader= new BufferedReader(reader);

            StringBuilder builder= new StringBuilder();
            String str;
            while((str=buffreader.readLine())!=null){
                builder.append(str+ "\n");

                String whole= builder.toString();
                String[] split=whole.split("\\r?\\n");
                 for (int i = 0; i < split.length; i++) {
                        w = split;
                    }

            }
            in.close();

        }
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


}
public void writeFile(String fileName, EditText x) {

    try {
        OutputStreamWriter out = new OutputStreamWriter(openFileOutput(fileName, MODE_APPEND));
        out.write(x.getText().toString() + "\n");

        out.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
}

Upvotes: 0

Views: 32

Answers (1)

RajeshVijayakumar
RajeshVijayakumar

Reputation: 10650

I hope this example will help you :

Passing ArrayList from one activity to another activity

Passing Object from one activity to another activity

Create Custom Class for that as follows :

public class SerIntArrData implements Parcelable{

public int[] intArr;

public int[] getIntArr() {
  return intArr;
}

public void setIntArr(int[] intArr) {
  this.intArr = intArr;
}

public SerIntArrData(int size) {
  intArr = new int[size];
}

public SerIntArrData(Parcel in) {
  intArr = (int[]) in.readSerializable();
}

@Override
public int describeContents() {
   return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
   dest.writeSerializable(intArr);
}


public static final Parcelable.Creator<SerIntArrData> CREATOR = new Parcelable.Creator<SerIntArrData>() {

@Override
public SerIntArrData createFromParcel(Parcel in) {
    return new SerIntArrData(in);
}

@Override
public SerIntArrData[] newArray(int size) {
    return new SerIntArrData[size];
}
};

}

Set Value in Parcelable

 SerIntArrData mParcelable = new SerIntArrData();
 mParcelable.setIntArr(intArrvalue);

 Intent i = new Intent(this, TargetActivity.class);
 i.putExtra("parcel",mParcelable);
 startActivity(i);

Read the data in the target activity

 SerIntArrData myParcelable = extras.getParcelable("parcel");
 intArrValue = myParcelable.getIntArr();

Upvotes: 1

Related Questions