从列表视图传递数据到另一个活动的实现方法

6

我正在尝试将这个主活动的数据传递到另一个活动中。

我成功地在活动之间发送数据....例如通过使用putExtraGetExtra方法以及通过意图传递从编辑文本到下一个活动的数据。

  • 但是,我在涉及从ListView到普通活动发送数据的任务中遇到了挑战。
  • 数据是从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;
    }
}

谢谢,


所以你正在尝试通过ListView中点击的餐厅名称进行传递。 - Peshal
1
在onItemClick中,您可以使用以下代码获取所点击的项目:Item item = yourData.get(position); 现在,您可以使用intent.putExtra传递此项。顺便问一下,这个Item对象是什么样子的? - Peshal
我已经更新了问题中的项目对象的外观,请您看一下。我尝试了您的解决方案,但在putextra行中出现了错误,即“在Intent类型中,putExtra(String, boolean)方法对于参数(String,Item)不适用”。 - user2609157
1
好的,你可以这样做:String item = yourData.get(position).getName(); 然后你可以使用以下代码将字符串添加到意图中:intent.putExtra("restaurent", item); - Peshal
谢谢,嘿,在另一个活动的最后,如果我不使用篮子发送数据,我该如何获取额外的数据......我使用findviewbyid找到文本视图,然后呢? - user2609157
1
如果是TextView,您需要使用textView.setText(getIntent().getExtras().getString("restaurent"))。我会在答案中写下这个,并且如果我解决了您的问题,请标记它为正确。 - Peshal
5个回答

5

好的,你需要执行以下操作:

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

然后,您可以使用以下方法将string添加到intent中:

intent.putExtra("restaurent", item);

在另一端,如果它是textview,您需要执行以下操作。
textView.setText(getIntent().getExtras().getString("restaurent")); 

1
intent.putExtra("City Name", String.valueOf(lvCity.getSelectedItem()));

尝试使用此代码进行转换。

0

既然您在ListView中有所选项目的索引,因此可以从适配器中获取该项的值:

String value = (String)customAdapter.getItem(position);
Intent myIntent = new Intent(MainActivity.this,CopperChimneyDesc.class);
intent.putExtra("restaurant_name", value);
startActivity(myIntent);

我尝试了你的解决方案,但是出现错误“无法将项强制转换为字符串”...有什么解决办法吗? - user2609157

0

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


但是如何从ListView中获取数据(JSON)并将其存储在字符串中...有什么想法吗? - user2609157
如果数据是静态的,例如session_key,我认为使用SharePreference是最好的选择。 - Ye Lin Aung
mJsonString = downloadFileFromInternet(urls[0]); if(mJsonString == null /|| mJsonString.isEmpty()/) return false; JSONObject jObject = null; try { jObject = new JSONObject(mJsonString); JSONArray jsonImageArray = jObject.getJSONArray("imageTarget"); - Sushil

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接