Moudiz
Moudiz

Reputation: 7377

after copying the imageadapter to fragment, image is not displaying

the Image not showing in the List adapter can you help me please?

in the log cat I am having this last message , it means I am getting the http result, however the adapter is not working

www.i I/System.out﹕ [com.justedhak.www.i.Listitem@2c7c0f18, com.justedhak.www.i.Listitem@1fbed71, com.justedhak.www.i.Listitem@3102b756, com.justedhak.www.i.Listitem@148873d7, com.justedhak.www.i.Listitem@af04c4, com.justedhak.www.i.Listitem@1190efad]
 E/d﹕ ppppp
 I/System.out﹕ [com.justedhak.www.i.Listitem@2c7c0f18, com.justedhak.www.i.Listitem@1fbed71, com.justedhak.www.i.Listitem@3102b756, com.justedhak.www.i.Listitem@148873d7, com.justedhak.www.i.Listitem@af04c4, com.justedhak.www.i.Listitem@1190efad, com.justedhak.www.i.Listitem@308af2e2]
 E/d﹕ ppppp
 I/System.out﹕ [com.justedhak.www.i.Listitem@2c7c0f18, com.justedhak.www.i.Listitem@1fbed71, com.justedhak.www.i.Listitem@3102b756, com.justedhak.www.i.Listitem@148873d7, com.justedhak.www.i.Listitem@af04c4, com.justedhak.www.i.Listitem@1190efad, com.justedhak.www.i.Listitem@308af2e2, com.justedhak.www.i.Listitem@3df67673]
 E/d﹕ ppppp
 I/System.out﹕ [com.justedhak.www.i.Listitem@2c7c0f18, com.justedhak.www.i.Listitem@1fbed71, com.justedhak.www.i.Listitem@3102b756, com.justedhak.www.i.Listitem@148873d7, com.justedhak.www.i.Listitem@af04c4, com.justedhak.www.i.Listitem@1190efad, com.justedhak.www.i.Listitem@308af2e2, com.justedhak.www.i.Listitem@3df67673, com.justedhak.www.i.Listitem@9f9a930]
 E/d﹕ ppppp
 I/System.out﹕ [com.justedhak.www.i.Listitem@2c7c0f18, com.justedhak.www.i.Listitem@1fbed71, com.justedhak.www.i.Listitem@3102b756, com.justedhak.www.i.Listitem@148873d7, com.justedhak.www.i.Listitem@af04c4, com.justedhak.www.i.Listitem@1190efad, com.justedhak.www.i.Listitem@308af2e2, com.justedhak.www.i.Listitem@3df67673, com.justedhak.www.i.Listitem@9f9a930, com.justedhak.www.i.Listitem@1bf9d9a9]
 E/d﹕ ppppp
 I/System.out﹕ [com.justedhak.www.i.Listitem@2c7c0f18, com.justedhak.www.i.Listitem@1fbed71, com.justedhak.www.i.Listitem@3102b756, com.justedhak.www.i.Listitem@148873d7, com.justedhak.www.i.Listitem@af04c4, com.justedhak.www.i.Listitem@1190efad, com.justedhak.www.i.Listitem@308af2e2, com.justedhak.www.i.Listitem@3df67673, com.justedhak.www.i.Listitem@9f9a930, com.justedhak.www.i.Listitem@1bf9d9a9, com.justedhak.www.i.Listitem@b695b2e]
 E/d﹕ ppppp

here is the code getting the json

  JSONObject jsonObj = new JSONObject(myJSON);
            peoples = jsonObj.getJSONArray("result");
            Listitem = new ArrayList<Listitem>();
            for(int i=0;i<peoples.length();i++){
                JSONObject c = peoples.getJSONObject(i);
                String id = c.getString("id");
                String url = c.getString("path");
                Listitem.add(new Listitem(id,url));
                Log.e("d", "ppppp");
                System.out.println(Listitem);
            }
            if (mListener != null)
                  mListener.myMethod(Listitem);

here is code in fragment , but the adpater its not working at all

public void downloadImage() { // OkHttpHandler handler = new OkHttpHandler();

//handler.execute();
String image = null;

try {
    OkHttpHandler handler = new OkHttpHandler(getActivity(),
            new OkHttpHandler.MyInterface() {
                @Override
                public void myMethod(ArrayList result) {
                    Toast.makeText(getActivity(), "Connection Succesful",
                            Toast.LENGTH_LONG).show();
                    GridViewAdapter adapter = new GridViewAdapter(getActivity(), R.layout.grid_item_layout, result);

                    adapter.notifyDataSetChanged();
                    list.setAdapter(adapter);
                }
            });

   image = handler.execute().get();
    Log.d("e", "hhhh handler");

this is the adapter

public class GridViewAdapter extends ArrayAdapter<Listitem> {

    private Context mcontext;
    private int layoutResourceId;

    public GridViewAdapter(Context context, int layoutResourceId, ArrayList<Listitem> listitem) {
        super(context, layoutResourceId, listitem);
        this.layoutResourceId = layoutResourceId;
        this.mcontext =context;

    }



    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        ViewHolder holder;

        if (row == null) {
            LayoutInflater inflater = LayoutInflater.from(mcontext);
            row = inflater.inflate(layoutResourceId, parent, false);
            holder = new ViewHolder();
            holder.imageTitle = (TextView) row.findViewById(R.id.text);
            holder.imageView = (ImageView) row.findViewById(R.id.imageView);
            row.setTag(holder);
        } else {
            holder = (ViewHolder) row.getTag();
        }
        Listitem item = getItem(position);
        System.out.println(item.getUrl());

        holder.imageTitle.setText(item.getId());
        Picasso.
                with(mcontext).
                load(item.getUrl())
                .placeholder(R.drawable.logo)
                .fit()
                .into(holder.imageView);

        return row;
    }

    static class ViewHolder {
        TextView imageTitle;
        ImageView imageView;
    }

this is the xml of the adapter

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp"

    android:orientation="vertical"
    android:padding="5dp">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="300dp"
        android:layout_height="300dp"
        />

    <TextView
        android:id="@+id/text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:gravity="center"
        android:maxLines="2"
        android:layout_below="@+id/grid_item_image"
        android:ellipsize="marquee"
        android:textSize="12sp" />

</RelativeLayout>

because its fragment , I initilaise the list here

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    final View v = inflater.inflate(R.layout.latestjokesfrag, container, false);
    list = (ListView) v.findViewById(R.id.ListView);
    return inflater.inflate(R.layout.latestjokesfrag, container, false);
}

i added this

 new OkHttpHandler.MyInterface() {
                        @Override
                        public void myMethod(ArrayList result) {
                            System.out.println("result");

this is result

www.i I/System.out﹕ result
10-24 15:22:35.000  31426-31426/com.justedhak.www.i I/System.out﹕ [com.justedhak.www.i.Listitem@34435c8a, com.justedhak.www.i.Listitem@239ca2fb, com.justedhak.www.i.Listitem@2c7c0f18, com.justedhak.www.i.Listitem@1fbed71, com.justedhak.www.i.Listitem@3102b756, com.justedhak.www.i.Listitem@148873d7, com.justedhak.www.i.Listitem@af04c4, com.justedhak.www.i.Listitem@1190efad, com.justedhak.www.i.Listitem@308af2e2, com.justedhak.www.i.Listitem@3df67673, com.justedhak.www.i.Listitem@9f9a930, com.justedhak.www.i.Listitem@1bf9d9a9]

fragmetn.xml

public class LatestJokesFrag extends Fragment{
    ListView list;

    public LatestJokesFrag() {
        // Required empty public constructor

    }
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        downloadImage();
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    public void downloadImage() {
        // OkHttpHandler handler = new OkHttpHandler();


        //handler.execute();
        String image = null;

        try {
            OkHttpHandler handler = new OkHttpHandler(getActivity(),
                    new OkHttpHandler.MyInterface() {
                        @Override
                        public void myMethod(ArrayList result) {
                            System.out.println("result");

                            System.out.println(result);
                            Toast.makeText(getActivity(), "Connection Succesful",
                                    Toast.LENGTH_LONG).show();
                            GridViewAdapter adapter = new GridViewAdapter(getActivity(), R.layout.grid_item_layout, result);

                            adapter.notifyDataSetChanged();
                            list.setAdapter(adapter);
                        }
                    });

           image = handler.execute().get();
            Log.d("e", "hhhh handler");
           // System.out.print(image);
            //if (image != null && image.length > 0){
            //  Log.isLoggable("e",image.length);
            //Log.d("e", "ddddddd entered the try");
            //      Bitmap bitmap = BitmapFactory.decodeByteArray(image, 0, image.length);
            //    imageView.setImageBitmap(bitmap);
            //              txtBytes.setText("Total bytes downloaded: " + image.length);


        } catch (Exception e) {
            Log.d("e", "rrrrrr error");

            e.printStackTrace();
//            txtBytes.setText("Hmm sorry, something went wrong!");
        }

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        final View v = inflater.inflate(R.layout.latestjokesfrag, container, false);
        list = (ListView) v.findViewById(R.id.ListView);
        return inflater.inflate(R.layout.latestjokesfrag, container, false);
    }
}

Upvotes: 0

Views: 78

Answers (1)

ProblemSlover
ProblemSlover

Reputation: 2537

The problem with your code. that you call downloadMethod inside Fragment onCreate() but you should do it inside the onActivityCreated() as stated in the source code

  onCreated() can be called while the fragment's activity is
 * still in the process of being created.  As such, you can not rely
 * on things like the activity's content view hierarchy being initialized
 * at this point.  If you want to do work once the activity itself is
 * created, see {@link #onActivityCreated(Bundle)}.

So here we go

public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
     downloadImage();
}

Also You made a mistake in onCreateView : It should return the view you inflated

E.g it should look like as follows

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        final View v = inflater.inflate(R.layout.latestjokesfrag, container, false);
        list = (ListView) v.findViewById(R.id.ListView);

        return v;
    }

Upvotes: 2

Related Questions