- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一个应用程序,可以在 reomte 数据库上进行查询,使用 Json 和 PHP 通过 MySQL 稍后将结束 posgress,因为它更加开放。
我的问题是,我的 SimpleAdapter 被 Eclipse 认为是未定义的,没有任何快速修复,奇怪的是,在我尝试使用输入示例时添加按钮之前,一切都工作正常。因此,我不知道如何修复这个错误。
代码是:
import java.util.ArrayList;
import java.util.HashMap;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class DataconectActivity extends ListActivity {
EditText searcher;
Button btmsearch;
// adding strings that are going to be the tags of the nodes for the list view in the ListAdapter
private static String TAG_PRODID = "codigo";
private static final String TAG_NOMBRE = "nombre";
private static final String TAG_DESCRIPCION = "descripcion";
private static final String TAG_COLOR = "color";
private static final String TAG_PESO = "peso";
private static final String TAG_PRECIO = "precio";
private static final String TAG_DISPONIBILIDAD = "disponibilidad";
private static final String TAG_INFO = "resultados";
// remember to change the url to try it with the web server http://trialsols.webege.com/mobil.php after this is important to add at least 10.000 items to the db to ensure the query efficiency
private static final String url ="http://192.168.1.50/mobilv1.php";
ArrayList<HashMap<String, String>> jsonResultList = new ArrayList<HashMap<String, String>>();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
searcher = (EditText) findViewById(R.id.searchtxt);
btmsearch = (Button) findViewById(R.id.searchbtm);
btmsearch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// Hashmap for ListView
String data = searcher.getText().toString() ;
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("precio",data));
// Creating JSON Parser instance
JsonParser jParser = new JsonParser();
// getting JSON string from URL
JSONArray jArray = jParser.getJSONFromUrl(nameValuePairs, url);
try {
// looping through All JsonData
for(int i = 0; i < jArray.length() ; i++){
JSONObject json_data = jArray.getJSONObject(i);
String nombre = json_data.getString("nombre");
String prodid = json_data.getString("prodid");
String descripcion = json_data.getString("descripcion");
String color = json_data.getString("color");
String peso = json_data.getString("peso");
String precio = json_data.getString("precio");
String disponibilidad = json_data.getString("disponibilidad");
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_PRODID, prodid);
map.put(TAG_NOMBRE, nombre);
map.put(TAG_DESCRIPCION, descripcion);
map.put(TAG_COLOR, color);
map.put(TAG_PESO, peso);
map.put(TAG_PRECIO, precio);
map.put(TAG_DISPONIBILIDAD, disponibilidad);
// adding HashList to ArrayList
jsonResultList.add(map);
}
}
catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
//adding the results of the parsed JSON data to a listView using a ListAdapter
ListAdapter adapter = new SimpleAdapter(this, jsonResultList,
R.layout.datalistitems,
new String[] { TAG_NOMBRE, TAG_PRECIO, TAG_DISPONIBILIDAD }, new int[] {
R.id.dbnombretxt, R.id.dbpreciotxt, R.id.dbdisponibilidadtxt });
// add in a ListActivity We need to make
setListAdapter(adapter);
// initialize a ListView to display the single item information
ListView lv = getListView();
lv.setTextFilterEnabled(true);
// generate the action when an item is clicked
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, create a view with the values
// getting values from selected ListItem
String name = ((TextView) view.findViewById(R.id.dbnombretxt)).getText().toString();
String cost = ((TextView) view.findViewById(R.id.dbpreciotxt)).getText().toString();
String stock = ((TextView) view.findViewById(R.id.dbdisponibilidadtxt)).getText().toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(), SearchResults.class);
in.putExtra(TAG_NOMBRE, name);
in.putExtra(TAG_PRECIO, cost);
in.putExtra(TAG_DISPONIBILIDAD, stock);
startActivity(in);
}
});
}
});
}
}
错误确实出现:
adapter = new SimpleAdapter(this, jsonResultList,
R.layout.datalistitems,
new String[] { TAG_NOMBRE, TAG_PRECIO, TAG_DISPONIBILIDAD }, new int[] {
R.id.dbnombretxt, R.id.dbpreciotxt, R.id.dbdisponibilidadtxt });
最佳答案
您正在 View.OnClickListener
内部构建 SimpleAdapter
实例,因此您对 this
的引用指向 OnClickListener
,这不是有效的上下文
。您将需要像这样修改您的代码行:
adapter = new SimpleAdapter(DataconectActivity.this, jsonResultList,
R.layout.datalistitems,
new String[] { TAG_NOMBRE, TAG_PRECIO, TAG_DISPONIBILIDAD }, new int[] {
R.id.dbnombretxt, R.id.dbpreciotxt, R.id.dbdisponibilidadtxt });
这将为您的构造函数提供正确的引用。
HTH
关于java - 构造函数 SimpleAdapter 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9420968/
这是我的适配器 public class Vk_row_adapter extends SimpleAdapter { final String ATTRIBUTE_NAME_TEXT_NAME =
行中的空指针异常: listView.setAdapter(sAdapter); public class MyEmployeFragment extends Fragment { M
我想填写我的 ListView一些数据,我使用 SimpleAdapter .但是我觉得SimpleAdapter仅适用于 List>我有 List喜欢遵循代码: 我能做什么?有什么办法吗? List
代码编译没有错误,但结果在我的手机上只是一个空列表。那么问题是什么?这是我在 Activity 中的 onCreate 方法 super.onCreate(savedInstanceState
我使用了两个不同颜色的自定义行。但它返回同一行黑色。我也有一个黄色行,但它只返回一个黑色行。我无法理解它,因此我在下面分享了我的源代码。请帮助我 json_orders = json.getJSONA
我有一个带有过滤 inputSearch 和可点击项(我从 MySQL 数据库中获取)的 SimpleAdapter。 有时当我吃午饭时我的项目会出现错误: java.lang.RuntimeExce
我有一个与自定义 SimpleAdapter 关联的 ListView 。我制作了自定义 SimpleAdapter,因为我想将不同的事件处理程序与包含的 TextView 和 ImageView 相
我是 Android 开发新手,我花了几天时间尝试让我的应用程序运行,但没有成功。我正在使用 AsyncTask 和 SimpleAdapter,它返回数据,但是我的 JSON 有子项。它是注释的 J
我向 SQLite 添加了许多数据,但我的 ListView 仅显示最后插入的数据。所有行都有相同的值。 public void onResume() { super.onRe
我想根据应用程序中的某些值对我的 Listview 进行排序,但我不知道如何编码。我研究发现人们正在使用比较器和集合来对数组列表进行排序。基本上,我想按某些状态进行排序,例如接受或拒绝。该值存储在 T
我正在使用此代码创建一个 SimpleAdapter String[] from = { ATTRIBUTE_NAME_TEXT, ATTRIBUTE_NAME_CHECKED, ATTRIBUT
我正在开发一个应用程序,可以在 reomte 数据库上进行查询,使用 Json 和 PHP 通过 MySQL 稍后将结束 posgress,因为它更加开放。 我的问题是,我的 SimpleAdapte
当我运行程序时,每行中的 TextView 文本都从默认文本更改为无。 HashMap 中的数据未显示。 我的主要应用布局(main.xml): 我的列表项布局(myrow
我有一个 SimpleAdapter,我用它来显示两行 ListView 。我希望这个简单适配器上的第 15 个位置具有不同的格式,但是当我滚动 ListView 时,它会丢失位置并且格式会更改位置。
我正在尝试在 SET 按钮上填充 ListView 。当我选择值微调器并根据列表进行填充时。我面临的问题是我多次点击 SET 按钮,它会将项目添加到 ListView 。 setButton.s
我用 json 创建 Rss,当我从后台获取 jsonObject 时,在 postExecute 中我用对象结果创建 Map,然后出现错误LogCat 导致 SimpleAdapter 中的 Web
在我的应用程序中,我试图在单击列表项时将 TextView 设置为删除线。但是 Listview clik 项目不起作用。 代码如下: listView = ( ListView ) find
我正在从 web.xml 文件和图像中读取数据。我将图像作为 Bitmap bmImg 获取,并将其放入 Hashmap 中。但我确实知道如何修改我的 simpleAdapter 函数以在 image
我目前正在使用联系人的 ContentProvider 从设备中检索联系人,并允许用户通过在 EditText 中键入来过滤结果。 为此,我在 SimpleAdapter 上设置了一个过滤器查询,如下
我使用 SAXparser 从各种提要中获取数据,然后使用 SimpleListAdapter 将其添加到单个列表中, 我使用以下代码将 RSS Feed 添加到我的 SimpleListAdapte
我是一名优秀的程序员,十分优秀!