将在列表视图中单击的项目的值从一个屏幕传递到另一个屏幕

时间:2011-12-26 18:42:58

标签: android android-intent bundle listactivity

我正在尝试将点击列表中的项目发送到另一个屏幕,但收到错误...

继承我的主要档案......

package com.bmc;

import java.util.ArrayList;
import java.util.HashMap;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class Main extends ListActivity {
    /** Called when the activity is first created. */
    private Context mCtx;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.myrequest);
        mCtx = this;

        ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();


        String xml = XMLfunctions.getXML("http://10.0.2.2/ss.htm");
        Document doc = XMLfunctions.XMLfromString(xml);

        int numResults = XMLfunctions.numResults(doc);

        if((numResults <= 0)){
            Toast.makeText(Main.this, "Error", Toast.LENGTH_LONG).show();  
            finish();
        }

        NodeList nodes = doc.getElementsByTagName("result");

        for (int i = 0; i < nodes.getLength(); i++) {                           
            HashMap<String, String> map = new HashMap<String, String>();    

            Element e = (Element)nodes.item(i);
            map.put("id", XMLfunctions.getValue(e, "id"));
            map.put("name", XMLfunctions.getValue(e, "id") + "-" + XMLfunctions.getValue(e, "name") + " :");
            mylist.add(map);            
        }       


        ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.main, 
                        new String[] { "name"}, 
                        new int[] { R.id.item_title });

        setListAdapter(adapter);

        final ListView lv = getListView();
        lv.setTextFilterEnabled(true);
        lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {              
                @SuppressWarnings("unchecked")
                HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);                   
                Toast.makeText(Main.this, "ID '" + o.get("id") + "' was clicked.", Toast.LENGTH_LONG).show(); 

                String selectedFromList = (String) (lv.getItemAtPosition(position));

                Intent i = new Intent(mCtx, Details.class);

                Bundle extras=new Bundle();
                extras.putSerializable("obj_to_pass", selectedFromList);
                i.putExtras(extras);

            startActivity(i);
    //          Main.this.finish();

            }
        });

    }
}   

继承我的Details.java文件......

package com.bmc;

import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;


public class Details extends Activity 
{

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.details);

        EditText txt;

        Bundle extras = getIntent().getExtras();
        Object my_obj = extras.getSerializable("obj_to_pass");

        txt=(EditText) findViewById(R.id.editText_desc1);

        txt.setText((CharSequence) my_obj);
    }

}

日志文件......

12-27 01:46:55.994: E/AndroidRuntime(311): FATAL EXCEPTION: main
12-27 01:46:55.994: E/AndroidRuntime(311): java.lang.ClassCastException: java.util.HashMap
12-27 01:46:55.994: E/AndroidRuntime(311):  at com.bmc.Main$1.onItemClick(Main.java:73)
12-27 01:46:55.994: E/AndroidRuntime(311):  at android.widget.AdapterView.performItemClick(AdapterView.java:284)
12-27 01:46:55.994: E/AndroidRuntime(311):  at android.widget.ListView.performItemClick(ListView.java:3382)
12-27 01:46:55.994: E/AndroidRuntime(311):  at android.widget.AbsListView$PerformClick.run(AbsListView.java:1696)
12-27 01:46:55.994: E/AndroidRuntime(311):  at android.os.Handler.handleCallback(Handler.java:587)
12-27 01:46:55.994: E/AndroidRuntime(311):  at android.os.Handler.dispatchMessage(Handler.java:92)
12-27 01:46:55.994: E/AndroidRuntime(311):  at android.os.Looper.loop(Looper.java:123)
12-27 01:46:55.994: E/AndroidRuntime(311):  at android.app.ActivityThread.main(ActivityThread.java:4627)
12-27 01:46:55.994: E/AndroidRuntime(311):  at java.lang.reflect.Method.invokeNative(Native Method)
12-27 01:46:55.994: E/AndroidRuntime(311):  at java.lang.reflect.Method.invoke(Method.java:521)
12-27 01:46:55.994: E/AndroidRuntime(311):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-27 01:46:55.994: E/AndroidRuntime(311):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-27 01:46:55.994: E/AndroidRuntime(311):  at dalvik.system.NativeStart.main(Native Method)

在运行上面的项目之后我得到错误和msg框选项&#34;强制关闭&#34; ...所以如果有人有解决方案那么帮助...... !!!感谢名单.. !!

1 个答案:

答案 0 :(得分:1)

如果您只是想传递字符串,您只需要:

intent.putExtra("myString", selectedFromList);

然后在下一个活动中:

String s = getIntent().getStringExtra("myString", "some default value");

像往常一样,发布日志。

修改

这些演员表不能 工作:

        String selectedFromList = (String) (lv.getItemAtPosition(position));

        HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);                   

您需要转换为其中一个,并且从您的代码中看起来像HashMap的正确演员。获得地图后,您需要通过其string检索key

这就是你在第73行获得ClassCastException的原因;您正在将HashMap投射到String