我从数据库中的JSON文件填充ArrayList并将它们加载到ListAdapter中。我的代码:
private JSONArray mList = null;
private ArrayList<HashMap<String, String>> mItemList;
...
public void updateJSONdata() {
mItemList = new ArrayList<HashMap<String, String>>();
JSONParser jParser = new JSONParser();
JSONObject json = jParser.getJSONFromUrl(READ_COMMENTS_URL);
try {
mList = json.getJSONArray(TAG_ITEMS_REPORT);
for (int i = 0; i < mList.length(); i++) {
JSONObject c = mList.getJSONObject(i);
// gets the content of each tag
String iName = c.getString(TAG_ITEM_NAME);
...
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_ITEM_NAME, iName);
...
// adding HashList to ArrayList
mItemList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
在这里,我更新了我的ListAdapter
并设置了onItemCLick
private void updateList() {
final ListAdapter adapter = new SimpleAdapter(this, mItemList,
R.layout.single_item_view,
new String[]{TAG_ITEM_NAME, TAG_ITEM_INFO,
TAG_ITEM_QUANTITY, TAG_ITEM_COMMENT, TAG_ITEM_LOCATION},
new int[]{R.id.singleItemView_itemName, R.id.singleItemView_ItemInfo,
R.id.login_username});
setListAdapter(adapter);
final ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View viewClicked,
int position, long id) {
String lst = mItemList.get(position).toString();
Toast.makeText(ReportViewer.this, "Position " + parent.getItemAtPosition(position), Toast.LENGTH_LONG).show();
Log.d(" ReportView onItemClick",
});
}
现在,我非常确定String lst
不是提取数据的最优雅和正确的方法。它确实为每个点击的位置提供了一个正确的字符串,但是所有值显然都包含在一个字符串中。如何一次提取一个值,例如TAG_ITEM_IFNO?感谢。
答案 0 :(得分:0)
你可以这样做:
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
// 1st Approach
HashMap<String, String> map = (HashMap<String, String>) l.getItemAtPosition(position);
Set<Entry<String, String>> entrySet = map.entrySet();
for(Entry<String, String> entry: entrySet) {
String key=entry.getKey();
String value=entry.getValue();
if(key=="your_key_that_you_have_mapped_to_some_textview_in_your_layout") {
// do what you want
}
}
// 2nd Approach
TextView tv=(TextView) v.findViewById(R.id.login_username);
String myvalue=tv.getText().toString();
}
而不是:
final ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View viewClicked,
int position, long id) {
String lst = mItemList.get(position).toString();
答案 1 :(得分:0)
这对我来说很有把戏
#include <windows.h>
#include "gui.h"
void CreateButtonControls(HWND hWindow, HWND* hButtonConvert)
{
const char* buttonConvertCaption = "Convert";
const unsigned int buttonConvertXPosition = 100;
const unsigned int buttonConvertYPosition = 10;
const unsigned int buttonConvertWidth = 125;
const unsigned int buttonConvertHeight = 50;
const DWORD buttonConvertStyle = WS_CHILD | WS_VISIBLE;
*hButtonConvert = CreateWindowEx(WS_EX_CLIENTEDGE, "Button", buttonConvertCaption, buttonConvertStyle, buttonConvertXPosition, buttonConvertYPosition, buttonConvertWidth, buttonConvertHeight, hWindow, (HMENU)IDB_CONVERT, NULL, NULL);;
}
void CreateEditControls(HWND hWindow, HWND* hEditNumber)
{
const char* editNumberCaption = NULL;
const unsigned int editNumberXPosition = 10;
const unsigned int editNumberYPosition = 10;
const unsigned int editNumberWidth = 50;
const unsigned int editNumberHeight = 30;
const DWORD editNumberStyle = WS_CHILD | WS_VISIBLE | WS_BORDER;
*hEditNumber = CreateWindowEx(WS_EX_CLIENTEDGE, "Edit", editNumberCaption, editNumberStyle, editNumberXPosition, editNumberYPosition, editNumberWidth, editNumberHeight, hWindow, (HMENU)IDE_NUMBER, NULL, NULL);
}
代码
mItemList.get(position).get(value)