将列表视图中单击的项目的值从一个屏幕传递到android中的另一个屏幕
我正在尝试将列表中单击的项目发送到另一个屏幕,但收到错误...
这是我的主文件...这
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您只是想传递字符串,那么您需要的是:
然后在下一个活动中:
像往常一样,发布日志。
编辑
这些转换不能同时工作:
并且
您需要转换为其中之一,从您的代码来看,正确的转换是
HashMap.获得地图后,您需要通过其
键
检索字符串
。这就是为什么您在第 73 行收到
ClassCastException
的原因;您正在将HashMap
转换为String
。If you are just trying to pass the String, all you need is:
and then in the next Activity:
As usual, post the logs.
Edit
These casts cannot both work:
and
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 thestring
by itskey
.That's why you're getting a
ClassCastException
at line 73; you're casting aHashMap
to aString
.