简体   繁体   English

如何从一个活动的列表视图传递数据并将其传递给另一活动?

[英]How to pass the data from listview of one activity and pass it to another activity?

In my android app I need to pass the data of listview to another activity,for example If I click the Item "Food" in listview,I want get that "Food" in the textview of another activity. 在我的Android应用程序中,我需要将listview的数据传递给另一个活动,例如,如果我单击listview中的“食物”项,我想在另一个活动的textview中获得“食物”。

Here is my code.. 这是我的代码。

public class list extends Activity { 公共课程列表扩展了活动{

String selected_id = "";
SimpleCursorAdapter adapter;
DBhelper helper;
SQLiteDatabase db;
Button btnd;
String budget;


protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.listlayout);

    helper = new DBhelper(this);



       Listview rldlist = (ListView) findViewById(R.id.rldlist);
       TextView ed= (TextView) findViewById(R.id.addbud);
       rldlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {


                Cursor row = (Cursor) adapter.getItemAtPosition(position);
                selected_id = row.getString(0);
                budget = row.getString(1);
                System.out.print("budget is"+budget);
                ed.setText(budget);

            }
        });

In the above code, TextView ed= (TextView) findViewById(R.id.addbud); 在上面的代码中, TextView ed= (TextView) findViewById(R.id.addbud); is referring the textview from another activity. 从另一个活动引用了textview。

But problem now is,The TextView remains empty. 但是现在的问题是,TextView仍然为空。

Below one is the code to go another activity. 下面是执行另一项活动的代码。

rldlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
                Intent myIntent = new Intent(list.this, addbudget.class);
                list.this.startActivity(myIntent);
            }
        });

Just pass the value using 'Intent.PutExtra()' method and retrieve data in 'onCreate' method of the other activity. 只需使用“ Intent.PutExtra()”方法传递值,然后在其他活动的“ onCreate”方法中检索数据。

rldlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override public void onItemClick(AdapterView arg0, View arg1, int position, long arg3) { @Override public void onItemClick(AdapterView arg0, View arg1, int position, long arg3){

            Intent myIntent = new Intent(list.this, addbudget.class);
myIntent .putExtra("passed data key",budget);
            startActivity(myIntent);



        }
    });

Now, in your 'Addbudget'activity's onCreate() method, 现在,在您的“添加预算”活动的onCreate()方法中,

Bundle data_from_list= getIntent().getExtras();
String value_in_tv= data_from_list.getString("passed data key");
TextView ed= (TextView) findViewById(R.id.addbud);
 ed.setText(value_in_tv);

Done. 做完了 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将数据从一个活动传递到另一个活动列表视图 - Pass data from one activity to another activity listview 如何将数据从一个活动传递到另一活动 - How to pass data from one activity to another activity 如何将数据从一个活动传递到 Android 中的另一个活动 - How to pass data from one activity to another activity in Android 如何将图像数据从一个活动传递到另一个活动? - How to pass image data from one activity to another activity? 我如何将数据从一个活动传递到另一活动 - how i pass data from one activity to another activity 如何将数据从列表视图传递到另一个活动页面? - how to pass data from listview to another activity page? 如何从列表视图获取数据并将其传递给另一个活动 - How to obtain data from a listview and pass it to another activity 如何将列表视图中选定项目的数据传递给另一个活动? - How to pass data from a selected item in a listview to another activity? 如何将数据从活动传递到另一个活动,然后再传递到Android上的另一个活动? - How to pass data from an activity to another activity and then to another activity on Android? 将数据从一个活动传递到另一活动并显示在Listview上 - Pass Data from one activity to other activity and display on Listview
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM