Android : Cannot display fetched json data into listview although it works in other framgents

on Saturday, April 18, 2015


i know my question is long but could you please help me? For my final year project i am developing an application that would display deals to registered vendors in the city. im having some troubles when trying to display json data. i am trying to display it in a listview which i have successfully done in other aspects of my application, but when i have tried to apply that code to this section it just doesn't display. i know that it is bring back the data because it shows in my logcat like below:



04-18 19:43:59.764 20014-20014/coursework.amisha.unilife W/ResourceType﹕ No package identifier when getting name for resource number 0x00000000
04-18 19:43:59.774 20014-20014/coursework.amisha.unilife W/ResourceType﹕ No package identifier when getting name for resource number 0x00000000
04-18 19:44:00.694 20014-20014/coursework.amisha.unilife E/STRING﹕ [{"dealid":"1","dealtitle":"20% Student Discount","startdate":"11\/04\/2015","expirydate":"20\/04\/2015","details":"H&M discount exclusively for students. Shop the new seasons trends and save extra with your exclusive 20% student discount. Just pop into out store today and show your student card at the checkout to receive your discount.","vendorid":"1","name":"H&M","type":"Fashion","category":"Shopping","address":"Bullring Shopping Centre Birmingham B5 4BF","telephone":"03447369000","email":"contact@hm.com","password":"7HroAMWZ3kkHFTkNo+hzowBQZXg1MDQ0ZTkzZjFm","salt":"5044e93f1f","photo":"http:\/\/www.amishanazran.tk\/images\/h&m1image.png"},{"dealid":"2","dealtitle":"half price dresses","startdate":"03\/03\/2015","expirydate":"30\/04\/2015","details":"H&M discount exclusively for students. Shop the new seasons trends and save extra with your exclusive 20% student discount. Just pop into out store today and show your student card at the checkout to receive your discount.","vendorid":"1","name":"H&M","type":"Fashion","category":"Shopping","address":"Bullring Shopping Centre Birmingham B5 4BF","telephone":"03447369000","email":"contact@hm.com","password":"7HroAMWZ3kkHFTkNo+hzowBQZXg1MDQ0ZTkzZjFm","salt":"5044e93f1f","photo":"http:\/\/www.amishanazran.tk\/images\/h&m1image.png"}]
<!-- Hosting24 Analytics Code -->
<script type="text/javascript" src="http://stats.hosting24.com/count.php"></script>
<!-- End Of Analytics Code -->


my php code to get this data from mysql database is shown below:



<?php
//PHP API for getting deals from vendor
// check to see if tag is set
header('Content-type: application/json');
// include database handler functions
require_once 'include/DB_Functions.php';
$db = new DB_Functions();
$vendorid = $_POST['vendorid'];

$sql = "SELECT * FROM deal JOIN vendor ON vendor.vendorid deal.vendorid
WHERE vendor.vendorid= $vendorid";
$result = mysql_query($sql) or trigger_error(mysql_error()." in ".$sql);

while($row = mysql_fetch_assoc($result)){
$output[]=$row;
}

json_encode($output);
echo $vendor;
echo(json_encode($output));


i know that my code uses the mysql_connect, mysql_query stuff but that was the only way i new how to connect to the database as i am very new at android development and i have used it throughout my application so it is late to change it if it isnt a big problem. i think the php and mysql side of my application is working fine becuase i know i can exchange data to and from android.


this is my fragment that would display the list of deals associated with the vendor at the bottom of the screen. the deals would be displayed depending on the vendor that was selected on the page before:



public class PlaceDetailFragment extends Fragment {
// URL used for getting vendors
private static String dealsinvendorsURL = "http://www.amishanazran.tk/dealsinvendors.php/";
private static final String TAG_V_ID = "vendorid";
private static final String TAG_NAME = "name";
private static final String TAG_TYPE = "type";
private static final String TAG_CATEGORY = "category";
private static final String TAG_ADDRESS = "address";
private static final String TAG_EMAIL = "email";
private static final String TAG_PHOTO = "photo";
private static final String TAG_TELEPHONE = "telephone";
private static final String TAG_DEAL_ID = "dealid";
private static final String TAG_DEAL_TITLE = "dealtitle";
private static final String TAG_START_DATE = "startdate";
private static final String TAG_EXPIRY_DATE = "expirydate";
private static final String TAG_DEAL_DESC = "details";

public String vendorid;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}{setHasOptionsMenu(true);}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.detail_place, parent, false);

// getting intent data
Intent i = getActivity().getIntent();

// Get JSON values from previous intent
vendorid = i.getStringExtra(TAG_V_ID);

getDealsForVendor(v);
String name = i.getStringExtra(TAG_NAME);
String type = i.getStringExtra(TAG_TYPE);
String category = i.getStringExtra(TAG_CATEGORY);
String address = i.getStringExtra(TAG_ADDRESS);
String telephone = i.getStringExtra(TAG_TELEPHONE);
String email = i.getStringExtra(TAG_EMAIL);
String imageSrc = i.getStringExtra(TAG_PHOTO);

TextView vendorName = (TextView)
v.findViewById(R.id.place_detail_name);
TextView vendorType = (TextView)
v.findViewById(R.id.place_detail_type);
TextView vendorAddress = (TextView)
v.findViewById(R.id.place_detail_address);
TextView vendorTelephone = (TextView)
v.findViewById(R.id.place_detail_telephone);
ImageView vendorPhoto = (ImageView)
v.findViewById(R.id.place_detail_photo);

// Displaying values on screen
vendorName.setText(name);
vendorType.setText(category + ", " + type);
vendorAddress.setText(address);
vendorTelephone.setText(telephone);
// get the image view string source and then
// convert to display the actual image

Bitmap bitmap = ListViewAdapter.getBitmapFromURL(imageSrc);
vendorPhoto.setImageBitmap(bitmap);


return v;
}

public void getDealsForVendor(View v){
String data;
final ArrayList<HashMap<String, String>> allDeals = new ArrayList<HashMap<String, String>>();
DealListAdapter adapter = new DealListAdapter(getActivity(),allDeals );
ListView list=(ListView)v.findViewById(R.id.vendor_deal_listview);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


// bringing the selected vendor details by starting new activity
Intent i = new Intent(getActivity(), DealsDetailActivity.class);
i.putExtra(TAG_DEAL_ID, allDeals.get(position).get("dealid"));
i.putExtra(TAG_DEAL_TITLE, allDeals.get(position).get("dealtitle"));
i.putExtra(TAG_START_DATE, allDeals.get(position).get("startdate"));
i.putExtra(TAG_EXPIRY_DATE, allDeals.get(position).get("expirydate"));
i.putExtra(TAG_DEAL_DESC, allDeals.get(position).get("details"));


startActivity(i);

}
});

try {

DefaultHttpClient defaultHttpClient = new DefaultHttpClient();
List<NameValuePair> args = new ArrayList<NameValuePair>();
args.add(new BasicNameValuePair("vendorid", vendorid));
HttpPost httpPost = new HttpPost(dealsinvendorsURL);
httpPost.setEntity(new UrlEncodedFormEntity(args));
HttpResponse httpResponse = defaultHttpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
data= EntityUtils.toString(httpEntity);


Log.e("STRING", data);

try {

JSONArray jsonArray = new JSONArray();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String sDealID = jsonObject.getString("dealid");
String sDealTitle = jsonObject.getString("dealtitle");
String sExpiryDate = jsonObject.getString("expirydate");
String sStartDate = jsonObject.getString("startdate");
String sDetails = jsonObject.getString("details");
Log.e(HomeActivity.TAG, " ERRORRRRRRR: " + sDealTitle);
HashMap<String, String> singleDeal = new HashMap<String, String>();
singleDeal.put(TAG_DEAL_ID, sDealID);
singleDeal.put(TAG_DEAL_TITLE, sDealTitle);
singleDeal.put(TAG_EXPIRY_DATE, sExpiryDate);
singleDeal.put(TAG_START_DATE, sStartDate);
singleDeal.put(TAG_DEAL_DESC, sDetails);
Log.e(HomeActivity.TAG, " ERRORRRRRRR: " + singleDeal);
allDeals.add(singleDeal);
Log.e(HomeActivity.TAG, allDeals.toString());
//allVendors.add(sType);
list.setAdapter(adapter);
}
} catch (JSONException e) {
e.printStackTrace();
}

}catch (ClientProtocolException e) {
Log.d(HomeActivity.TAG, e.getLocalizedMessage());
} catch (IOException e) {
Log.d(HomeActivity.TAG, e.getLocalizedMessage());
}
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_search:

return true;
case R.id.action_map:
Intent mapIntent = new Intent(getActivity(), MapActivity.class);
startActivity(mapIntent);
return true;
case R.id.action_profile:
Intent profileIntent = new Intent(getActivity(), ProfileActivity.class);
startActivity(profileIntent);

default:
return super.onOptionsItemSelected(item);
}
}

public class DealListAdapter extends BaseAdapter {
private Context context;
ArrayList<HashMap<String, String>> arrayList =
new ArrayList<HashMap<String, String>>();

public DealListAdapter(Context c, ArrayList<HashMap<String, String>> arrayList1){
this.context = c;
this.arrayList = arrayList1;
}
public int getCount() { return arrayList.size();}
public Object getItem(int position) {return arrayList.get(position);}
public long getItemId(int position){ return position;}

public View getView(int position, View convertView, ViewGroup parent){
LayoutInflater inflater = (LayoutInflater)context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null){
convertView = inflater.inflate(R.layout.vendor_deals_list_item, null);
}


TextView dealTitle = (TextView)convertView.findViewById(R.id.vendor_deal_listitem_title);
dealTitle.setText(arrayList.get(position).get("name"));
Log.e(HomeActivity.TAG, " ERRORRRRRRR: " + dealTitle);
TextView expiryDate = (TextView)convertView.findViewById(R.id.vendor_deal_listitem_expirydate);
expiryDate.setText(arrayList.get(position).get("type"));

return convertView;
}
}
}


im not sure why it isnt working as the concepts that i use here are used thoughout my coding when popluating listview and gridviews in other fragments and they work. i don't get any errors in the logcat either.


could you please help me because i am struggling and i havent been able to find anything that can resolve this problem. i have tried to search for parsing errors, listview displaying problems and adapter problems but i cant seem to find a solution that works for me.


Thank you in advance :)


0 comments:

Post a Comment