- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在创建一个 Android 版 GoogleMaps 应用程序,并将位置列表存储在在线服务器的数据库中。我成功地拉动了它们,现在我想使用 Actionbar 搜索 View 创建一个用户可以搜索的 ListView ,它将更改当前加载的数据以仅显示链接到当前选定位置的数据。
但是,由于我调用它的位置,在将 ListViewAdapter 代码更改为可运行代码后尝试将数组列表放入适配器时,我遇到了无法转换为 android.content.Context 的错误。我的主要 Activity 代码如下:
TownSelector ts;
ListView townList;
ListViewAdapter townAdapter; <---set variable for adapter here
String[] townID;
String[] townName;
ArrayList<TownSelector> arraylist = new ArrayList<TownSelector>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//setContentView(R.layout.search);
setUpMapIfNeeded();
// Hashmap for ListView
locationList = new ArrayList<HashMap<String, String>>();
// Locate the ListView in listview_main.xml
townList = (ListView) findViewById(R.id.listview);
}
class LoadAllInfo extends AsyncTask<String, String, String> {
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> paramsLocations = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject jsonLocations = jParser.makeHttpRequest(url_all_locations, "GET", paramsLocations);
//Get all offer
if(jsonLocations != null)
{
// Check your log cat for JSON reponse
Log.d("All Locations: ", jsonLocations.toString());
try {
// Checking for SUCCESS TAG
int success = jsonLocations.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Offers
locations = jsonLocations.getJSONArray(TAG_LOCATIONS);
// looping through All Offers
for (int i = 0; i < locations.length(); i++) {
JSONObject c = locations.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_LID);
String locationName = c.getString(TAG_LNAME);
// creating new HashMap
HashMap<String, String> locationsListMap = new HashMap<String, String>();
// adding each child node to HashMap key => value
locationsListMap.put(TAG_LID, id);
locationsListMap.put(TAG_LNAME, locationName);
// adding HashList to ArrayList
locationList.add(locationsListMap);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
return null;
}
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into map marker
* */
//TODO setMapMarkers
townID = new String[locationList.size()];
townName = new String[locationList.size()];
for(int l = 0; l <= (locationList.size() - 1); l++)
{
Log.d("Hello", "location loop");
Log.d("Hello", locationList.get(l).toString().split("Location_ID=")[1].split(",")[0]);
townID[l] = locationList.get(l).toString().split("Location_ID=")[1].split(",")[0];
Log.d("Hello", locationList.get(l).toString().split("Location_Name=")[1].split(",")[0].replace("}",""));
townName[l] = locationList.get(l).toString().split("Location_Name=")[1].split(",")[0].replace("}","");
}
for (int i = 0; i < townID.length; i++)
{
ts = new TownSelector(townID[i], townName[i]);
// Binds all strings into an array
arraylist.add(ts);
}
// Pass results to ListViewAdapter Class
townAdapter = new ListViewAdapter(this, arraylist); <--Create new instant of adapter here
// Binds the Adapter to the ListView
townList.setAdapter(townAdapter); //This is the line that causes a crash
}
});
}
}
ListViewAdapter代码:
// Declare Variables
Context mContext;
LayoutInflater inflater;
private List<TownSelector> locationlist = null;
private ArrayList<TownSelector> arraylist;
public ListViewAdapter(Runnable runnable, List<TownSelector> locationlist) {
mContext = (Context) runnable;
this.locationlist = locationlist;
inflater = LayoutInflater.from(mContext);
this.arraylist = new ArrayList<TownSelector>();
this.arraylist.addAll(locationlist);
}
在找到这个堆栈流问题后,我将 ListViewAdapter 代码更改为下面的内容 android callback fails in fragment fails cannot be cast to android.content.Context
public ListViewAdapter(Context context,Runnable runnable, List<TownSelector> locationlist) {
mContext = context;
this.runnable = runnable;
this.locationlist = locationlist;
inflater = LayoutInflater.from(mContext);
this.arraylist = new ArrayList<TownSelector>();
this.arraylist.addAll(locationlist);
}
错误调用栈:
09-22 03:58:03.467: E/AndroidRuntime(15586): FATAL EXCEPTION: main
09-22 03:58:03.467: E/AndroidRuntime(15586): Process: biz.nickbullcomputing.bevnav, PID: 15586
09-22 03:58:03.467: E/AndroidRuntime(15586): java.lang.ClassCastException: biz.nickbullcomputing.bevnav.MainActivity$LoadAllInfo$1 cannot be cast to android.content.Context
09-22 03:58:03.467: E/AndroidRuntime(15586): at biz.nickbullcomputing.bevnav.ListViewAdapter.<init>(ListViewAdapter.java:25)
09-22 03:58:03.467: E/AndroidRuntime(15586): at biz.nickbullcomputing.bevnav.MainActivity$LoadAllInfo$1.run(MainActivity.java:409)
09-22 03:58:03.467: E/AndroidRuntime(15586): at android.app.Activity.runOnUiThread(Activity.java:4794)
09-22 03:58:03.467: E/AndroidRuntime(15586): at biz.nickbullcomputing.bevnav.MainActivity$LoadAllInfo.onPostExecute(MainActivity.java:376)
09-22 03:58:03.467: E/AndroidRuntime(15586): at biz.nickbullcomputing.bevnav.MainActivity$LoadAllInfo.onPostExecute(MainActivity.java:1)
09-22 03:58:03.467: E/AndroidRuntime(15586): at android.os.AsyncTask.finish(AsyncTask.java:632)
09-22 03:58:03.467: E/AndroidRuntime(15586): at android.os.AsyncTask.access$600(AsyncTask.java:177)
09-22 03:58:03.467: E/AndroidRuntime(15586): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
09-22 03:58:03.467: E/AndroidRuntime(15586): at android.os.Handler.dispatchMessage(Handler.java:102)
09-22 03:58:03.467: E/AndroidRuntime(15586): at android.os.Looper.loop(Looper.java:157)
09-22 03:58:03.467: E/AndroidRuntime(15586): at android.app.ActivityThread.main(ActivityThread.java:5867)
09-22 03:58:03.467: E/AndroidRuntime(15586): at java.lang.reflect.Method.invokeNative(Native Method)
09-22 03:58:03.467: E/AndroidRuntime(15586): at java.lang.reflect.Method.invoke(Method.java:515)
09-22 03:58:03.467: E/AndroidRuntime(15586): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
09-22 03:58:03.467: E/AndroidRuntime(15586): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)
09-22 03:58:03.467: E/AndroidRuntime(15586): at dalvik.system.NativeStart.main(Native Method)
最佳答案
您的 asyncTask townAdapter = new ListViewAdapter(this, arraylist);
中的这一部分不正确。你的编辑器应该显示错误。你需要传递 YourActivityName.this
只是这是发送 LoadAllInfo
作为错误的上下文
关于java - 可运行的 ListViewAdapter 导致 "cannot be cast to android.content.Context"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25966262/
1.这个ListView Adapter集成在fragment上 public class ListViewAdapter extends BaseAdapter { // Declare Var
我正在尝试从 ListViewAdapter 中删除一个项目,我已经实现了我的 setOnItemLongClickListener 执行此操作: getListView().setOnItemLon
我很困惑。我在网上找到了一些建议,但我无法在此代码上实现它。这是我的问题。每次我滚动时, ListView 的顺序都会困惑。我不知道该怎么做。我真的需要一些帮助。我会非常感谢你的好意。这是我的代码:
像往常一样,这个问题来自 Android 的新用户。三周后,我会尽力解释这个问题。 首先,我从今天开始使用 Exlipse 3.7.1 和 SDK r15,因为我想我会尝试升级,也许这就是导致问题的原
我想在 ListViewAdapter 中创建超过 1500 个 LyricsFullDetail,但我的问题是我不想在 ListViewAdapter.java 中硬编码 Lyrics Full D
我目前正在创建一个 Android 版 GoogleMaps 应用程序,并将位置列表存储在在线服务器的数据库中。我成功地拉动了它们,现在我想使用 Actionbar 搜索 View 创建一个用户可以搜
我正在创建一个具有一个 Mainactivity 和两个类的应用程序。它包含 ListView 适配器,用于显示带有图像和详细信息的名称列表。当单击任何图像时...将弹出警报对话框,从选项中,我希望用
我是一名优秀的程序员,十分优秀!