使用目的等活动将数据传递到列表视图 [英] Passing Data to listview from other activity using intent

查看:79
本文介绍了使用目的等活动将数据传递到列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的项目中,第一个活动都有一个列表视图和一个按钮和第2个活动有的EditText 和其他信息和按钮。我希望当用户输入细节1日活动的的ListView 来传递从第二个活动数据。在这里,当我进入它是没有得到显示在的ListView数据 ..什么问题?

主要活动: -

  @覆盖
保护无效的onActivityResult(INT申请code,INT结果code,意图数据){
    super.onActivityResult(要求code,结果code,数据);
    LV的ListView =(ListView控件)findViewById(R.id.theListView);   如果(要求code == 1){
       如果(结果code == RESULT_OK){           字符串det_rec = data.getStringExtra(详细信息);
           ArrayList的<串GT; strArr =新的ArrayList<串GT;();
           的for(int i = 0; I< det_rec.length();我++){
               strArr.add(行:+ I);
           }
           ListAdapter适配器=新ArrayAdapter<串GT;(这一点,android.R.layout.simple_list_item_1,strArr);
           lv.setAdapter(适配器);       }
       如果(结果code == RESULT_CANCELED){
           返回;
       }
   }      // ListView的LV =(ListView控件)findViewById(R.id.theListView);
      //字符串det_rec = data.getStringExtra(详细信息);
       // ArrayList的<串GT; strArr =新的ArrayList<串GT;();
       //的for(int i = 0; I< det_rec.length();我++){
       // strArr.add(行:+ I);
  //}
    // ListAdapter适配器=新ArrayAdapter<串GT;(这一点,android.R.layout.simple_list_item_1,strArr);
   // lv.setAdapter(适配器);
}

}

第二项活动: -

 公共无效add_usr_tsk_btn(查看视图){
    等的EditText =(EditText上)findViewById(R.id.task_name_edit_txt);
    串detls =将String.valueOf(et.getText());
    意向GoBack的= getIntent();
    goback.putExtra(详细资料,detls);
    的setResult(RESULT_OK,GoBack的);
    完();
}公共无效cancl_btn(查看视图){
    意向GoBack的= getIntent();
    完();
}

}

主要活动: -

 <的LinearLayout
的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
的xmlns:工具=htt​​p://schemas.android.com/tool​​s
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:方向=垂直
机器人:重力=左
工具:上下文=MainActivity。><的LinearLayout
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:方向=横向
    机器人:layout_weight =1>    < ListView控件
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:ID =@ + ID / theListView>    < /&的ListView GT;< / LinearLayout中><的LinearLayout
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:方向=横向>    <的ImageButton
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文字=添加任务
        机器人:ID =@ + ID /按钮1
        机器人:背景=@绘制/ addtask
        安卓的onClick =onAddCLick
        机器人:layout_alignParentBottom =真
        机器人:layout_alignParentStart =真/>    <的ImageButton
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:背景=@绘制/ shwmap
        机器人:文字=查看地图
        机器人:ID =@ + ID /按钮2
        安卓的onClick =onMapbtnClck/>    <按钮
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文字=设置
        机器人:ID =@ + ID /按钮3
        安卓的onClick =onSetngClck
         />
< / LinearLayout中>

第二项活动: -

 <的LinearLayout
的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:方向=垂直
机器人:重力=左><的EditText
    机器人:layout_width =364dp
    机器人:layout_height =WRAP_CONTENT
    机器人:ID =@ + ID / task_name_edit_txt
    机器人:提示=请输入您的详细信息/><的LinearLayout
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:方向=横向><的TextView
    机器人:layout_width =250dp
    机器人:layout_height =WRAP_CONTENT
    机器人:文字=设置位置
    机器人:ID =@ + ID / TextView的/>
<按钮
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:文字=@字符串/ Set_loc_btn
    安卓的onClick =set_usr_loc/>< / LinearLayout中><的LinearLayout
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:方向=横向>    <按钮
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文字=@字符串/ Add_task_btn
        安卓的onClick =add_usr_tsk_btn
        机器人:ID =@ + ID / add_task/>    <按钮
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文字=@字符串/ cancel_btn
        安卓的onClick =cancl_btn
        机器人:ID =@ + ID / cancel_btn/>
    < / LinearLayout中>
< / LinearLayout中>


解决方案

您与以下code通过你的数据:

  goback.putExtra(详细资料,detls);

和你有与下面code:

 字符串det_rec = data.getStringExtra(详细信息);

当你看到详细信息不一样的详细信息,修改其中的一个,

当您使用的onActivityResult 您必须检查请求code 结果$ C $ç,使用该字段可以读<一个href=\"http://stackoverflow.com/questions/10407159/how-to-manage-start-activity-for-result-on-android\">

我认为你需要阅读列表创建教程,你可以用,因为我这个不落实的工作都:

  ListAdapter适配器=新ArrayAdapter&LT;串GT;(这一点,android.R.layout.simple_list_item_1,的Integer.parseInt(det_rec));

您必须传递一个的ArrayList 并在code 的ArrayList&LT;弦乐&GT; 来您的适配器,而不是的Integer.parseInt(det_rec)

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>

解决方案

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)

这篇关于使用目的等活动将数据传递到列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆