0

I want to ask, I have 2 classes:

   1. an activity class (to get data from json)
   2. a fragment class (not to do something)

and I want to get data from json of activity via fragment class. Can be combine Activity and Fragment in a class ? and how to do ?

I combined activity and fragment in a Fragment class, I have used a GridView to get data and display JSON, execute the AsyncTask in the this Fragment

This is my code after updated 25/10:

public class FeedBackFragment extends Fragment {

ProgressDialog pDialog;
JSONParser jsonParser = new JSONParser();

MyAdapter adapter;
JSONArray manufacturers = null;

// manufacturers JSON url
private static final String URL_MANUFACTURERS ="MYURL";

public FeedBackFragment() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.feedback_gridview_manufacturer, container, false);

    GridView gridView = (GridView) view.findViewById(R.id.gridview);
    gridView.setAdapter(new MyAdapter(getActivity(), manufacturersList));

    gridView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View view, int arg2, long arg3) {
            // on selecting a single manufacturer
            // CategoryCarActivity will be launched to show category car inside the manufacturer
            Intent i = new Intent(getActivity(), CategoryCarActivity.class);

            // send manufacturer id to activity to get list of cars under that manufacturer
            String manufacturer_id = ((TextView) view.findViewById(R.id.manufacturer_id)).getText().toString();
            i.putExtra("manufacturer_id", manufacturer_id);

            startActivity(i);
        }
    });

    return view;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onActivityCreated(savedInstanceState);

    // manufacturersList = new ArrayList<>();
    new LoadAllManufacturers().execute();
}


class LoadAllManufacturers extends AsyncTask<String, String, String> {
    /**
     * Before starting background thread Show Progress Dialog
     * */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(getActivity());
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }

    /**
     * After completing background task Dismiss the progress dialog
     * **/
    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);

        adapter.notifyDataSetChanged();
        // dismiss the dialog after getting all manufacturers
        if (pDialog.isShowing())
            pDialog.dismiss();
    }
}

private class MyAdapter extends BaseAdapter
{

    // List<POJOManufacturer> listData = null;
    LayoutInflater inflater;
    Context context;

    public MyAdapter(Context context, ArrayList<HashMap<String, String>> arrayList)
    {
        // this.context = context;
        this.manufacturersList = arrayList;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        if (manufacturersList != null)
            return manufacturersList.size();
        return 0;
    }

    @Override
    public Object getItem(int i)
    {
        if (manufacturersList != null)
            return manufacturersList.get(i);
        return null;
    }

    @Override
    public long getItemId(int i)
    {
        if (manufacturersList != null)
            return manufacturersList.get(i).hashCode();
        return 0;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup)
    {
        ViewHolder holder;
        if (convertView == null)
        {
            convertView = inflater.inflate(R.layout.gridview_item, null);
            holder = new ViewHolder();
            holder.name = (TextView) convertView.findViewById(R.id.text);
            holder.iconName = (ImageView) convertView.findViewById(R.id.picture);

            convertView.setTag(holder);
        }
        else {

            holder = (ViewHolder) convertView.getTag();
        }           
        holder.name.setText(this.manufacturersList.get(i).getClass().getName());
        // holder.iconName.setImageResource(this.manufacturersList.get(i).image);

        return convertView;
    }

    public class ViewHolder
    {
        TextView name;
        ImageView iconName;
    }        
   }
 }

I have updated and added: manufacturerList = new ArrayList<>. everything seem is better, and it happen some issues in getView() method, I have try and it's only display with 7 empty items in gridview, and not display content and image

So How fill data from Adapter into Gridview?

luongkhanh
  • 1,753
  • 2
  • 16
  • 30
  • Yes. It is possible. Please show your code and what you've attempted, though – OneCricketeer Oct 16 '16 at 09:26
  • 1
    Though, beware, this sounds like a repost of the previous question you had. http://stackoverflow.com/questions/40066514/implement-activity-in-fragment-android – OneCricketeer Oct 16 '16 at 09:26
  • Hi Cricket_007, I try to explain for you understand my question – luongkhanh Oct 16 '16 at 09:31
  • I will post again my source code here – luongkhanh Oct 16 '16 at 09:33
  • I posted several useful comments on the other post. I'm still suggesting Retrofit. I also suggest downloading the JSON into the Fragment directly rather than passing it between the Activity and the Fragment. – OneCricketeer Oct 16 '16 at 09:34
  • If you have the exact same code... Don't extend ListActivity. Make a GridView in the Activity. Done.... You've combined the layouts. What is the problem with that? – OneCricketeer Oct 16 '16 at 09:36
  • as you have reviewed my source code in: http://stackoverflow.com/questions/40066514/implement-activity-in-fragment-android. My problem is: I want to load all data of ManufacturerActivity and display data in HomeFragment – luongkhanh Oct 16 '16 at 09:48
  • Then move the AsyncTask it to its own file. Execute the AsyncTask in the Fragment, and populate the Adapter. – OneCricketeer Oct 16 '16 at 10:00
  • Ok I will try right now and back to you :) – luongkhanh Oct 16 '16 at 10:06
  • Hi Cricket_007, I have try and it has happened some errors, if I want to convert ManufacturerActivity to ManufacturerFragment, is it ok ? or do you have better solution for me ? – luongkhanh Oct 17 '16 at 00:34
  • You can't convert an Activity to a Fragment. You need an Activity to display a Fragment. – OneCricketeer Oct 17 '16 at 00:39
  • Hi Cricket_007, I have updated my new source code base on your advance, but Im facing an error, I have updated my source code as above – luongkhanh Oct 18 '16 at 05:23

1 Answers1

1

constructor ManufacturerFragment in class ManufacturerFragment cannot be applied to given types;

gridView.setAdapter() takes an adapter, not a Fragment

And new ManufacturerFragment() doesn't accept an Context.

I am not really sure why you think you need to create a new ManufacturerFragment within the Fragment class you already are in. Did you mean to do gridView.setAdapter(new MyAdapter(getActivity()))?

Also, your manufacturersList needs to be loaded into that adapter, so you'll need to figure that out.

And you need to use getActivity() instead of getActivity().getApplicationContext() in most places.

Then, you should only call new LoadAllManufacturers().execute(); in either onCreateView or onActivityCreated, not both. Otherwise, you're running two AsyncTasks.

Then, onPostExecute already runs on the UI thread, no need to use getActivity().runOnUiThread(new Runnable() {...

Once you do figure out how to put that ArrayList into the Adapter class, you'll want to call adapter.notifyDataSetChanged() within onPostExecute to tell the adapter to refresh the data, thereby updating the GridView to display the data.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245