循环列表 - 选择列表中的多个项目

时间:2014-08-05 02:09:32

标签: java android json android-layout android-activity

大家好日子,

简而言之,我有一个列表,通过JSON数据填充数组列表。点击后,它会将用户带到单个项目活动,这实际上提供了有关该特定项目的更多信息。现在我希望用户最多这样做三次。换句话说,在用户点击数组列表上的三个不同项目之前,它会不断提示他们在列表中选择其他项目,直到他们选择了3个项目。一旦选择了3个项目,用户将被重定向到另一个活动页面。

以下是数组活动页面列表的代码

public class EventsActivity extends Activity{

    private static final String URL_WEB_SERVICE = "http://dooba.ca/analytics/ed.php";
    private GridView gv;
    private ArrayList<Events_List> container;
    private ArrayList<Events_List> items;
    public Uri list_item_bac;
    public String list_item_name;
    public String list_item_description;
    public String list_item_price;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.events_list_layout);
        gv = (GridView) findViewById(R.id.gridview);
        container = new ArrayList<Events_List>();
        //download JSON
        listDownload();


        GridView s = (GridView) findViewById(R.id.gridview);
        s.setOnItemClickListener(new OnItemClickListener(){

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(EventsActivity.this,EventSingleItemActivity.class);

                intent.putExtra("list_item_name", list_item_name);
                intent.putExtra("list_item_description", list_item_description);
                intent.putExtra("list_item_price",list_item_price);

                startActivity(intent); //start Activity
            }
        });
    }
    public void listDownload(){
        RequestQueue volley = Volley.newRequestQueue(this);
        JsonObjectRequest json = new JsonObjectRequest(Method.GET, URL_WEB_SERVICE, null, ResponseListener(), ErrorListener());
        volley.add(json);
    }

    private Response.Listener<JSONObject> ResponseListener() {
        return new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    //your JSON Array
                    JSONArray array = response.getJSONArray("list_item");
                    for(int i = 0; i < array.length(); i++){
                        container.add(convertirAnuncio(array.getJSONObject(i)));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                gv.setAdapter(new AdapterEvents(getApplicationContext(),container));
                }
            };
        };


    private Response.ErrorListener ErrorListener() {
        return new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) { }
        };
    }

    //object JSON
    private final Events_List convertirAnuncio(JSONObject obj) throws JSONException {
        long id = obj.getLong("id"); //id 
        String list_item_name = obj.getString("list_item_name"); 
        String list_item_description = obj.getString("list_item_description");
        String list_item_price = obj.getString("list_item_price");
        Uri uri = Uri.parse(obj.getString("list_item_bac"));
        return new Events_List(id,list_item_name,list_item_description,list_item_price, uri);
    }
}

以下是单项活动的代码

public class EventSingleItemActivity extends Activity {

    // Declare Variables
    String list_item_name;
    String list_item_description;
    String list_item_price;



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_events_single_item);

        Intent i = getIntent();
        list_item_name = i.getStringExtra("list_item_name");
        list_item_description = i.getStringExtra("list_item_description");
        list_item_price = i.getStringExtra("list_item_price");

        TextView txtname = (TextView) findViewById(R.id.name);
        TextView txtdescription = (TextView) findViewById(R.id.description);
        TextView txtprice = (TextView) findViewById(R.id.price);

        // Set results to the TextViews
        txtname.setText(list_item_name);
        txtdescription.setText(list_item_description);
        txtprice.setText(list_item_price);


    }
}

提前致谢,我感谢所有人的帮助。

0 个答案:

没有答案
相关问题