单个项目单击列表

时间:2014-08-06 03:09:53

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

有了支持,我创建了一个通过JSON数据填充的数组列表,当用户点击某个项目时,它会将它们带到一个活动页面,为其提供有关它的更多信息。我试图完成这个,但到目前为止的结果并不令人满意。在某种意义上,数组列表确实成功显示,但是当单击某个项目时,会意外显示空白活动页面。

下面是数组活动代码列表:

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);


    }

以下是单项XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/price"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/name" />

    <TextView
        android:id="@+id/description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/price" />

    <ImageView
        android:id="@+id/image_head"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:background="#000000"
        android:padding="1dp" />

</RelativeLayout>
    }

更新

    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", container.get(position).getList_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);
        }
    }

Any help would be greatly appreciated.

更新2 事件列表活动

public class Events_List {
    public long id;
    public String list_item_title;
    public String list_item_price;
    public String list_item_description;
    public Uri url;

    public Events_List(long id, String list_item_title, String list_item_description, String list_item_price, Uri url){
        this.id = id;
        this.list_item_title = list_item_title;
        this.list_item_description = list_item_description;
        this.list_item_price = list_item_price;
        this.url = url;
    }

}    

1 个答案:

答案 0 :(得分:1)

<强>问题:

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

这些值是空的,因为它们从未被使用或初始化,因此当您切换活动并从意图中获取值时,它将返回空字符串,从而不会给您带来结果

<强>溶液

使用您的arraylist对象container并使用onItemClick的位置,并使用arraylist的get方法获取值

<强>样品:

intent.putExtra("list_item_name", container.get(position).getList_item_name);

请记住,您的班级Events_List必须拥有List_item_name getter 方法

<强>更新

public class Events_List {
public long id;
public String list_item_title;
public String list_item_price;
public String list_item_description;
public Uri url;

public Events_List(long id, String list_item_title, String list_item_description, String list_item_price, Uri url){
    this.id = id;
    this.list_item_title = list_item_title;
    this.list_item_description = list_item_description;
    this.list_item_price = list_item_price;
    this.url = url;
}

public String getList_item_title()
{
    return this.list_item_title;
}

public String getList_item_price()
{
    return this.list_item_price;
}

public String getList_item_description()
{
    return this.list_item_description;
}

} 

如何使用

    intent.putExtra("list_item_name", container.get(position).getList_item_title());
intent.putExtra("list_item_description", container.get(position).getList_item_description());
intent.putExtra("list_item_price",container.get(position).getList_item_description());
相关问题