从列表视图活动之间的数据传递到另一个活动 [英] Passing data between activities from listview to another activity

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

问题描述

我成功通过 putExtra 活动之间发送数据....就像从编辑文本到下一个活动的数据GetExtra 方法和传球为意图

I am successful in sending data between activities .... Like the data from Edit-text to next activity through putExtra and GetExtra methods and passing as intents


  • 但我在这个特殊的任务面临的挑战是在涉及
    从发送列表视图数据,一个普通的活动

  • 数据在列表视图中填充从JSON上的点击,所以当

  • 行如何从该行的数据发送到一个新的活动

任何想法,

ativity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ListView
        android:id="@+id/listViewID"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 

        android:layout_gravity="center">
    </ListView>

</LinearLayout>

MainActivity.java

public class MainActivity extends Activity {

    // url to make request
    private static String url = "http://54.218.73.244:7002/";
    List<Item> yourData = new ArrayList<Item>();

    ProgressDialog progressDialog;

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

        //Instantiating ProgressDialog with onCreate method
        progressDialog=new ProgressDialog(MainActivity.this);
        new ParsingAsync().execute();

    }

    private class ParsingAsync extends AsyncTask<Void, Void, Void>
    {

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
            progressDialog=ProgressDialog.show(MainActivity.this, "", "Please Wait", true, false);


        }

        @Override
        protected Void doInBackground(Void... params) {
            // TODO Auto-generated method stub

            // Creating JSON Parser instance
            JSONObjParser jParser = new JSONObjParser();

            // getting JSON string from URL
            JSONArray json = jParser.getJSONFromUrl(url);

            try {
                for (int i = 0; i < json.length(); i++) {
                    JSONObject c = json.getJSONObject(i);

                    // Storing each json item in variable
                    String NAME=c.getString("restaurantNAME");

                    yourData.add(new Item(NAME));
                }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            progressDialog.dismiss();
            ListView yourListView = (ListView) findViewById(R.id.listViewID);
            ListAdapter customAdapter = new ListAdapter(MainActivity.this, R.layout.itemlistrow, yourData);
            yourListView.setAdapter(customAdapter);
            yourListView.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
                    // When clicked, show a toast with the TextView text
                    if(position == 0)
                    {
                        //code specific to first list item    
                        Intent myIntent = new Intent(MainActivity.this,CopperChimneyDesc.class);
                        startActivity(myIntent);
                    }else if(position == 1)
                    {
                        //Intent myIntent = new Intent(MainActivity.this,AroyDesc.class);
                        //startActivity(myIntent);                  
                    }

                }
            });
        }

    }

}

item.java

public class Item{
    private String Name;

    public Item(String name){
        this.Name = name;
    }
    public String getName(){
        return Name;
    }
}

谢谢,

推荐答案

好了,所以你会做

String item = yourData.get(position).getName(); 

然后就可以使用字符串添加到意图:

Then you can add the string to intent using:

intent.putExtra("restaurent", item);

在另一端如果TextView的你会怎么做。

On the other end if its textview you would do

textView.setText(getIntent().getExtras().getString("restaurent")); 

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

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