从Adapter中设置RecyclerView内的图像

时间:2017-05-17 01:21:46

标签: java android android-studio recycler-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);
}
}

2 个答案:

答案 0 :(得分:1)

你构造函数调用不够param

# Applies the function to all columns except 'category', the group_by column
testdata %>%
    group_by(category) %>%
    mutate_all(function(x) { x - min(x)})

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

答案 1 :(得分:1)

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

adapter = new StateRecyclerAdapter(arrayList);

你应该改为

adapter = new StateRecyclerAdapter(this, arrayList);

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

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