从listview java

时间:2015-10-11 10:24:35

标签: java android listview

代码显示我从PHP代码获取JSON并将其添加到Reservation User类型的列表中,然后在数组Adapter中烘烤该列表。 但我的问题是:我想从列表视图中再次获取这些参数, (日期,时间,tarif,stadiumN)当我点击一个特定的项目(只是那些参数,没有别的) 任何想法怎么样? 如图所示我输入

`myreservations.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Log.e("aaaaaaaaaaaaa: ",list1.get(i).toString());

            }
        });
    }

但它通过添加文本来输出toString。

public class UserShowReservation extends ActionBarActivity {
ListView myreservations;
ArrayList<ReservationUser> list1 = new ArrayList<ReservationUser>();
JSONParser parser = new JSONParser();
PreferenceUtils preferenceUtils;
int success=-1;
String id;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.user_show_reservation);
    myreservations = (ListView)findViewById(R.id.lisreservationuser);
    preferenceUtils = new PreferenceUtils(getApplicationContext());
    id=preferenceUtils.getLastId();
    list1.clear();
    new NetworkRequestTask().execute();

    myreservations.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            Log.e("aaaaaaaaaaaaa: ",list1.get(i).toString());

        }
    });
}

private class NetworkRequestTask extends AsyncTask<Void, Void, String> {

    protected void onPreExecute() {
    }

    @Override
    protected String doInBackground(Void... params) {
        String connect = Utility.getReservationsUser+"userid="+id;
        List<NameValuePair> params1 = new ArrayList<NameValuePair>();
        JSONObject json = parser.makeHttpRequest(connect, "POST", params1);
        try {
            success = json.getInt("success");
            if (success == 1) {
                JSONArray arr = json.getJSONArray("reservations");
                for (int i = 0; i < arr.length(); i++) {
                    ReservationUser ru = new ReservationUser();
                    ru.date = arr.getJSONObject(i).getString("date");
                    ru.time = arr.getJSONObject(i).getInt("time");
                    ru.finaltarif = arr.getJSONObject(i).getDouble("finaltarif");
                    ru.stadiunN=arr.getJSONObject(i).getString("name");
                    list1.add(ru);
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    protected void onPostExecute(String result) {
        if(success==1) {
            ArrayAdapter<ReservationUser> adapter = new ArrayAdapter<ReservationUser>(getApplicationContext(),
                    android.R.layout.simple_list_item_1, list1);
            myreservations.setAdapter(adapter);
        }
    }
}

}

public class ReservationUser {
    public String date;
    public int time;
    public double finaltarif;
    public String stadiunN;
    public String toString() {
        return "DATE : " + date + "\nPrice : " + finaltarif + " $" + "\nTime : " + time + ":00" + "\nStadium : " + stadiunN;
    }

}

0 个答案:

没有答案