在片段关闭应用程序中调用BaseAdapter

时间:2015-11-18 13:45:41

标签: android fragment baseadapter

在片段关闭应用程序中调用BaseAdapter

注释行spinner.setAdapter(new Category_Adapter(getActivity(),categorylist));

工作 错误记录

enter image description here

Class FragmentNews

public class FragmentNews extends Fragment {
ArrayList<Category> categorylist = new ArrayList<Category>();

@Override
public View onCreateView(final LayoutInflater inflater,
        ViewGroup container, Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    final View rootView = inflater.inflate(R.layout.fragment_news,
            container, false);
    Spinner spinner = (Spinner) rootView.findViewById(R.id.category);
    new fechPosts().execute("");


    return rootView;
}
class fechPosts extends AsyncTask<String, String, String> {

    @Override
    protected void onPreExecute() {
        ringProgressDialog = new ProgressDialog(getActivity());
        ringProgressDialog.setMessage("در حال ارسال پیام");
        ringProgressDialog.setCancelable(true);
        ringProgressDialog.show();
        super.onPreExecute();
    }

    @Override
    protected String doInBackground(String... params) {

        String result = fetch(params[0]);
        return result;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        Activity myActivity = getActivity();
        spinner.setAdapter(new Category_Adapter(getActivity(), categorylist));

        ringProgressDialog.dismiss();

    }

}
public String fetch(String titel) {
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpGet httppost = null;
    httppost = new HttpGet(
            "http://mynikan.ir/paliz/mobile/GetAllProduct.php");
    String r = "ok";
    String result = null;
    InputStream inputStream = null;
    try {
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        inputStream = response.getEntity().getContent();
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                inputStream, "iso-8859-1"));
        StringBuilder sb = new StringBuilder();
        sb.append(reader.readLine() + "\n");
        String line = "";
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        result = sb.toString();

        JSONArray array = null;
        try {
            array = new JSONArray(result);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        if (array.length() != 0) {
            for (int i = 0; i < array.length(); i++) {
                JSONObject json_data;
                try {
                    json_data = array.getJSONObject(i);
                    Category obj = new Category();
                    obj.Image = json_data.getString("Product_Image");
                    obj.Title = json_data.getString("Price");
                    categorylist.add(obj);

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        } else {}

    } catch (ClientProtocolException e) {

    } catch (IOException e) {

    }
    return r;
}

类适配器

public class Category_Adapter extends BaseAdapter {
public String[] items;
public LayoutInflater myinflater;
Context context;
static class ViewHolder
 {
  TextView text;
  TextView price;
  ImageView im;
  }
public int[] picmenu;
ArrayList<Category> categorylist = new ArrayList<Category>();
public Category_Adapter(Context c, ArrayList<Category> pthemop) {
    myinflater = LayoutInflater.from(c);
    context = c;

    categorylist = pthemop;

}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return categorylist.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return position;
}
@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
    final ViewHolder holder;
    // /////
    if (convertView == null) {
        convertView = myinflater.inflate(R.layout.snipper_single, null);
        holder = new ViewHolder();
        holder.text = (TextView) convertView.findViewById(R.id.text);
        holder.im = (ImageView) convertView.findViewById(R.id.image);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.text.setText(categorylist.get(position).Title);
    Picasso.with(context).load(categorylist.get(position).Image)
            .into(holder.im);
    return convertView;
}}

班级类别

public class Category {
String Title;
String Image;

}

1 个答案:

答案 0 :(得分:0)

下面:

spinner.setAdapter(new Category_Adapter(getActivity(), categorylist));

导致问题的行,因为Spinner的spinner对象为null

onCreateView方法中创建新对象,而不是初始化在spinner中使用的对象。

onCreateView方法更改为:

spinner = (Spinner) rootView.findViewById(R.id.category);
 new fechPosts().execute("");