0

I'm having 2 activities in my project where 1st activity has a list view and a button and 2nd activity has EditText and other info and a Button. i want to to pass data from 2nd activity when the user enters details to 1st activity's ListView. Here when I'm entering the data it is not getting displayed in the ListView.. what is the problem ?

Main Activity :-

   @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    ListView lv = (ListView) findViewById(R.id.theListView);

   if(requestCode==1) {
       if (resultCode == RESULT_OK) {

           String det_rec = data.getStringExtra("Details");
           ArrayList<String> strArr = new ArrayList<String>();
           for(int i=0;i<det_rec.length();i++) {
               strArr.add("Row :" + i);
           }
           ListAdapter adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, strArr);
           lv.setAdapter(adapter);

       }
       if (resultCode == RESULT_CANCELED) {
           return;
       }
   }

      // ListView lv = (ListView) findViewById(R.id.theListView);
      // String det_rec = data.getStringExtra("Details");
       // ArrayList<String> strArr = new ArrayList<String>();
       // for(int i=0;i<det_rec.length();i++){
       // strArr.add("Row :"+i);
  //}
    //ListAdapter adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, strArr);
   // lv.setAdapter(adapter);
}

}

Second Activity :-

   public void add_usr_tsk_btn(View view){
    EditText et = (EditText)findViewById(R.id.task_name_edit_txt);
    String detls = String.valueOf(et.getText());
    Intent goback = getIntent();
    goback.putExtra("Details",detls);
    setResult(RESULT_OK,goback);
    finish();
}

public void cancl_btn(View view) {
    Intent goBack = getIntent();
    finish();
}

}

activity main :-

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="left"
tools:context=".MainActivity">

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_weight="1">

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/theListView">

    </ListView>

</LinearLayout>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add Task"
        android:id="@+id/button1"
        android:background="@drawable/addtask"
        android:onClick="onAddCLick"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"/>

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/shwmap"
        android:text="Show Map"
        android:id="@+id/button2"
        android:onClick="onMapbtnClck"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Settings"
        android:id="@+id/button3"
        android:onClick="onSetngClck"
         />
</LinearLayout>

second activity :-

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="left">

<EditText
    android:layout_width="364dp"
    android:layout_height="wrap_content"
    android:id="@+id/task_name_edit_txt"
    android:hint="Enter your Details" />

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

<TextView
    android:layout_width="250dp"
    android:layout_height="wrap_content"
    android:text="Set Location"
    android:id="@+id/textView" />
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/Set_loc_btn"
    android:onClick="set_usr_loc"/>

</LinearLayout>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/Add_task_btn"
        android:onClick="add_usr_tsk_btn"
        android:id="@+id/add_task"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/cancel_btn"
        android:onClick="cancl_btn"
        android:id="@+id/cancel_btn"/>
    </LinearLayout>
</LinearLayout>
user2205230
  • 173
  • 1
  • 2
  • 16

1 Answers1

1

you pass your data with following code:

goback.putExtra("Details",detls);

and you got that with following code:

String det_rec = data.getStringExtra("details");

as you see Details is not same as details, change one of them ,

as you use onActivityResult you must check requestCode and resultCode, for using that field you can read This

i think you need read creating list tutorial, you can start with This, because i this implementation not worked at all:

ListAdapter adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, Integer.parseInt(det_rec));

you must pass one ArrayList and in your code ArrayList<String> to your adapter instead of Integer.parseInt(det_rec)

Community
  • 1
  • 1
Shayan Pourvatan
  • 11,898
  • 4
  • 42
  • 63
  • m unable to c the listview even if thers no data.. i have placed buttons below listview but they are above n the lsitview is not getting displayed.. wht m i missing.. ? – user2205230 Feb 09 '15 at 13:17
  • read documentation that i've post for you, you can solve your problem – Shayan Pourvatan Feb 09 '15 at 17:05
  • according to the doc have modified the code but now the project is not running.. "unfortunately the project is stopped".. sumthns goin wrong :-( – user2205230 Feb 09 '15 at 18:42
  • i dono how now wen i run my projct it is getting executed.. but m not getting the data whch i type in my edittext instead the listview shows the number of rows this is bcoz of the for loop.. n wen i remove the for loop the text whch i enter is getting displayed but if i type the data 2nd time the previous data is goin off n m able to c the new data typed.. how can i let my listview to keep my previous data too..? – user2205230 Feb 10 '15 at 12:38
  • plzz reply i need help.. :-( – user2205230 Feb 11 '15 at 19:17
  • i need your logical for helping you , without that i can't find out what is wrong with your code, post full logcat in answer part and notify me to help you – Shayan Pourvatan Feb 12 '15 at 19:49
  • 1) by default my buttons r placed on the top of the screen coz there is nothng to display in the listview i.e the listview is empty.. i want to place my buttons below on the screen whther there is data on the listview or not. – user2205230 Feb 12 '15 at 20:17
  • 2) wen i type sumthn in the 2nd activites edittext i want tht text to goto the listview whch is ther in the 1st activity n now m able to do ths but the problem is nly 1 row is getting displayed if i go to the edittext n type sumthn again the previously typed data is goin off, as of now there no errors coz m able to show nly 1 row of data at a time on my listview..how can i let my lsitview to show prevously added data..? @shayan pourvatan i ahve xplained wht xctly do i need.. thnk u.. – user2205230 Feb 12 '15 at 20:18
  • 1- in first linear layout ( parent of listView ) change `android:layout_height="wrap_content"` to `android:layout_height= 0dp` – Shayan Pourvatan Feb 12 '15 at 20:42
  • 2- you need create one ArrayList in first activity, then add your item to this list in the onActivityResult , and show new data, as you add your data to pre data you can see all data that you added in your edit text, at this tim you set edit text value as listView data so you can't see previous data, you need store them – Shayan Pourvatan Feb 12 '15 at 20:45
  • i tried of changing the listviews layout but its not happening.. n i have created the arraylist in the onActivityResult in mainactivity right ? u talkng abt tht ? – user2205230 Feb 14 '15 at 17:54
  • can i create listview programmaticly i.e wen i enter sumthn in the edit text the listview shld b created n the details shld be displayed or else the layout shld show tht thers no data.. is ths possible ? – user2205230 Feb 14 '15 at 17:55