这是我的代码,它在列表视图中显示 JSON 属性。我在列表视图中仅显示“名称”,我想将其他属性作为参数传递给其他活动。下面是我的代码,我只想在列表视图中显示名称“星期一、星期二、星期三”。当用户单击“dish、Gender、age”等其他值时,将作为参数传递给其他 Activity。我该怎么做?
{
"student": [
{
"id": 1,
"name": "Monday",
"dish": "Biryani",
"Gender": "M",
"age": 10,
"birthdate": "23/05/2002"
},
{
"id": 2,
"name": "Tuesday",
"dish": "Sandwish",
"Gender": "M",
"age": 12,
"birthdate": "08/01/2000"
},
{
"id": 3,
"name": "Wednesday",
"dish": "Chicken Tikka",
"Gender": "F",
"age": 14,
"birthdate": "01/03/1998"
},
public class SeletecDayofweek extends Activity implements
OnItemClickListener {
private static final String rssFeed = "https://www.dropbox.com/s/rhk01nqlyj5gixl
/jsonparsing.txt?dl=1";
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;
String myname;
JSONObject objJson ;
@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++) {
objJson = jsonArray.getJSONObject(i);
Item objItem = new Item();
objItem.setId(objJson.getInt(ID));
objItem.setName(objJson.getString(NAME));
myname= 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) {
}
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();
}
}
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()));
}
return view;
}
public class ViewHolder {
public TextView tvName, tvCity, tvBDate, tvGender, tvAge;
}
}
public class Item {
private int id;
private String name;
private String city;
private String gender;
private int age;
private String birthdate;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getBirthdate() {
return birthdate;
}
public void setBirthdate(String birthdate) {
this.birthdate = birthdate;
}
}