JSONをマップとして取得して配列リストに保存し、それらを文字列として取得してリストに保存し、リストから1つずつ取得する方法は?
質問する
59 次
1 に答える
1
以前同じ作業をしました。私のアクティビティを確認してください:
public class BandMembers extends ListActivity {
public static final String TAG_MEMBER_NAME = "member_name";
public static final String TAG_MEMBER_SKILL = "member_skill";
public static final String TAG_THUMB_MEMBER_IMAGE = "member_thumb_image";
public static String URL = "http://clients.xxxxxxxx.com/xxxxxx/web_service/mainAPI.php";
ArrayList<HashMap<String, String>> memberList;
String responseData = null;
private ListView listView;
TextView title;
protected ImageLoader imageLoader = ImageLoader.getInstance();
DisplayImageOptions options;
public static class Config {
public static final boolean DEVELOPER_MODE = false;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.band_members);
Typeface tf = Typeface.createFromAsset(this.getAssets(), "font.TTF");
title = (TextView)findViewById(R.id.textBandMembers);
title.setEllipsize(TextUtils.TruncateAt.END);
title.setTypeface(tf);
options = new DisplayImageOptions.Builder()
.showStubImage(R.drawable.loading)
.showImageForEmptyUri(R.drawable.no_image_available)
.showImageOnFail(R.drawable.no_image_available)
.cacheInMemory()
.cacheOnDisc()
.bitmapConfig(Bitmap.Config.RGB_565)
.build();
memberList = new ArrayList<HashMap<String,String>>();
new AsyncData().execute();
}
class AsyncData extends AsyncTask<String, Void, Void> {
ProgressDialog pDialog;
@Override
protected void onPreExecute() {
pDialog = new ProgressDialog(BandMembers.this);
pDialog.setTitle("Loading...");
pDialog.setMessage("Please wait...");
pDialog.show();
super.onPreExecute();
}
@Override
protected Void doInBackground(String... args) {
// TODO Auto-generated method stub
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(URL);
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("rquest","{\"method\":\"band_members\",\"body\":[{}]}"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
responseData = EntityUtils.toString(resEntity);
try {
JSONArray data = new JSONArray(responseData);
for (int i = 0; i < data.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
JSONObject c = data.getJSONObject(i);
String memberName = c.getString(TAG_MEMBER_NAME);
String memberSkill = c.getString(TAG_MEMBER_SKILL);
String memberImage = c.getString(TAG_THUMB_MEMBER_IMAGE);
map.put(TAG_MEMBER_NAME, memberName);
map.put(TAG_MEMBER_SKILL, memberSkill);
map.put(TAG_THUMB_MEMBER_IMAGE, memberImage);
memberList.add(map);
}
} catch (JSONException e) {
// TODO: handle exception
e.printStackTrace();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Void result) {
super.onPostExecute(result);
listView = (ListView) findViewById(android.R.id.list);
((ListView) listView).setAdapter(new BandMemberAdapter(memberList));
super.onPostExecute(result);
if (pDialog != null && pDialog.isShowing()) {
pDialog.dismiss();
}
}
}
public class BandMemberAdapter extends BaseAdapter {
ArrayList<HashMap<String, String>> memberListTemp;
HashMap<String, String> myData;
public BandMemberAdapter(ArrayList<HashMap<String, String>> memberList1)
{
memberListTemp=memberList1;
}
@Override
public int getCount() {
return memberListTemp.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
TextView textName;
TextView textSkill;
ImageView image;
if (convertView == null) {
view = getLayoutInflater().inflate(R.layout.band_member_row_file, parent, false);
image = (ImageView) view.findViewById(R.id.imageMember);
textName = (TextView) view.findViewById(R.id.textMemberName);
textSkill = (TextView) view.findViewById(R.id.textMemberInfo);
} else {
return convertView;
}
myData = memberListTemp.get(position);
Typeface tfb = Typeface.createFromAsset(getApplicationContext().getAssets(), "fontbold.TTF");
textName.setText(myData.get(BandMembers.TAG_MEMBER_NAME));
textName.setTypeface(tfb);
textSkill.setText(myData.get(BandMembers.TAG_MEMBER_SKILL));
textSkill.setTypeface(tfb);
String uri = "http://clients.xxxxxxx.com/xxxxxxxx/web_admin/assets/images/bandmembers/thumbs/" + myData.get(BandMembers.TAG_THUMB_MEMBER_IMAGE).replace(" ", "%20");
imageLoader.displayImage(uri, image, options);
return view;
}
}
}
これがお役に立てば幸いです。
于 2013-08-25T17:23:08.863 に答える