0

In my android app I need to pass the data of listview to another activity,for example If I click the Item "Food" in listview,I want get that "Food" in the textview of another activity.

Here is my code..

public class list extends Activity {

String selected_id = "";
SimpleCursorAdapter adapter;
DBhelper helper;
SQLiteDatabase db;
Button btnd;
String budget;


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

    helper = new DBhelper(this);



       Listview rldlist = (ListView) findViewById(R.id.rldlist);
       TextView ed= (TextView) findViewById(R.id.addbud);
       rldlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {


                Cursor row = (Cursor) adapter.getItemAtPosition(position);
                selected_id = row.getString(0);
                budget = row.getString(1);
                System.out.print("budget is"+budget);
                ed.setText(budget);

            }
        });

In the above code,TextView ed= (TextView) findViewById(R.id.addbud); is referring the textview from another activity.

But problem now is,The TextView remains empty.

Below one is the code to go another activity.

rldlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
                Intent myIntent = new Intent(list.this, addbudget.class);
                list.this.startActivity(myIntent);
            }
        });
xyz
  • 71
  • 3
  • 12
  • 2
    [This](http://stackoverflow.com/questions/4848260/passing-data-from-list-view-to-another-activity) will help you out. It has been asked and answered. – user5432778 Oct 12 '15 at 02:09

1 Answers1

1

Just pass the value using 'Intent.PutExtra()' method and retrieve data in 'onCreate' method of the other activity.

rldlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override public void onItemClick(AdapterView arg0, View arg1, int position, long arg3) {

            Intent myIntent = new Intent(list.this, addbudget.class);
myIntent .putExtra("passed data key",budget);
            startActivity(myIntent);



        }
    });

Now, in your 'Addbudget'activity's onCreate() method,

Bundle data_from_list= getIntent().getExtras();
String value_in_tv= data_from_list.getString("passed data key");
TextView ed= (TextView) findViewById(R.id.addbud);
 ed.setText(value_in_tv);

Done. :)

DGN
  • 702
  • 3
  • 12
  • Ok. Please verify whether the data is retrieved in 'Addbudget' activity's ''onCreate". Please log the value and check. And check whether the TextView id is correct – DGN Oct 12 '15 at 03:05
  • ID is correct,but log the value mean System.out.print? I tried that too,but it's not printing anything – xyz Oct 12 '15 at 03:12
  • Sry it's printing "NULL" value – xyz Oct 12 '15 at 03:21
  • Oh..so there lies the problem.. Correct that and I think It is done then. – DGN Oct 12 '15 at 03:22
  • The problem now is, It's printing the budget value correctly,but in 'Addbudget' activity it's printing NULL .plz help to solve this – xyz Oct 12 '15 at 03:30
  • yes,when I hardcode the value to budget above the oncreate method of current activity,that value is retrieving in the textview of 'addbudget' Activity – xyz Oct 12 '15 at 03:50
  • value mean I just hard-code like String budget="test" ,then I can retrieve it in ADDBUDGET Activity. – xyz Oct 12 '15 at 04:08
  • Thank you so much for your immediate replies.i got it.all are working perfectly now(in my code i called the onclick listener two times,afte I correct it,all are woking) – xyz Oct 12 '15 at 05:30