【发布时间】:2015-04-13 08:52:37
【问题描述】:
我有一个存储在 ArrayList > 中的 A 数据,从 JSON 中检索到 以形式(即)
[{price: =1685 name: =Monographie Der Gattung Pezomachus (Grv.) by Arnold F. Rster}]
我需要在 Android 中将所有地图元素显示为列表形式。 我尝试了很多方法,但我无法做到。
还帮助我了解其中使用的布局
已编辑:
MySimpleArrayAdapter adapter = new MySimpleArrayAdapter(myarr_list);
setListAdapter(adapter);
在 MySimpleArrayAdapter 类中,在构造函数中
public MySimpleArrayAdapter( ArrayList<HashMap<String,String>> pl) {
LayoutInflator inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
在此之后控制不会继续, MySimpleArrayAdapter 类
public class MySimpleArrayAdapter extends BaseAdapter{
ArrayList<HashMap<String, String>> ProductList = new ArrayList<HashMap<String, String>>();
LayoutInflater inflater;
@Override
public int getCount() {
// TODO Auto-generated method stub
return 0;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
//Constructor
public MySimpleArrayAdapter( ArrayList<HashMap<String,String>> pl) {
this.ProductList = pl;
inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public View getView(int position, View convertView, ViewGroup parent) {
View myview = convertView;
if (convertView == null) {
myview = inflater.inflate(R.layout.show_search_result, null);
}
TextView price = (TextView) myview.findViewById(R.id.price);
TextView name = (TextView) myview.findViewById(R.id.name);
HashMap<String, String> pl = new HashMap<String, String>();
pl = ProductList.get(position);
//Setting
price.setText(pl.get("price"));
name.setText(pl.get("name"));
return myview;
}
}
我在这里编辑 SearchResultsTask 中由 AsyncTask 扩展的 onPostExecute 类
protected void onPostExecute(JSONObject json) {
if (json != null && json.length() > 0) {
try {
JSONArray json_results = (JSONArray)(json.get("results"));
String parsedResult = "";
System.out.println("-> Size ="+ json_results.length());
for(int i = 0; i < json_results.length(); i++){
HashMap<String, String> map = new HashMap<String, String>();
JSONObject json_i = json_results.getJSONObject(i);
map.put("name: ",json_i.getString("name") + "\n");
map.put("price: ",json_i.getString("price") + "\n");
arr_list.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
System.out.println("-> Size =====arr_llist ="+ arr_list.size());
// CustomListAdapter adapter = new CustomListAdapter (arr_list);
//final StableArrayAdapter adapter = new StableArrayAdapter(this, R.id.result, arr_list);
// listview.setAdapter(adapter);
MyListActivity obj1 = new MyListActivity();
Bundle icicle = null;
obj1.onCreate(icicle);
}
public class MyListActivity extends Activity {
public void onCreate(Bundle icicle) {
// System.out.println("In my list Activity");
// super.onCreate(icicle);
//populate list
MySimpleArrayAdapter adapter = new MySimpleArrayAdapter(this,arr_list);
// System.out.println("in 2");
adapter.getView(0, listview, listview);
listview.setAdapter(adapter);
}
}
【问题讨论】:
-
向我们展示您尝试过但未成功的许多方法
-
向我们展示您的适配器的代码
-
MySimpleArrayAdapter#getView(){ // p 是位置,你会从 getView() 的 args 中得到它 HashMap
yourMapAtIndexP = yourList.get(P); String key = "你的键值"; String valueToShow = yourMapAtIndexP.get(key); // 现在在你想要的任何视图中显示它 } -
@GauravGupta ,我无法通过这个充气机 = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
-
嘿@GauravGupta,我的地图结构就像
{name: =Monographie Der Gattung Pezomachus (Grv.) by Arnold F.Rster, price: =1685},我无法通过你的方法访问这些值。帮我 ..!我做了这个System.out.println(mylist.get("price")); System.out.println(mylist.get("name"));,它给了null
标签: android android-arrayadapter