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

发布于 2024-12-22 23:02:14 字数 5300 浏览 3 评论 0原文

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

这是我的主文件...这

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)

运行上述项目后收到错误和消息带有选项“强制关闭”的框...所以如果有人有解决方案,请帮助...!谢谢..!!

I am trying to send the item clicked in list to another screen but getting an error...

Heres my main file...

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();

            }
        });

    }
}   

Heres my Details.java file...

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);
    }

}

Log file...

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)

After running above project am getting error and msg box with option "Force Close"... So if anyone has solution then help...!!! thanx..!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自由如风 2024-12-29 23:02:14

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

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.获得地图后,您需要通过其检索字符串

这就是为什么您在第 73 行收到 ClassCastException 的原因;您正在将 HashMap 转换为 String

If you are just trying to pass the String, all you need is:

intent.putExtra("myString", selectedFromList);

and then in the next Activity:

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

As usual, post the logs.

Edit

These casts cannot both work:

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

and

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

You need to cast to one or the other and from your code it looks like the proper cast is to the HashMap. Once you have the map, you need to then retrieve the string by its key.

That's why you're getting a ClassCastException at line 73; you're casting a HashMap to a String.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文