【问题标题】:Set image inside RecyclerView from Adapter从 Adapter 设置 RecyclerView 内的图像
【发布时间】:2017-10-16 06:36:07
【问题描述】:

所以我的 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 的位置发布您的 Activity/Fragment 代码。该错误不是由于设置图像问题。
  • Included StartPage.java 它从启动时导入的 SQL 数据库中获取数据。它一切正常,但我只想通过包含图像来增强应用程序,这就是我卡住的地方。
  • 感谢 Quang,我在 adapter = new StateRecyclerAdapter(arrayList); 处遇到错误我也需要提供上下文,但不确定如何提供。是的,我是一个新手,如果这是一个愚蠢的问题,我很抱歉。
  • 请在下面查看我的答案。

标签: java android android-studio android-recyclerview


【解决方案1】:

错误来自您的活动中的这一行:

adapter = new StateRecyclerAdapter(arrayList);

你应该改成

adapter = new StateRecyclerAdapter(this, arrayList);

因为您的 SateRecyclerAdapter 构造函数需要两个参数(上下文和数据列表):

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

【讨论】:

    【解决方案2】:

    你的构造函数调用的参数不够

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

    【讨论】:

    • 我会尝试并感谢您提供的信息,但构造函数仍然存在问题。我是否需要您的解决方案的“上下文”,因为我即将删除上下文代码并在没有上下文的情况下尝试它
    猜你喜欢
    • 1970-01-01
    • 2021-12-17
    • 2019-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多