简体   繁体   English

我将图像附加到正在从服务器下载的回收器视图中,但是回收器视图未显示任何图像

[英]I am attaching a image to a recycler view which is being downloaded from server, but recycler view is not showing any image

The recycler view is showing other details in activity, except the image. 回收者视图显示活动中的其他详细信息,除了图像。 I am loading an image through AsyncTask in getImage() . 我正在通过getImage() AsyncTask加载图像。 What am I doing wrong? 我究竟做错了什么?

Here's my code: 这是我的代码:

MainActivity.java MainActivity.java

int i;
Bitmap bitmap,imageOfPoday;
RecyclerView rvPoday;
ProductAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    NavigationDrawerFragment drawerFragment = (NavigationDrawerFragment)getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
    drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), toolbar);

    pd = ProgressDialog.show(MainActivity.this, "Please Wait...", null, true, true);
    callPoday(PODAY_URL);
}

public List<Product> getDataPoday() {
    List<Product> data=new ArrayList<>();
    for(i = 0; i < parsedPODAY_ID.size(); i++) {
        Product current=new Product();
        current.productName=parsedPODAY_NAME.get(i);
        current.price=parsedPODAY_OFFPRICE.get(i);
        current.pImage=getImage("http://www.tontosworld.com/img/productimages/" + parsedPODAY_P_IMG.get(i));
        Toast.makeText(this,current.pImage+"",Toast.LENGTH_LONG).show();
        data.add(current);
    }
    return data;

}

public Bitmap getImage(String s) {
    LoadImageTask task=new LoadImageTask();
    task.execute(s);
    return imageOfPoday;
}

class LoadImageTask extends AsyncTask<String,String,Bitmap> {
    @Override
    protected Bitmap doInBackground(String... strings) {
        try {
            bitmap = BitmapFactory.decodeStream((InputStream) new URL(strings[0]).getContent());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bitmap;
    }

    @Override
    protected void onPostExecute(Bitmap b) {
        super.onPostExecute(b);
        if(b == null) {
            Toast.makeText(MainActivity.this,"Error in loading image",Toast.LENGTH_LONG).show();
        }
        else {
            imageOfPoday=b;
            Toast.makeText(MainActivity.this,"This is for image"+imageOfPoday,Toast.LENGTH_LONG).show();
        }
    }
}

ProductAdapter.java ProductAdapter.java

public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.MyViewHolder> {
    public LayoutInflater inflater;
    List<Product> data= Collections.emptyList();

    public ProductAdapter(Context context,List<Product> data){
        inflater = LayoutInflater.from(context);
        this.data = data;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = inflater.inflate(R.layout.recycler_view_single,parent,false);
        MyViewHolder holder = new MyViewHolder(v);
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        Product current = data.get(position);
        holder.name.setText(current.productName);
        holder.price.setText(current.price);
        holder.image.setImageBitmap(current.pImage);
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        TextView name,price;
        ImageView image;

        public MyViewHolder(View itemView) {
            super(itemView);
            name = (TextView)itemView.findViewById(R.id.rvItemName);
            price = (TextView)itemView.findViewById(R.id.rvItemPrice);
            image = (ImageView)itemView.findViewById(R.id.rvItemImage);
        }
    }
}

Product.java 产品.java

public class Product {
    String productName, price;
    Bitmap pImage;
}

recycler_view_single.xml recycler_view_single.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <android.support.v7.widget.CardView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="?android:selectableItemBackground">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/rvItemImage"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:src="@mipmap/ic_launcher" />

            <TextView
                android:id="@+id/rvItemName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Name" />

            <TextView
                android:id="@+id/rvItemPrice"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Price" />

        </LinearLayout>
    </android.support.v7.widget.CardView>
</LinearLayout>

activity_main.xml activity_main.xml

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/drawer_layout">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:orientation="vertical">

        <include
            android:id="@+id/app_bar"
            layout="@layout/app_bar"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Product of the day"
            android:textSize="25sp"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            android:textColor="#000"/>

        <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/rvProductOfTheDay">
        </android.support.v7.widget.RecyclerView>
    </LinearLayout>

    <fragment
        android:id="@+id/fragment_navigation_drawer"
        android:layout_width="280dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:layout="@layout/fragment_navigation_drawer"
        android:name="com.tontosworld.tontosworld.NavigationDrawerFragment">
    </fragment>

</android.support.v4.widget.DrawerLayout>

Thanks for your help. 谢谢你的帮助。

You are missing the update notice for your adapter (which displays the data). 您缺少适配器的更新通知(该适配器显示数据)。

Everytime you update your List, you need to call 每次更新列表时,您都需要致电

adapter.notifyDataSetChanged();
//if you only change one item, and use recyclerview, call:
adapter.notifyItemChanged(); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM