我尝试在片段中创建listview并从数据库加载数据。但我不知道这个:(我得到了一些红十字架并且不知道如何解决它。我从扩展活动而不是片段得到了自己的酸性代码:( 谁都可以帮帮我?
这是代码
public class FragmentaReviews extends SherlockFragment {
private SimpleAdapter adpt;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.row_main, container,
false);
adpt = new SimpleAdapter(new ArrayList<Review>(), this);
ListView lView = (ListView) rootView.findViewById(R.id.listview);
lView.setAdapter(adpt);
// Exec async load task
(new AsyncListViewLoader()).execute("http://10.0.3.2/text.php");
return rootView;
}
private class AsyncListViewLoader extends AsyncTask<String, Void, List<Review>> {
private final ProgressDialog dialog = new ProgressDialog(getActivity());
@Override
protected void onPostExecute(List<Review> result) {
super.onPostExecute(result);
dialog.dismiss();
adpt.setItemList(result);
adpt.notifyDataSetChanged();
}
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog.setMessage("Downloading Reviews...");
dialog.show();
}
@Override
protected List<Review> doInBackground(String... params) {
List<Review> result = new ArrayList<Review>();
try {
URL u = new URL(params[0]);
HttpURLConnection conn = (HttpURLConnection) u.openConnection();
conn.setRequestMethod("GET");
conn.connect();
InputStream is = conn.getInputStream();
// Read the stream
byte[] b = new byte[1024];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while ( is.read(b) != -1)
baos.write(b);
String JSONResp = new String(baos.toByteArray());
JSONArray arr = new JSONArray(JSONResp);
for (int i=0; i < arr.length(); i++) {
result.add(convertReview(arr.getJSONObject(i)));
}
return result;
}
catch(Throwable t) {
t.printStackTrace();
}
return null;
}
private Review convertReview(JSONObject obj) throws JSONException {
String id = obj.getString("id");
String name = obj.getString("name");
return new Review(id, name);
}
}
}
红十字会
adpt = new SimpleAdapter(new ArrayList<Review>(), this);
adpt.setItemList(result);
这里是适配器
public class SimpleAdapter extends ArrayAdapter<Review> {
private List<Review> itemList;
private Context context;
public SimpleAdapter(List<Review> itemList, Context ctx) {
super(ctx, android.R.layout.simple_list_item_1, itemList);
this.itemList = itemList;
this.context = ctx;
}
public int getCount() {
if (itemList != null)
return itemList.size();
return 0;
}
public Review getItem(int position) {
if (itemList != null)
return itemList.get(position);
return null;
}
public long getItemId(int position) {
if (itemList != null)
return itemList.get(position).hashCode();
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.fragmenta_review, null);
}
Review c = itemList.get(position);
TextView text = (TextView) v.findViewById(R.id.id);
text.setText(c.getid());
TextView text1 = (TextView) v.findViewById(R.id.name);
text1.setText(c.getname());
return v;
}
public List<Review> getItemList() {
return itemList;
}
public void setItemList(List<Review> itemList) {
this.itemList = itemList;
}
}
答案 0 :(得分:0)
好的,SimpleAdapter的构造函数将Context对象作为其第二个参数。您传入的'this'因此不是上下文对象(例如Activity),因此您必须传入上下文对象。 'this'指的是你的FragmentReviews类。您可以使用this.getActivity()作为构造函数的第二个参数。您可能需要将代码更改为以下内容:
Context ctx = (Context)(this.getActivity());
adpt = new SimpleAdapter(new List<Review>(), ctx);
您的构造函数使用List对象,但您使用ArrayList对象调用它。