以下は、jsonから解析されたリストビューにデータを表示する私のコードです。
ユーザーがリスト内の任意の項目をクリックしたときに、新しいアクティビティを開始したいと考えています。
この URL http://www.androidhive.info/2012/01/android-json-parsing-tutorial/をたどりました。これは json ファイルです http://api.androidhive.info/contacts/
ユーザーがリストビュー内の任意のアイテムをクリックしたときに新しいアクティビティを開始し、残りの json 値をパラメーターとして渡す方法は?
現在、リストビューには名前のみが表示されていますが、メール、性別、モバイルなどの残りの項目を他のアクティビティに渡したいと考えています。
"id": "c200",
"name": "Ravi Tamada",
"email": "ravi@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
public class NewsRowAdapter extends ArrayAdapter<Item> {
private Activity activity;
private List<Item> items;
private Item objBean;
private int row;
Context context;
public NewsRowAdapter(Activity act, int resource, List<Item> arrayList) {
super(act, resource, arrayList);
this.activity = act;
this.row = resource;
this.items = arrayList;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
ViewHolder holder;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(row, null);
holder = new ViewHolder();
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
if ((items == null) || ((position + 1) > items.size()))
return view;
objBean = items.get(position);
holder.tvName = (TextView) view.findViewById(R.id.txtText);
if (holder.tvName != null && null != objBean.getName()
&& objBean.getName().trim().length() > 0) {
holder.tvName.setText(Html.fromHtml(objBean.getName()));
Intent intent=new Intent(context,TodayLunch.class);
intent.putExtra("name", Html.fromHtml(objBean.getName()));
context.startService(intent);
}
return view;
}
public class ViewHolder {
public TextView tvName, tvCity, tvBDate, tvGender, tvAge;
}
}
package com.schoollunchapp;
public class SeletecDayofweek extends Activity implements OnItemClickListener {
private static final String rssFeed = "http://192.168.2.100/jsonparsing.txt";
private static final String ARRAY_NAME = "student";
private static final String ID = "id";
private static final String NAME = "name";
private static final String CITY = "dish";
private static final String GENDER = "Gender";
private static final String AGE = "age";
private static final String BIRTH_DATE = "birthdate";
ListView listMainMenu;
List<Item> arrayOfList;
//MainMenuAdapter mma;
NewsRowAdapter objAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.selectdayofweek);
listMainMenu = (ListView) findViewById(R.id.listMainMenu2);
listMainMenu.setOnItemClickListener(this);
arrayOfList = new ArrayList<Item>();
if (URLUtils.isNetworkAvailable(SeletecDayofweek.this)) {
new MyTask().execute(rssFeed);
} else {
showToast("No Network Connection!!!");
}
}
// My AsyncTask start...
class MyTask extends AsyncTask<String, Void, String> {
ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(SeletecDayofweek.this);
pDialog.setMessage("Loading...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected String doInBackground(String... params) {
return URLUtils.getJSONString(params[0]);
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if (null != pDialog && pDialog.isShowing()) {
pDialog.dismiss();
}
if (null == result || result.length() == 0) {
showToast("No data found from web!!!");
SeletecDayofweek.this.finish();
} else {
try {
JSONObject mainJson = new JSONObject(result);
JSONArray jsonArray =
mainJson.getJSONArray(ARRAY_NAME);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject objJson =
jsonArray.getJSONObject(i);
Item objItem = new Item();
objItem.setId(objJson.getInt(ID));
objItem.setName(objJson.getString(NAME));
objItem.setCity(objJson.getString(CITY));
objItem.setGender(objJson.getString(GENDER));
objItem.setAge(objJson.getInt(AGE));
objItem.setBirthdate(objJson.getString(BIRTH_DATE));
arrayOfList.add(objItem);
}
} catch (JSONException e) {
e.printStackTrace();
}
setAdapterToListview();
}
}
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// showDeleteDialog(position);
}
public void setAdapterToListview() {
objAdapter = new NewsRowAdapter(SeletecDayofweek.this,
R.layout.main_menu_item,
arrayOfList);
listMainMenu.setAdapter(objAdapter);
}
public void showToast(String msg) {
Toast.makeText(SeletecDayofweek.this, msg, Toast.LENGTH_LONG).show();
}
}