6

我正在尝试将数据从这个主要活动传递到另一个活动

我成功地在活动之间发送数据....就像从 Edit-text 到下一个活动的数据通过putExtraGetExtra方法并作为意图传递

  • 但是我在这个特定任务中面临挑战,它涉及将数据从列表视图发送到普通活动
  • 数据从 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);                  
                    }

                }
            });
        }

    }

}

项目.java

public class Item{
    private String Name;

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

谢谢,

4

5 回答 5

5

好的,你会这样做

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

然后你可以添加stringintent使用:

intent.putExtra("restaurent", item);

另一方面,如果textview你愿意

textView.setText(getIntent().getExtras().getString("restaurent")); 
于 2013-08-13T03:48:02.563 回答
1
intent.putExtra("City Name", String.valueOf(lvCity.getSelectedItem()));

试试这个来投。

于 2015-08-13T05:04:44.410 回答
0

由于您在用户单击的列表视图中有项目的索引,因此您可以从适配器获取项目值:

String value = (String)customAdapter.getItem(position);
Intent myIntent = new Intent(MainActivity.this,CopperChimneyDesc.class);
intent.putExtra("restaurant_name", value);
startActivity(myIntent);
于 2013-08-13T02:39:24.793 回答
0

您可以使用 putxtera 方法打包数据并将其发送到其他活动。请参阅如何在 onSaveInstanceState() 中存储值并检索?

于 2013-08-13T02:34:58.980 回答
0

您可以将数据保存在其中sharedPreferences并可以从其他活动中访问它。只是一个想法:)

于 2013-08-13T02:25:50.927 回答