Here is my code :
public class Fragment1 extends SherlockFragment {
ListView lista;
ArrayList<String> items = new ArrayList<String>();
private ArrayList<MyListAdapter> thelista = new ArrayList<MyListAdapter>();
View rootView;
ListView list;
TextView title, price;
MyListAdapter adapter;
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// TODO Auto-generated method stub
super.onCreateOptionsMenu(menu, inflater);
MenuInflater blowup = this.getSherlockActivity()
.getSupportMenuInflater();
blowup.inflate(R.menu.menuxml, menu);
return;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.main, container, false);
ActionBar ab = this.getSherlockActivity().getSupportActionBar();
ab.setTitle("");
title = (TextView) rootView.findViewById(R.id.title);
price = (TextView) rootView.findViewById(R.id.details);
list = (ListView) rootView.findViewById(R.id.list);
SpinnerAdapter mSpinnerAdapter = ArrayAdapter.createFromResource(
this.getSherlockActivity(), R.array.phones_array,
android.R.layout.simple_spinner_dropdown_item);
ab.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
ab.setListNavigationCallbacks(mSpinnerAdapter, null);
StrictMode.enableDefaults();
getData();
return rootView;
}
public void getData() {
String result = "";
InputStream isr = null;
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpost = new HttpPost(
"http://10.0.2.2/AndroidArabia/android3arabiya.php");
HttpResponse resposne = httpclient.execute(httpost);
HttpEntity entity = resposne.getEntity();
isr = entity.getContent();
} catch (Exception e) {
Log.e("log_tag", "error in http connection" + e.toString());
Toast.makeText(getSherlockActivity(),
"Couldn't Connect To The Internet", Toast.LENGTH_LONG)
.show();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
isr, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
isr.close();
result = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting Result " + e.toString());
}
try {
JSONArray jArray = new JSONArray(result);
for (int i = 0; i < jArray.length(); i++) {
JSONObject json = jArray.getJSONObject(i);
thelista.add(new MyListAdapter(json.getString("PhoneName"),
json.getString("PhonePrice")));
}
list.setAdapter((ListAdapter) thelista);
} catch (Exception e) {
Log.e("lag_tag", "ERROR PARSING DATA" + e.toString());
Toast.makeText(getSherlockActivity(),
"Couldn't Connect To The Internet", Toast.LENGTH_LONG)
.show();
}
MyListAdapter Code :
public class MyListAdapter extends BaseAdapter {
private String[] data;
private String[] data2;
private Context context;
public MyListAdapter(String string, String string2) {
// TODO Auto-generated constructor stub
}
@Override
public int getCount() {
return data.length;
}
@Override
public Object getItem(int position) {
return data[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = LayoutInflater.from(context).inflate(R.layout.pricelist,
parent, false);
TextView text1 = (TextView) rowView.findViewById(R.id.title);
TextView text2 = (TextView) rowView.findViewById(R.id.details);
text1.setText(data[position]);
text2.setText(data2[position]);
return rowView;
}
}
}
price list xml :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dip" >
<LinearLayout
android:id="@+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginRight="5dip"
android:background="@drawable/image_bg"
android:padding="3dip" >
<ImageView
android:id="@+id/list_image"
android:layout_width="50dip"
android:layout_height="50dip" />
</LinearLayout>
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/thumbnail"
android:layout_toRightOf="@+id/thumbnail"
android:text="Smartphone Name"
android:textColor="#040404"
android:textSize="15dip"
android:textStyle="bold"
android:typeface="sans" />
<TextView
android:id="@+id/details"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/title"
android:layout_marginTop="1dip"
android:layout_toRightOf="@+id/thumbnail"
android:text="Smartphone Price"
android:textColor="#343434"
android:textSize="10dip" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="@drawable/rating" />
</RelativeLayout>
And Finally main xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#b5b5b5"
android:dividerHeight="1dp" >
</ListView>
</LinearLayout>
I'm not an expert in custom layout but i'm getting an error in parsing the JSON Array into the custom listview. Of course my main goal is to fill the custom listview with the JSON array but i don't have any clue how to do it and non of question asked before have helped me. Can anyone help me right here? Here all the info, the program is stopping at the JSon catch exception and giving me the toastBox : Couldn't connect to the internet. any suggestion? :)