HashMap中不能被转换为java.lang.String [英] HashMap cannot be cast to java.lang.string
本文介绍了HashMap中不能被转换为java.lang.String的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从列表视图中获取值。列表视图是由HashMap的类型的数组列表中设置。所以,如果我想获得一个字符串中,我得到了错误的值的HashMap不能转换为java.lang.String
details1.java
包com.example.festipedia_logo;进口的java.util.ArrayList;
进口的java.util.HashMap;
进口的java.util.List;进口org.apache.http.NameValuePair;
进口org.apache.http.message.BasicNameValuePair;
进口org.json.JSONArray;
进口org.json.JSONException;
进口org.json.JSONObject;进口com.actionbarsherlock.app.SherlockFragment;
进口com.actionbarsherlock.app.SherlockListFragment;
//进口com.example.festipedia_logo.Searchpage.LoadAllProducts;
//进口com.example.connection.disp;进口android.app.Activity;
进口android.app.Fragment;
进口android.app.ListActivity;
进口android.app.ProgressDialog;
进口android.content.Intent;
进口android.os.AsyncTask;
进口android.os.Bundle;
进口android.support.v4.app.FragmentTransaction;
进口android.util.Log;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.AdapterView;
进口android.widget.AdapterView.OnItemClickListener;
进口android.widget.ArrayAdapter;
进口android.widget.Button;
进口android.widget.EditText;
进口android.widget.ListAdapter;
进口android.widget.ListView;
进口android.widget.SimpleAdapter;
进口android.widget.Spinner;
进口android.widget.TextView;
进口android.widget.Toast;公共类扩展详情1 {SherlockFragment
ArrayAdapter<串GT;适配器;
的String []城市;
//进度对话框
私人ProgressDialog pDialog; //创建JSON解析器对象
JSONParser jParser =新JSONParser();
的EditText B:
ArrayList的<&HashMap的LT;字符串,字符串>>产品列表; // URL获得所有产品清单
私人静态字符串url_all_products =http://192.168.43.77/festipedia/get_all_products.php;
按钮; // JSON节点名称
私有静态最后弦乐TAG_SUCCESS =成功;
私有静态最后弦乐TAG_PRODUCTS =产品;
私有静态最后弦乐TAG_NAME =eventname已; //产品JSONArray
JSONArray产品= NULL;
ListView控件升;
微调微调;
@覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
捆绑savedInstanceState){
super.onCreate(savedInstanceState);
//setContentView(R.layout.second);
查看rootView = inflater.inflate(R.layout.home2,集装箱,FALSE); //的setContentView(R.layout.all_products);
L =(ListView控件)rootView.findViewById(R.id.list); //为哈希映射的ListView
产品列表=新的ArrayList<&HashMap的LT;字符串,字符串>>();
新LoadAllProducts()执行();
l.setOnItemClickListener(新OnItemClickListener(){
@覆盖
公共无效onItemClick(适配器视图<>母公司,观景,
INT位置,长的id){
// 做一点事
//字符串itemValue =(字符串)l.getItemAtPosition(位置);
//意向意图=新意图(getActivity()。getBaseContext()
// MainActivity.class);
// intent.putExtra(信息,itemValue);
// getActivity()startActivity(意向)。 //捆绑ARGS =新包();
// args.putString(doctor_id,itemValue);
//点评详情newFragment =新点评详情();
// newFragment.setArguments(参数);
//字符串str =((的TextView)childView).getText()的toString()。 // MainActivity OB =新MainActivity();
// ListView中单击项目指标
INT itemPosition =位置; // ListView中单击项目价值
字符串itemValue =(字符串)l.getItemAtPosition(位置);
最后FragmentTransaction英尺= getFragmentManager()调用BeginTransaction()。
ft.replace(R.id.content_frame,新SlidetabTesting(itemValue),巨星内容);
ft.commit();
} }); 返回rootView;
} / **
*背景异步任务以通过HTTP请求负载的所有产品
* * /
类LoadAllProducts扩展的AsyncTask<字符串,字符串,字符串> { @覆盖
在preExecute保护无效(){
super.on preExecute(); pDialog =新ProgressDialog(getActivity());
pDialog.setMessage(加载产品请稍候......);
pDialog.setIndeterminate(假);
pDialog.setCancelable(假);
pDialog.show();
} / **
*让所有的产品网址
* * /
保护字符串doInBackground(字符串参数... args){
//大厦参数
清单<&的NameValuePair GT; PARAMS =新的ArrayList<&的NameValuePair GT;();
// URL从获取JSON字符串
JSONObject的JSON = jParser.makeHtt prequest(url_all_products,GET,则params); // Log.d(所有产品,json.toString()); 尝试{
//检查成功TAG
INT成功= json.getInt(TAG_SUCCESS); 如果(成功== 1){
发现//产品
//获取产品阵列
产品= json.getJSONArray(TAG_PRODUCTS); //产品通过所有循环
的for(int i = 0; I< products.length();我++){
JSONObject的C = products.getJSONObject(I) //存储在变量中的每个JSON项目 字符串名称= c.getString(TAG_NAME); //l.setFilterText(id);
//创建新的HashMap
HashMap的<字符串,字符串>地图=新的HashMap<字符串,字符串>(); //将每个子节点的HashMap键=>值
map.put(TAG_NAME,名); //添加HashList到ArrayList的
productsList.add(地图);
}
}其他{
//没有发现产品
//启动添加新产品活动
}
}赶上(JSONException E){
e.printStackTrace();
} 返回null;
} / **
*在完成后台任务之后辞退进度对话框
* ** /
保护无效onPostExecute(字符串FILE_URL){
//让所有的产品后,关闭该对话框
pDialog.dismiss();
//从后台线程更新界面
getActivity()。runOnUiThread(新的Runnable(){
公共无效的run(){
/ **
*更新解析JSON数据到ListView控件
* * /
ListAdapter适配器=新SimpleAdapter(
getActivity(),产品列表,
R.layout.list_item,新的String [] {
TAG_NAME},
新的INT [] {R.id.name});
//更新列表视图
l.setAdapter(适配器);
}
});
} }
}
logcat的
20 03-30:03:43.341:E / ViewRootImpl(8395):sendUserActionEvent()== MView的空
03-30 20:03:47.111:E / ViewRootImpl(8395):sendUserActionEvent()== MView的空
03-30 20:03:49.416:E / AndroidRuntime(8395):致命异常:主要
03-30 20:03:49.416:E / AndroidRuntime(8395):工艺:com.example.festipedia_logo,PID:8395
03-30 20:03:49.416:E / AndroidRuntime(8395):java.lang.ClassCastException:java.util.HashMap中不能转换为java.lang.String
03-30 20:03:49.416:E / AndroidRuntime(8395):在com.example.festipedia_logo.details1 $ 1.onItemClick(details1.java:103)
03-30 20:03:49.416:E / AndroidRuntime(8395):在android.widget.AdapterView.performItemClick(AdapterView.java:308)
03-30 20:03:49.416:E / AndroidRuntime(8395):在android.widget.AbsListView.performItemClick(AbsListView.java:1483)
03-30 20:03:49.416:E / AndroidRuntime(8395):在android.widget.AbsListView $ PerformClick.run(AbsListView.java:3485)
03-30 20:03:49.416:E / AndroidRuntime(8395):在android.widget.AbsListView $ 3.run(AbsListView.java:4843)
03-30 20:03:49.416:E / AndroidRuntime(8395):在android.os.Handler.handleCallback(Handler.java:733)
03-30 20:03:49.416:E / AndroidRuntime(8395):在android.os.Handler.dispatchMessage(Handler.java:95)
03-30 20:03:49.416:E / AndroidRuntime(8395):在android.os.Looper.loop(Looper.java:157)
03-30 20:03:49.416:E / AndroidRuntime(8395):在android.app.ActivityThread.main(ActivityThread.java:5356)
03-30 20:03:49.416:E / AndroidRuntime(8395):在java.lang.reflect.Method.invokeNative(本机方法)
03-30 20:03:49.416:E / AndroidRuntime(8395):在java.lang.reflect.Method.invoke(Method.java:515)
03-30 20:03:49.416:E / AndroidRuntime(8395):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1265)
03-30 20:03:49.416:E / AndroidRuntime(8395):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
03-30 20:03:49.416:E / AndroidRuntime(8395):在dalvik.system.NativeStart.main(本机方法)
有什么办法由我就能检索值
解决方案
java.lang.ClassCastException:java.util.HashMap中不能被转换为
java.lang.String中
块引用>这行是问题
字符串itemValue =(字符串)l.getItemAtPosition(位置);
更改为
l.setOnItemClickListener(新OnItemClickListener(){
@覆盖
公共无效onItemClick(适配器视图<>母公司,观景,INT位置,长的id){HashMap的<字符串,字符串>地图=(HashMap的<字符串,字符串>)l.getItemAtPosition(位置);
字符串值= map.get(TAG_SUCCESS);
斯汀产品= map.get(TAG_PRODUCTS);
字符串名称= map.get(TAG_NAME);
... //在code休息
}I am trying to get values from a list view. The list view is set by an array list of hashmap type. So , if I am trying to get the values in a string I get the error
HashMap cannot be cast to java.lang.string
details1.java
package com.example.festipedia_logo; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import org.apache.http.NameValuePair; import org.apache.http.message.BasicNameValuePair; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import com.actionbarsherlock.app.SherlockFragment; import com.actionbarsherlock.app.SherlockListFragment; //import com.example.festipedia_logo.Searchpage.LoadAllProducts; //import com.example.connection.disp; import android.app.Activity; import android.app.Fragment; import android.app.ListActivity; import android.app.ProgressDialog; import android.content.Intent; import android.os.AsyncTask; import android.os.Bundle; import android.support.v4.app.FragmentTransaction; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.ListAdapter; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.Spinner; import android.widget.TextView; import android.widget.Toast; public class details1 extends SherlockFragment { ArrayAdapter<String> adapter; String[] city; // Progress Dialog private ProgressDialog pDialog; // Creating JSON Parser object JSONParser jParser = new JSONParser(); EditText b; ArrayList<HashMap<String, String>> productsList; // url to get all products list private static String url_all_products = "http://192.168.43.77/festipedia/get_all_products.php"; Button a; // JSON Node names private static final String TAG_SUCCESS = "success"; private static final String TAG_PRODUCTS = "products"; private static final String TAG_NAME = "eventname"; // products JSONArray JSONArray products = null; ListView l; Spinner spinner; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { super.onCreate(savedInstanceState); //setContentView(R.layout.second); View rootView = inflater.inflate(R.layout.home2, container, false); // setContentView(R.layout.all_products); l = (ListView) rootView.findViewById(R.id.list); // Hashmap for ListView productsList = new ArrayList<HashMap<String, String>>(); new LoadAllProducts().execute(); l.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // Do something // String itemValue = (String) l.getItemAtPosition(position); // Intent intent = new Intent(getActivity().getBaseContext(), // MainActivity.class); // intent.putExtra("message", itemValue); // getActivity().startActivity(intent); // Bundle args = new Bundle(); // args.putString("doctor_id",itemValue); // details1 newFragment = new details1 (); // newFragment.setArguments(args); //String str = ((TextView)childView).getText().toString(); //MainActivity ob=new MainActivity(); // ListView Clicked item index int itemPosition = position; // ListView Clicked item value String itemValue = (String) l.getItemAtPosition(position); final FragmentTransaction ft = getFragmentManager().beginTransaction(); ft.replace(R.id.content_frame,new SlidetabTesting(itemValue) , "Fest Content"); ft.commit(); } }); return rootView; } /** * Background Async Task to Load all product by making HTTP Request * */ class LoadAllProducts extends AsyncTask<String, String, String> { @Override protected void onPreExecute() { super.onPreExecute(); pDialog = new ProgressDialog(getActivity()); pDialog.setMessage("Loading products. Please wait..."); pDialog.setIndeterminate(false); pDialog.setCancelable(false); pDialog.show(); } /** * getting All products from url * */ protected String doInBackground(String... args) { // Building Parameters List<NameValuePair> params = new ArrayList<NameValuePair>(); // getting JSON string from URL JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params); // Log.d("All Products: ", json.toString()); try { // Checking for SUCCESS TAG int success = json.getInt(TAG_SUCCESS); if (success == 1) { // products found // Getting Array of Products products = json.getJSONArray(TAG_PRODUCTS); // looping through All Products for (int i = 0; i < products.length(); i++) { JSONObject c = products.getJSONObject(i); // Storing each json item in variable String name = c.getString(TAG_NAME); //l.setFilterText(id); // creating new HashMap HashMap<String, String> map = new HashMap<String, String>(); // adding each child node to HashMap key => value map.put(TAG_NAME, name); // adding HashList to ArrayList productsList.add(map); } } else { // no products found // Launch Add New product Activity } } catch (JSONException e) { e.printStackTrace(); } return null; } /** * After completing background task Dismiss the progress dialog * **/ protected void onPostExecute(String file_url) { // dismiss the dialog after getting all products pDialog.dismiss(); // updating UI from Background Thread getActivity().runOnUiThread(new Runnable() { public void run() { /** * Updating parsed JSON data into ListView * */ ListAdapter adapter = new SimpleAdapter( getActivity(), productsList, R.layout.list_item, new String[] { TAG_NAME}, new int[] { R.id.name }); // updating listview l.setAdapter(adapter); } }); } } }
LOGCAT
03-30 20:03:43.341: E/ViewRootImpl(8395): sendUserActionEvent() mView == null 03-30 20:03:47.111: E/ViewRootImpl(8395): sendUserActionEvent() mView == null 03-30 20:03:49.416: E/AndroidRuntime(8395): FATAL EXCEPTION: main 03-30 20:03:49.416: E/AndroidRuntime(8395): Process: com.example.festipedia_logo, PID: 8395 03-30 20:03:49.416: E/AndroidRuntime(8395): java.lang.ClassCastException: java.util.HashMap cannot be cast to java.lang.String 03-30 20:03:49.416: E/AndroidRuntime(8395): at com.example.festipedia_logo.details1$1.onItemClick(details1.java:103) 03-30 20:03:49.416: E/AndroidRuntime(8395): at android.widget.AdapterView.performItemClick(AdapterView.java:308) 03-30 20:03:49.416: E/AndroidRuntime(8395): at android.widget.AbsListView.performItemClick(AbsListView.java:1483) 03-30 20:03:49.416: E/AndroidRuntime(8395): at android.widget.AbsListView$PerformClick.run(AbsListView.java:3485) 03-30 20:03:49.416: E/AndroidRuntime(8395): at android.widget.AbsListView$3.run(AbsListView.java:4843) 03-30 20:03:49.416: E/AndroidRuntime(8395): at android.os.Handler.handleCallback(Handler.java:733) 03-30 20:03:49.416: E/AndroidRuntime(8395): at android.os.Handler.dispatchMessage(Handler.java:95) 03-30 20:03:49.416: E/AndroidRuntime(8395): at android.os.Looper.loop(Looper.java:157) 03-30 20:03:49.416: E/AndroidRuntime(8395): at android.app.ActivityThread.main(ActivityThread.java:5356) 03-30 20:03:49.416: E/AndroidRuntime(8395): at java.lang.reflect.Method.invokeNative(Native Method) 03-30 20:03:49.416: E/AndroidRuntime(8395): at java.lang.reflect.Method.invoke(Method.java:515) 03-30 20:03:49.416: E/AndroidRuntime(8395): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265) 03-30 20:03:49.416: E/AndroidRuntime(8395): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081) 03-30 20:03:49.416: E/AndroidRuntime(8395): at dalvik.system.NativeStart.main(Native Method)
Is there any way by which I will be able to retrieve values
解决方案java.lang.ClassCastException: java.util.HashMap cannot be cast to java.lang.String
This line is the problem
String itemValue = (String) l.getItemAtPosition(position);
Change to
l.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view,int position, long id) { HashMap<String,String> map =(HashMap<String,String>)l.getItemAtPosition(position); String value = map.get(TAG_SUCCESS); Sting products = map.get(TAG_PRODUCTS); String name= map.get(TAG_NAME); ... // rest of the code }
这篇关于HashMap中不能被转换为java.lang.String的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文