简体   繁体   English

单项单击列表

[英]Single Item Click in a list

With support, I have created a list of arrays populated through JSON data, and where when a user clicks on an item, it takes them to an activity page that provides them with more information about it. 在支持下,我创建了一个通过JSON数据填充的数组的列表,并且当用户单击某个项目时,该列表将它们带到一个活动页面,该页面为他们提供了有关它的更多信息。 I have tried to accomplish this but the result so far have been unsatisfactory. 我已尝试完成此操作,但到目前为止效果并不理想。 In the sense that the list of arrays do display successfully, but when an item is click a blank activity page is unexpectedly shown. 从某种意义上说,数组列表确实可以成功显示,但是当单击某个项目时,会意外显示空白活动页面。

Below is list of arrays activity code: 以下是数组活动代码列表:

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

Below is the single item click 以下是单项点击

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


    }

Below is the single item XML 以下是单项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>
    }

Update 更新

    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.

Update 2 Event List Activity 更新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;
    }

}    

problem: 问题:

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

Those values are empty because they are never used or initialized, so by the time you switch activity and get the values from the intent it will return empty string thus giving you no result 这些值是空的,因为它们从未使用过或未初始化,因此,当您切换活动并从意图中获取值时,它们将返回空字符串,因此没有结果

solution: 解:

use your arraylist object container and use the position of the onItemClick and get the values using the get method of the arraylist 使用您的arraylist对象container并使用onItemClick的位置,并使用arraylist的get方法获取值

sample: 样品:

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

remember that your class Events_List must have a getter method for List_item_name 记住,你的类Events_List必须有一个getter方法List_item_name

UPDATE: 更新:

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

} 

how to use it: 如何使用它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM