从列表视图主活动传递数据 [英] Pass data from listview to main activity

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

问题描述

我有一个主要活动包括按钮,TextView中。当我按一下按钮,它会显示一个列表视图和一些数据填充到该列表视图。在ListView中,我点击一个项目,我要项目数据发送到TextView的主活动。但是,它有崩溃。我不知道,以发现问题和日志(不显示)。你可以看看我的code和提供给我解决?

MainActivity

  btnManage =(按钮)findViewById(R.id.btnManage);
btnManage.setText(manageLabel);
btnManage.setOnClickListener(新View.OnClickListener(){
    @覆盖
    公共无效的onClick(最终查看V){
        意向意图=新意图(v.getContext(),ListViewActivity.class);
        intent.putExtra(List_data,你好);
        。v.getContext()startActivity(意向);    }
});

在ListViewActivity我有

 捆绑包= getIntent()getExtras()。
串data_String = bundle.getString(List_data);
ArrayList的<串GT;数据=新的ArrayList<串GT;();
data.add(data_String);
listAdapter =新ArrayAdapter<串GT;(这一点,R.layout.simplerow,数据);
//设置一个ArrayAdapter为ListView的适配器。
mainListView.setAdapter(listAdapter);
//点​​击一个项目
公共无效onItemClick(适配器视图<>母公司,观景,INT位置,
      长ID){字符串DATA_SEND =(字符串)mainListView.getItemAtPosition(位置);
意向意图=新意图(这一点,MainActivity.class);
intent.putExtra(DATA_SEND,DATA_SEND);
startActivity(意向);
 }

在MainActivity中的onCreate功能,我有

 捆绑包= getIntent()getExtras()。
串data_activity = bundle.getString(DATA_SEND);

清单文件

 <活动
        机器人:名字=。MainActivity
        机器人:标签=@字符串/ APP_NAME>
        &所述;意图滤光器>
            <作用机器人:名字=android.intent.action.MAIN/>
            <类机器人:名字=android.intent.category.LAUNCHER/>
        &所述; /意图滤光器>
    < /活性GT;
    <活动
        机器人:名字=。ListViewActivity
        机器人:标签=@字符串/ APP_NAME
        机器人:screenOrientation =画像
    />


解决方案

在ListViewActivity您尝试启动MainActivity这已经启动因此错误。

您需要完成ListViewActivity acivity所以它只是又回到了previous活动是在MainActivity和传递数据

在ListViewActivity完成的活动,并通过数据发回这样的

 意向意图=新的Intent();
intent.putExtra(DATA_SEND,DATA_SEND);
的setResult(RESULT_OK,意向);
完();

在MainActivity你开始喜欢这个ListViewActivity

 意向书I =新意图(getApplicationContext(),ListViewActivity.class);
i.putExtra(List_data,你好);
startActivityForResult(I,1);

也MainActivity你需要让你添加从ListViewActivity找回数据

 公共无效的onActivityResult(INT申请code,INT结果code,意图数据){        如果(要求code == 1){
            如果(结果code == RESULT_OK){
                字符串数据= data.getStringExtra(DATA_SEND);              //做什么用的数据串
}}};

如果(要求code == 1){是给你当你开始使用 startActivityForResult活动数量ID(我,1); 。如果你想要做同样的另一个活动,你可以做 startActivityForResult(1,2); ,并取回您检查ID 2 <数据/ code> 如果(要求code == 2){...

I have a main activity included the button and textview. When I click the button, it will display a listview and fill some data into that listview. In the listview, I click a item, I want to send the item data to textview in main activity. However, it has crash. I have no idea to find the problem and log (does not show). Could you look at my code and provide me the solution?

MainActivity

btnManage = (Button) findViewById(R.id.btnManage);
btnManage.setText(manageLabel);
btnManage.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(final View v) {
        Intent intent = new Intent(v.getContext(), ListViewActivity.class);
        intent.putExtra("List_data", "Hello");
        v.getContext().startActivity(intent);

    }
});

In ListViewActivity I have

Bundle bundle = getIntent().getExtras();
String data_String= bundle.getString("List_data");
ArrayList<String> data = new ArrayList<String>();
data.add(data_String);
listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, data);    
// Set the ArrayAdapter as the ListView's adapter.
mainListView.setAdapter( listAdapter ); 
//Click one item
public void onItemClick(AdapterView<?> parent, View view, int position,
      long id) {

String  data_send    = (String) mainListView.getItemAtPosition(position);
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("Data_Send", data_send);
startActivity(intent);
 }

In the onCreate function in MainActivity, I have

Bundle bundle = getIntent().getExtras();
String data_activity =bundle.getString("Data_Send");

The Manifest file

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".ListViewActivity"
        android:label="@string/app_name" 
        android:screenOrientation="portrait"
    />

解决方案

In ListViewActivity you are trying to start MainActivity which is already started hence the error.

You need to finish ListViewActivity acivity so it just goes back to the previous activity which is the MainActivity and pass data

in ListViewActivity you finish the activity and pass data back like this

Intent intent = new Intent();
intent.putExtra("Data_Send", data_send);
setResult(RESULT_OK, intent);
finish();

in MainActivity you start the ListViewActivity like this

Intent i = new Intent(getApplicationContext(), ListViewActivity.class);
i.putExtra("List_data", "Hello");
startActivityForResult(i, 1);

also in MainActivity you need to get data back from the ListViewActivity so you add

public void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == 1) {
            if (resultCode == RESULT_OK) {
                String data = data.getStringExtra("Data_Send");

              // do whatever with data string
}}};

The if (requestCode == 1) { is the number id you give when you start an activity with startActivityForResult(i, 1);. If you want to do the same with another activity you can do startActivityForResult(i, 2); and to get back data you check for id 2 if (requestCode == 2) { ...

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

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