Passing Data to listview from other activity using

2019-09-20 03:03发布

问题:

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>

回答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)