Tôi đã tạo hoạt động xuất dữ liệu json từ danh sách. Nhưng bây giờ tôi muốn thực hiện nó trong một mảnh. Trong đoạn này, tôi muốn xem nó như là GridView. Và cả hai tệp đều hoạt động tốt. nhưng khi tôi cố gắng triển khai AsyncTask, tôi nhận được một số thẻ đỏ dưới dạng mã không thể truy cập được. Xin vui lòng giúp tôi việc này.triển khai AsyncTask trong Fragment android
được sửa đổi: New
public class SalesFragment extends Fragment {
GridView gridView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View gv = inflater.inflate(R.layout.hot_sales, null);
gridView = (GridView) gv.findViewById(R.id.grid_view);
bindGridview();
return gv;
}
public void bindGridview() {
new MyAsyncTask(getActivity(),gridView).execute("");
}
class MyAsyncTask extends AsyncTask<String, String, String> {
GridView mGridView;
Activity mContext;
Response response;
public MyAsyncTask(Activity context,GridView gview) {
this.mGridView=gview;
this.mContext=context;
}
protected String doInBackground(String... params) {
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/sample.txt");
if(file.exists()) {
try{
Reader inputStreamReader = new InputStreamReader(new FileInputStream(file));
Gson gson = new Gson();
this.response = gson.fromJson(inputStreamReader, Response.class);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (@SuppressWarnings("hiding") IOException e){
e.printStackTrace();
}
}else{
System.out.println("Error");
}
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
//List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();
for(Sales sales : this.response.sales){
HashMap<String, String> hm = new HashMap<String,String>();
if (sales.getCategories1().contains("12")){
//hm.put("sale_title", "" + sales.getShort_title());
for(Shop shop : this.response.shops){
String image_file = new String(Environment.getExternalStorageDirectory().getAbsolutePath()
+ "/images/" + shop.getImage());
if(shop.getId().equals(sales.getShop_id())){
hm.put("shop_image", image_file);
System.out.println(shop_image);
}
}
if(hm.size()>0){
gridView.setAdapter(new ImageAdapter(MainActivity.this,R.layout.grid_layout , imgArray, titleArray));
}
}
}
}
}
}
Làm thế nào để gọi hình ảnh vào GridView trên hình trên? Hãy giúp tôi.
Có lẽ đăng một số mã? Bất kỳ số lượng các vấn đề có thể gây ra điều này. – blaffie
đăng mã được triển khai của bạn tại đây ............ – Piyush
@ Piyush Gupta & blaffie: mã được thêm ngay bây giờ. Tôi muốn làm điều tương tự như hoạt động trong phân đoạn. – Kabe