Question

i am not able to populate the imageview textview inside gridview using fragments.

It is showing blank intead of gridview populating in my project can please anyone see the code below and let me know what i have to change

And the populating the image and text is from the mysql database dynamically

public class StoreHomeFragment extends Fragment {
final ArrayList<HashMap<String, String>> MyArrList = new ArrayList<HashMap<String, String>>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.store_home, container, false);
    final GridView gridView1 = (GridView)rootView.findViewById(R.id.store_home_gridview); 
    gridView1.setAdapter(new ImageAdapter(rootView.getContext(), MyArrList));
    return rootView;
}

//Activity is created

@Override

 public void onActivityCreated(Bundle savedInstanceState) {
     super.onActivityCreated(savedInstanceState);

     String url = "http://192.168.1.132/Android/App/good.php"; //url where i am using select query and retrieving it from database

        try {
            JSONArray data = new JSONArray(getJSONUrl(url));

            HashMap<String, String> map;

            for(int i = 0; i < data.length(); i++){
                JSONObject c = data.getJSONObject(i);//retrieving from db
                map = new HashMap<String, String>();
                map.put("name", c.getString("name"));
                map.put("artist", c.getString("artist"));
                map.put("price", c.getString("price"));
                map.put("image", c.getString("image"));


                MyArrList.add(map);
            }           

            //gridView1.setAdapter(new ImageAdapter(this,MyArrList));

        } catch (JSONException e) {

            e.printStackTrace();
        } catch (Exception e) {

            e.printStackTrace();
        }

    }      

//I have used imageAdapter

class ImageAdapter extends BaseAdapter 

{
    private Context context;
    public ImageView imageView;
    private ArrayList<HashMap<String, String>> MyArr = new ArrayList<HashMap<String, String>>();

    public ImageAdapter(Context c,ArrayList<HashMap<String, String>> list) 
    {   
        context = c;

        MyArr = list;
    }

    public int getCount() {

        return MyArr.size();
    }

    public Object getItem(int position) {

        return position;
    }

    public long getItemId(int position) {

        return position;
    }
    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.store_home_gridview_row, null); 
        }

        TextView tv_title = (TextView) convertView.findViewById(R.id.textview_name);
        tv_title.setText("Title:"+MyArr.get(position).get("name"));

        TextView tv_artist = (TextView) convertView.findViewById(R.id.textview_artist);
        tv_artist.setText("Artist:"+MyArr.get(position).get("artist"));

        TextView tv_duration = (TextView) convertView.findViewById(R.id.textview_price);
        tv_duration.setText("Price:"+MyArr.get(position).get("price"));

        String abc = (MyArr.get(position).get("image"));
        String abcd = "http://192.168.1.132/images/products/"+abc;

        imageView = (ImageView) convertView.findViewById(R.id.imageView1);
        try
        {
            URL url3 = null;
            try {
                url3 = new URL(abcd);
                } catch (Exception e) {

                e.printStackTrace();
            }
            Bitmap bmp = BitmapFactory.decodeStream(url3.openConnection().getInputStream()); //image is populated 
            imageView.setImageBitmap(bmp);
        }
        catch(Exception par)
        {
            imageView.setImageResource(android.R.drawable.ic_menu_report_image);
        }
        return convertView;         
    }

} 



    /*** Get JSON Code from URL ***/
    public String getJSONUrl(String url) {
        StringBuilder str = new StringBuilder();
        HttpClient client = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);
        try 
        {
            HttpResponse response = client.execute(httpGet);
            StatusLine statusLine = response.getStatusLine();
            int statusCode = statusLine.getStatusCode();
            System.out.println ( "status Code : " + statusCode );
            if (statusCode == 200) 
            { 
                HttpEntity entity = response.getEntity();
                InputStream content = entity.getContent();
                BufferedReader reader = new BufferedReader(new InputStreamReader(content));
                String line;
                while ((line = reader.readLine()) != null) 
                {
                    str.append(line);
                }
            }   
            else 
            {
                Log.e("Log", "Failed to download file..");
            }
        } 
        catch (ClientProtocolException e) 
        {
            e.printStackTrace();
        } 
        catch (IOException e) 
        {
            e.printStackTrace();
        }
        System.out.println ( "str : " + str.toString() );
        return str.toString();
    }


 }
Was it helpful?

Solution

Follow the steps Hope this helps you

1) Remove these lines from onCreateView() method

final GridView gridView1 = (GridView)rootView.findViewById(R.id.store_home_gridview); 
    gridView1.setAdapter(new ImageAdapter(rootView.getContext(), MyArrList));

2)Modify onActivityCreated() as follow

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    new AsyncTask<Void, Void, Void>() {

        @Override
        protected Void doInBackground(Void... params) {
            String url = "http://192.168.1.132/Android/App/good.php";

            try {
                JSONArray data = new JSONArray(getJSONUrl(url));

                HashMap<String, String> map;

                for (int i = 0; i < data.length(); i++) {
                    JSONObject c = data.getJSONObject(i);

                    map = new HashMap<String, String>();
                    map.put("name", c.getString("name"));
                    map.put("artist", c.getString("artist"));
                    map.put("price", c.getString("price"));
                    map.put("image", c.getString("image"));

                    MyArrList.add(map);
                }

            } catch (JSONException e) {

                e.printStackTrace();
            } catch (Exception e) {

                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            final GridView gridView1 = (GridView) rootView.findViewById(R.id.store_home_gridview);
            gridView1.setAdapter(new ImageAdapter(getActivity(), MyArrList));

        }
    }.execute();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top