I thought I parsed my data in the right manner but I am unable to have it display. Could you please let me know where my error is?
I have various things that are being passed but I only want each item on the list view to display certain attributes and the following is my code. I'm just having a blank main activity show up.
public class MainActivity extends Activity {
List<Product> productList = new ArrayList<Product>();
ListAdapter adapter = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String result = "";
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost();
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
InputStream is = httpEntity.getContent();
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result" + e.toString());
}
} catch (Exception e) {
Log.e("log_tag", "Error in http connection" + e.toString());
}
try {
JSONArray json = new JSONArray(result);
for (int i = 0; i < json.length(); i++) {
JSONObject productsMade = json.getJSONObject(i);
int PRODUCT_ID = productsMade.getInt("productId");
int STYLE_ID = productsMade.getInt("styleId");
String BRAND_NAME = productsMade.getString("brandName");
String PRODUCT_NAME = productsMade.getString("productName");
int COLOR_ID = productsMade.getInt("colorId");
int ORIGINAL_PRICE = productsMade.getInt("originalPrice");
int PERCENT_OFF = productsMade.getInt("percentOff");
int PRICE = productsMade.getInt("price");
String PRODUCT_URL = productsMade.getString("productUrl");
String IMAGE_URL = productsMade.getString("imageUrl");
productList.add(new Product(PRODUCT_ID, STYLE_ID, BRAND_NAME, PRODUCT_NAME,
COLOR_ID, ORIGINAL_PRICE, PERCENT_OFF, PRICE, PRODUCT_URL, IMAGE_URL));
}
} catch (JSONException e){
Log.e("log_tag", "Error converting result" + e.toString());
}
ListView myListView = (ListView)findViewById(R.id.list_view);
adapter = new ListAdapter();
myListView.setAdapter(adapter);
class ListAdapter extends ArrayAdapter<Product>{
ListAdapter() {
super(MainActivity.this, android.R.layout.simple_list_item_1, productList);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
LayoutInflater inflater = getLayoutInflater();
convertView = inflater.inflate(R.layout.list_values, null);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
holder.populateFrom(productList.get(position));
return (convertView);
}
}
class ViewHolder {
public TextView textBrandName = null;
public TextView textProductName = null;
public TextView textOriginalPrice = null;
ViewHolder(View row) {
textBrandName = (TextView)row.findViewById(R.id.brand_name);
textProductName = (TextView)row.findViewById(R.id.product_name);
textOriginalPrice = (TextView)row.findViewById(R.id.original_price);
}
void populateFrom(Product r) {
textBrandName.setText(r.getBrandName());
textProductName.setText(r.getProductName());
textOriginalPrice.setText(r.getOriginalPrice());
}
}
}
Where am I going wrong that I just see a blank page?
My list_value.xml file looks like:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/brand_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/product_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id = "@+id/original_price"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"/>
</LinearLayout>
While my main_activity.xml just has a list view within in.