Reputation:
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. 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 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
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;
}
}
Upvotes: 0
Views: 107
Reputation: 26198
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
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
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());
Upvotes: 0