将数据从列表视图传递到另一个活动

时间:2022-02-25 12:58:36

i want to create an activity, in which, i would like to have a listview, there can be about 20-30 items in a list view,on tapping any particular value in listview, it should move to another activity, with the data of list view.

我想创建一个活动,其中,我希望有一个列表视图,列表视图中可以有大约20-30个项目,在列表视图中点击任何特定值,它应该移动到另一个活动,具有列表显示。

Just wanna pass data from listview to another activity. Suggestion plz Regards

只想将listview中的数据传递给另一个活动。建议plz问候

6 个解决方案

#1


4  

Implement ListView's OnItemClickListener, once you handle this event, try to get the location of the row that was clicked.

实现ListView的OnItemClickListener,处理此事件后,尝试获取所单击行的位置。

Once you get it, access that particular row position in the source array (or whatever else you're having). This way, you'll have the data that you want to pass to another activity.

获得它之后,访问源数组中的特定行位置(或者您拥有的任何其他行)。这样,您将拥有要传递给其他活动的数据。

Now use this code:

现在使用此代码:

Intent anotherActivityIntent = new Intent(this, AnotherActivity.class);
anotherActivityIntent.putExtra("my.package.dataToPass",dataFromClickedRow);
startActivity(anotherActivityIntent);

and when the anotherActivityIntent starts the AnotherActivity class, use following code to access the value that you had passed:

当anotherActivityIntent启动AnotherActivity类时,使用以下代码访问您传递的值:

Bundle recdData = getIntent().getExtras();
String myVal = recdData.getString("my.package.dataToPass");

Now you have your data in myVal variable. you can use any other data type, whichever you like.

现在,您的数据在myVal变量中。您可以使用任何其他数据类型,无论您喜欢哪种。

#2


2  

You can pass data from one activity to another activity:

您可以将数据从一个活动传递到另一个活动:


see this link

看到这个链接


and to get data for ListView you have to first implement getListView.setOnItemClickListener(), and have to get position of item in ListView and use the index to get data form your adapter from where you are binding data to ListView.

要获取ListView的数据,您必须首先实现getListView.setOnItemClickListener(),并且必须获取ListView中项目的位置,并使用索引从您将适配器数据绑定到ListView的数据。


#3


2  

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

   Object obj = this.getListAdapter().getItem(position);
   String value= obj.toString();

   Intent intent= new Intent(CurrrentClass.this,NextClass.class);
   intent.putExtra("value", value);                 
   startActivity(intent);    
}

Hope this will help you.

希望这会帮助你。

#4


1  

String selectedItem =arrayAdapter.getItem(position);

String selectedItem = arrayAdapter.getItem(position);

            Intent intent = new Intent(getApplicationContext(), 
            Your_Second_Activity.class);
            intent.putExtra("selectedItem", selectedItem);
            startActivity(intent);

           Second_Activity.Class
           Bundle bundle = getIntent().getExtras();
           String yourItem = bundle.getString("selectedItem");

  Now! your selected item is inside in the yourItem Variable...

#5


0  

Use Bundle in onClickListner of the listview .

在listview的onClickListner中使用Bundle。

Bundle will pass data from one activity to Next.

Bundle会将数据从一个活动传递到Next。

#6


0  

There are two ways:

有两种方法:

  1. Pass it into Intent

    将它传递给Intent

    intent.putExtra("jobNo", item.jobNo);
    
  2. Use Application scope

    使用应用范围

    ((MyApplication) getApplication()).setJobNo(item.jobNo);
    

#1


4  

Implement ListView's OnItemClickListener, once you handle this event, try to get the location of the row that was clicked.

实现ListView的OnItemClickListener,处理此事件后,尝试获取所单击行的位置。

Once you get it, access that particular row position in the source array (or whatever else you're having). This way, you'll have the data that you want to pass to another activity.

获得它之后,访问源数组中的特定行位置(或者您拥有的任何其他行)。这样,您将拥有要传递给其他活动的数据。

Now use this code:

现在使用此代码:

Intent anotherActivityIntent = new Intent(this, AnotherActivity.class);
anotherActivityIntent.putExtra("my.package.dataToPass",dataFromClickedRow);
startActivity(anotherActivityIntent);

and when the anotherActivityIntent starts the AnotherActivity class, use following code to access the value that you had passed:

当anotherActivityIntent启动AnotherActivity类时,使用以下代码访问您传递的值:

Bundle recdData = getIntent().getExtras();
String myVal = recdData.getString("my.package.dataToPass");

Now you have your data in myVal variable. you can use any other data type, whichever you like.

现在,您的数据在myVal变量中。您可以使用任何其他数据类型,无论您喜欢哪种。

#2


2  

You can pass data from one activity to another activity:

您可以将数据从一个活动传递到另一个活动:


see this link

看到这个链接


and to get data for ListView you have to first implement getListView.setOnItemClickListener(), and have to get position of item in ListView and use the index to get data form your adapter from where you are binding data to ListView.

要获取ListView的数据,您必须首先实现getListView.setOnItemClickListener(),并且必须获取ListView中项目的位置,并使用索引从您将适配器数据绑定到ListView的数据。


#3


2  

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

   Object obj = this.getListAdapter().getItem(position);
   String value= obj.toString();

   Intent intent= new Intent(CurrrentClass.this,NextClass.class);
   intent.putExtra("value", value);                 
   startActivity(intent);    
}

Hope this will help you.

希望这会帮助你。

#4


1  

String selectedItem =arrayAdapter.getItem(position);

String selectedItem = arrayAdapter.getItem(position);

            Intent intent = new Intent(getApplicationContext(), 
            Your_Second_Activity.class);
            intent.putExtra("selectedItem", selectedItem);
            startActivity(intent);

           Second_Activity.Class
           Bundle bundle = getIntent().getExtras();
           String yourItem = bundle.getString("selectedItem");

  Now! your selected item is inside in the yourItem Variable...

#5


0  

Use Bundle in onClickListner of the listview .

在listview的onClickListner中使用Bundle。

Bundle will pass data from one activity to Next.

Bundle会将数据从一个活动传递到Next。

#6


0  

There are two ways:

有两种方法:

  1. Pass it into Intent

    将它传递给Intent

    intent.putExtra("jobNo", item.jobNo);
    
  2. Use Application scope

    使用应用范围

    ((MyApplication) getApplication()).setJobNo(item.jobNo);