这是我的代码:
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 代码:
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;
}
}
}
价目表 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>
最后是 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>
我不是自定义布局方面的专家,但在将 JSON 数组解析为自定义列表视图时出错。当然,我的主要目标是用 JSON 数组填充自定义列表视图,但我不知道如何做到这一点,而且之前提出的问题也没有帮助我。任何人都可以在这里帮助我吗?这里有所有信息,程序在 JSon 捕获异常处停止并给我 toastBox :无法连接到互联网。有什么建议吗?:)