簡體   English   中英

從適配器在RecyclerView中設置圖像

[英]Set image inside RecyclerView from Adapter

因此,我在RecyclerView中有一個狀態列表,這可以正常工作。 但是我想從資產文件夾中將圖像添加到文本的左側,該圖像與res文件夾處於同一級別,而不是子文件夾,並且不起作用。

我的布局文件如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="5dp"
android:paddingTop="5dp">

<ImageView
    android:id="@+id/state_Icon_View"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_centerVertical="true"
    tools:ignore="HardcodedText" />

<TextView
    android:id="@+id/state_Item"
    android:layout_width="wrap_content"
    android:layout_height="50dp"
    android:layout_centerVertical="true"
    android:layout_marginLeft="@dimen/activity_horizontal_margin"
    android:layout_marginStart="@dimen/activity_horizontal_margin"
    android:layout_toEndOf="@+id/state_Icon_View"
    android:layout_toRightOf="@+id/state_Icon_View"
    android:text="The State"
    android:textColor="#000000"
    android:textSize="24sp"
    tools:ignore="HardcodedText" />

這是我的數據提供者:

import android.widget.ImageView;

public class StateDataProvider
{
private String state;
private ImageView image;

public StateDataProvider(String state)
{this.setState(state);}

public StateDataProvider(ImageView image)
{this.setImage(image);}

public String getState() {
    return state;
}

public ImageView getImage() {
    return image;
}

public void setState(String state)
{
    this.state = state;
}

public void setImage(ImageView image)
{
    this.image = image;
}
}

最后是我的適配器:

public class StateRecyclerAdapter extends RecyclerView.Adapter <StateRecyclerAdapter.RecyclerViewHolder> {

private ArrayList<StateDataProvider> arrayList = new ArrayList<StateDataProvider>();

private Context context;

public StateRecyclerAdapter(Context context, ArrayList<StateDataProvider> arrayList )
{
    this.arrayList = arrayList;
    this.context = context;

}

private String theStateIcon;

@Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.state_layout,parent,false);

    RecyclerViewHolder recyclerViewHolder = new RecyclerViewHolder(view);

    return recyclerViewHolder;
}

@Override
public void onBindViewHolder(StateRecyclerAdapter.RecyclerViewHolder holder, int position) {
    StateDataProvider data_provider = arrayList.get(position);

    holder.state.setText(data_provider.getState());

    String whichState =  data_provider.getState();
    switch (whichState) {
        case "New South Wales":
            theStateIcon = "NSW_icon.png";
        case "Victoria":
            theStateIcon = "VIC_icon.png";
        case "Queensland":
            theStateIcon = "QLD_icon.png";
        case "Tasmania":
            theStateIcon = "TAS_icon.png";
        case "South Australia":
            theStateIcon = "SA_icon.png";
        case "Northern Territory":
            theStateIcon = "NT_icon.png";
        case "Western Australia":
            theStateIcon = "WA_icon.png";
    }

    int resourceId = context.getResources().getIdentifier(theStateIcon, "assets", "au.com.itmobilesupport.sqltwo");
    holder.state_Icon.setImageDrawable(ContextCompat.getDrawable(context,resourceId));

}

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

public static class RecyclerViewHolder extends RecyclerView.ViewHolder
{
    TextView state;
    ImageView state_Icon;

    public RecyclerViewHolder(View view) {
        super(view);
        state = (TextView)view.findViewById(R.id.state_Item);
        state_Icon = (ImageView) view.findViewById(R.id.state_Icon_View);
        }
}
}

所以我試圖獲取上下文,然后為Imageview設置圖像,但是當我嘗試獲取上下文時,出現以下錯誤:

Error:(48, 19) error: constructor StateRecyclerAdapter in class 
StateRecyclerAdapter cannot be applied to given types;
required: Context,ArrayList<StateDataProvider>
found: ArrayList<StateDataProvider>
reason: actual and formal argument lists differ in length

編輯:這是我的StatePage.java

public class StatePage extends AppCompatActivity  {

private Cursor reefs;
private MyDatabase db;
private String chosenState;
RecyclerView recyclerView;
RecyclerView.Adapter adapter;
RecyclerView.LayoutManager layoutManager;
ArrayList<StateDataProvider> arrayList = new 
ArrayList<StateDataProvider>();
StateDataProvider dataprovider;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_state_page);
    db = new MyDatabase(this);

    reefs = db.getStateData(); 

    do{
        dataprovider = new StateDataProvider(reefs.getString(1));
        arrayList.add(dataprovider);

    } while (reefs.moveToNext());

    recyclerView = (RecyclerView)findViewById(R.id.recycler_view);
    recyclerView.setHasFixedSize(true);

    layoutManager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(layoutManager);

    adapter = new StateRecyclerAdapter(arrayList);
    recyclerView.setAdapter(adapter);

    recyclerView.addOnItemTouchListener(
            new MyRecyclerViewClickListener(this, new 
MyRecyclerViewClickListener.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {
                    chosenState = arrayList.get(position).getState();
                    goToRegionPage();
//                        System.out.println("State is" + 
arrayList.get(position).getState());
                }
            })
    );
}
public void goToRegionPage()
{
    Intent intent = new Intent(this, RegionPage.class);
    intent.putExtra("theState", chosenState);
    startActivity(intent);
}
}

您的構造函數調用的參數不足

StateRecyclerAdapter state = new StateRecyclerAdapter(arrayList);

StateRecyclerAdapter state = new StateRecyclerAdapter(getApplicationContext(),arrayList);


然后是圖像部分

您需要先獲取位圖

private Bitmap getBitmapFromAsset(String strName)
{
        AssetManager assetManager = getAssets();
        InputStream istr = null;
        try {
            istr = assetManager.open(strName);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Bitmap bitmap = BitmapFactory.decodeStream(istr);
        return bitmap;
}

然后

holder.state_Icon.setImageBitmap(getBitmapFromAsset("name of image"));

錯誤是來自“活動”中的此行:

adapter = new StateRecyclerAdapter(arrayList);

您應該更改為

adapter = new StateRecyclerAdapter(this, arrayList);

因為您的SateRecyclerAdapter構造函數需要兩個參數(上下文和數據列表):

public StateRecyclerAdapter(Context context, ArrayList<StateDataProvider> arrayList )

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM