复制imageadapter到片段后,图像不显示 [英] after copying the imageadapter to fragment, image is not displaying

查看:110
本文介绍了复制imageadapter到片段后,图像不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不显示在列表适配器的图片,你能帮帮我吗?

在我有这个最后的消息日志猫,这意味着我得到了HTTP结果,但该适配器不工作

  www.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
 我/的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
 我/的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
 我/的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
 我/的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
 我/的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
 

这里是code得到JSON

 的JSONObject jsonObj =新的JSONObject(myJSON);
            人民= jsonObj.getJSONArray(结果);
            列表项=新的ArrayList<列表项>();
            的for(int i = 0; I< peoples.length();我++){
                JSONObject的C = peoples.getJSONObject(我);
                字符串ID = c.getString(ID);
                字符串URL = c.getString(路径);
                Listitem.add(新列表项(ID,网址));
                Log.e(D,PPPPP);
                的System.out.println(列表项);
            }
            如果(mListener!= NULL)
                  mListener.myMethod(列表项);
 

下面是code的片段,但adpater它不工作

公共无效downloadImage(){     // OkHttpHandler处理器=新OkHttpHandler();

  // handler.execute();
字符串图像= NULL;

尝试 {
    OkHttpHandler处理器=新OkHttpHandler(getActivity()
            新OkHttpHandler.MyInterface(){
                @覆盖
                公共无效在myMethod(ArrayList的结果){
                    Toast.makeText(getActivity(),连接成功的,
                            Toast.LENGTH_LONG).show();
                    GridViewAdapter适配器=新GridViewAdapter(getActivity(),R.layout.grid_item_layout,结果);

                    adapter.notifyDataSetChanged();
                    list.setAdapter(适配器);
                }
            });

   。图像= handler.execute()获得();
    Log.d(E,HHHH处理程序);
 

这是适配器

 公共类GridViewAdapter扩展ArrayAdapter<列表项> {

    私人语境mcontext;
    私人诠释layoutResourceId;

    公共GridViewAdapter(上下文的背景下,INT layoutResourceId,ArrayList的<列表项>列表项){
        超(背景下,layoutResourceId,列表项);
        this.layoutResourceId = layoutResourceId;
        this.mcontext =背景;

    }



    @覆盖
    公共查看getView(INT位置,查看convertView,ViewGroup中父){
        查看排= convertView;
        ViewHolder持有人;

        如果(行== NULL){
            LayoutInflater充气= LayoutInflater.from(mcontext);
            行= inflater.inflate(layoutResourceId,父母,假);
            持有人=新ViewHolder();
            holder.imageTitle =(TextView中)row.findViewById(R.id.text);
            holder.imageView =(ImageView的)row.findViewById(R.id.imageView);
            row.setTag(保持器);
        } 其他 {
            支架=(ViewHolder)row.getTag();
        }
        列表项的项目=的getItem(位置);
        的System.out.println(item.getUrl());

        holder.imageTitle.setText(item.getId());
        毕加索。
                与(mcontext)。
                负载(item.getUrl())
                .placeholder(R.drawable.logo)
                。适合()
                。走进(holder.imageView);

        返回行;
    }

    静态类ViewHolder {
        TextView的imageTitle;
        ImageView的ImageView的;
    }
 

这是适配器的XML

 < RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_marginTop =5DP

    机器人:方向=垂直
    机器人:填充=5DP>

    < ImageView的
        机器人:ID =@ + ID / ImageView的
        机器人:layout_width =300dp
        机器人:layout_height =300dp
        />

    <的TextView
        机器人:ID =@ + ID /文
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_marginTop =5DP
        机器人:重力=中心
        机器人:MAXLINES =2
        机器人:layout_below =@ + ID / grid_item_image
        机器人:ellipsize =金字招牌
        机器人:TEXTSIZE =12SP/>

< / RelativeLayout的>
 

由于它的片段,我initilaise名单这里

  @覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
                         捆绑savedInstanceState){
    //充气的布局该片段
    最终的视图V = inflater.inflate(R.layout.latestjokesfrag,集装箱,假);
    名单=(ListView控件)v.findViewById(R.id.ListView);
    返回inflater.inflate(R.layout.latestjokesfrag,集装箱,假);
}
 

我说这

 新OkHttpHandler.MyInterface(){
                        @覆盖
                        公共无效在myMethod(ArrayList的结果){
                            的System.out.println(结果);
 

这是结果

  www.i我/的System.out:结果
10-24 15:22:35.000 31426-31426 / com.justedhak.www.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,融为一体。 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

 公共类LatestJokesFrag扩展片段{
    ListView控件列表;

    公共LatestJokesFrag(){
        //要求空公共构造

    }
    公共无效onActivityCreated(@Nullable捆绑savedInstanceState){
        super.onActivityCreated(savedInstanceState);
        downloadImage();
    }
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);

    }

    公共无效downloadImage(){
        // OkHttpHandler处理器=新OkHttpHandler();


        //handler.execute();
        字符串图像= NULL;

        尝试 {
            OkHttpHandler处理器=新OkHttpHandler(getActivity()
                    新OkHttpHandler.MyInterface(){
                        @覆盖
                        公共无效在myMethod(ArrayList的结果){
                            的System.out.println(结果);

                            的System.out.println(结果);
                            Toast.makeText(getActivity(),连接成功的,
                                    Toast.LENGTH_LONG).show();
                            GridViewAdapter适配器=新GridViewAdapter(getActivity(),R.layout.grid_item_layout,结果);

                            adapter.notifyDataSetChanged();
                            list.setAdapter(适配器);
                        }
                    });

           。图像= handler.execute()获得();
            Log.d(E,HHHH处理程序);
           // System.out.print(图像);
            //如果(图像= NULL和放大器;!&安培; image.length> 0){
            // Log.isLoggable(E,image.length);
            //Log.d("e,DDDDDDD进入试);
            //位图位= BitmapFactory.de codeByteArray(图像,0,image.length);
            // imageView.setImageBitmap(位);
            // txtBytes.setText(下载的总字节数:+ image.length);


        }赶上(例外五){
            Log.d(E,RRRRRR错误);

            e.printStackTrace();
// txtBytes.setText(嗯不好意思,出事了!);
        }

    }

    @覆盖
    公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
                             捆绑savedInstanceState){
        //充气的布局该片段
        最终的视图V = inflater.inflate(R.layout.latestjokesfrag,集装箱,假);
        名单=(ListView控件)v.findViewById(R.id.ListView);
        返回inflater.inflate(R.layout.latestjokesfrag,集装箱,假);
    }
}
 

解决方案

您code中的问题。您致电downloadMethod内片段的onCreate(),但你应该这样做在里面 onActivityCreated()作为源说code

  onCreated()的调用,而该片段的活动
 *尚处于创建的过程。因此,你可以不依赖
 *在搞什么活动的内容视图层次结构正在初始化
 * 在此刻。如果你想要做的工作,一旦活动本身是
 *创建,见{@link #onActivityCreated(包)}。
 

所以,在这里我们去

 公共无效onActivityCreated(@Nullable捆绑savedInstanceState){
    super.onActivityCreated(savedInstanceState);
     downloadImage();
}
 

你也取得了 onCreateView 的错误:它应该返回的视图中,虚增

例如它看起来应该如下:

  @覆盖
    公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
                             捆绑savedInstanceState){
        //充气的布局该片段
        最终的视图V = inflater.inflate(R.layout.latestjokesfrag,集装箱,假);
        名单=(ListView控件)v.findViewById(R.id.ListView);

        返回伏;
    }
 

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);
    }
}

解决方案

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;
    }

这篇关于复制imageadapter到片段后,图像不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆