通过单击项目listView在textView上传递数据

时间:2014-09-26 10:18:56

标签: android listview android-activity android-listview textview

每次单击列表视图中的任何文本时,我都想在新活动的TextView上调用setText。例如,如果我点击listView中包含" Android"我想要#34;移动"在文本视图中显示在新活动中。

查找以下代码:

主要活动代码:

MainActivity.java

package com.example.listui;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {
TextView txtView;
ListView listview;
String [] myStringArray = {"JAVA","Android","php","jQuery","JAVAScript","Paython"};
String [] info = {"for Desktop","for Mobile","for Web","for Web to improve interFace","like jQuery","For desktop"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    txtView = (TextView) findViewById(R.id.txtVo);
    listview = (ListView) findViewById(R.id.listView1);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>       (this,android.R.layout.simple_list_item_1,myStringArray);
    listview.setAdapter(adapter);
    listview.setOnItemClickListener(new OnItemClickListener() {



        @Override
        public void onItemClick(AdapterView<?> MainActivity, View view,
                int position, long id) {


                Intent i = new Intent(getApplication(),TxtMainActivity.class);
                startActivity(i);
            txtView.setText(info[3]);


            }

        }
    );





    }


}

文字查看活动代码: TxtMainActivity.java

package com.example.listui;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class TxtMainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_txt_main);
}


}

文字查看代码: activity_txt_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.listui.TxtMainActivity" >

<TextView
    android:id="@+id/txtVo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world"
    android:onClick="setV" />

活动主要代码: activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.listui.MainActivity" >

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

</ListView>

5 个答案:

答案 0 :(得分:3)

使用此

txtView.setText(info[position]);

而不是

txtView.setText(info[3]);

如果您想转到下一个活动,请将putExtra用于Intent

 Intent i = new Intent(MainActivity.this,TxtMainActivity.class);
 i.putExtra("key",info[position]);
 startActivity(i);

以及

进入您的新活动
 String key = getIntent().getExtra("key");

答案 1 :(得分:0)

使用

   txtView.setText(info[position]);

答案 2 :(得分:0)

使用此:

txtView.setText(info[position]);

答案 3 :(得分:0)

将信息作为额外意图传递

listview.setOnItemClickListener(new OnItemClickListener() {



        @Override
        public void onItemClick(AdapterView<?> MainActivity, View view,
                int position, long id) {


                Intent i = new Intent(getApplication(),TxtMainActivity.class);
                i.putExtra("info",info[position]);
                startActivity(i);

            }

        }
    );

TxtMainActivity的OnCreate()内部

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_txt_main);
    String info = getIntent().getExtra("info");
    ((TextView)findViewById(R.id.txtVo)).setText(info);

}

答案 4 :(得分:0)

使用putExtra()getExtra()

@Override
public void onItemClick(AdapterView<?> MainActivity, View view,
        int position, long id) {
Intent i = new Intent(getApplication(),TxtMainActivity.class);
i.putExtra("My_TextView_Data", info[position]);
startActivity(i);
}




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_txt_main);
    String txtViewData;
    if (savedInstanceState == null) {
        extras = getIntent().getExtras();
        if(extras == null) {
            txtViewData= null;
        } else {
            txtViewData= extras.getString("My_TextView_Data");
        }
    }
    else
    {
        txtViewData= (String) savedInstanceState.getSerializable("My_TextView_Data");
    }
    txtView.setText(txtViewData);
}