我正在使用Recyclerview并希望在其中添加图像,但效果很好。我想在我的资源文件夹中使用int数组drawables并从我的SD卡上的set文件夹加载图像。我有一个字符串数组,列出了所有图像的路径,并在我的cardview中设置了textview,但我无法将其转换为int数组以加载图像。
这是我的代码,我已经注释掉了字符串数组和int数组,它们加载了硬编码的drawable.resources和textview。
如果您需要更多信息/代码,我可以发布它。提前谢谢。
feed.setMyFiles(previewsPath [i]);工作正常。
feed.setThumbnail(intarray [i]);是我陷入困境的地方。
MainActivity
public class MainCardActivity extends ActionBarActivity {
private RecyclerView recyclerView;
private CardView cardView;
private ArrayList<FeedProperties> os_versions;
private RecyclerView.Adapter mAdapter;
// private RecyclerView.LayoutManager mLayoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.maincardlayout);
initContrls();
}
private void initContrls() {
//cardView = (CardView) findViewById(R.id.cardList);
recyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
// final String[] versions = {"Synergy", "Synergy", "Synergy", "Synergy", "Synergy", "Synergy", "Synergy"};
// final int[] previewsImages = {R.drawable.psss, R.drawable.psss, R.drawable.psss, R.drawable.psss, R.drawable.sss, R.drawable.dog, R.drawable.sss, R.drawable.psss, R.drawable.dog, R.drawable.dog, R.drawable.sss, R.drawable.psss};
// Check for count
os_versions = new ArrayList<FeedProperties>();
File pathToFiles = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Overlays/Previews");
String[] previewsPath = pathToFiles.list();
//ArrayList<String> filePaths;
//populate filePaths with your list of file paths.
//feed.setThumbnail(Drawable.createFromPath(filePaths.get(i)));
//int intarray[] = new int[previewsPath.length];
// for (int i = 0; i < previewsPath.length; i++) {
// FeedProperties feed = new FeedProperties();
ArrayList<String> filePaths = new ArrayList<>();
for (String s : pathToFiles.list()) {
filePaths.add(s); }
for (int i = 0; i < previewsPath.length; i++) {
FeedProperties feed = new FeedProperties();
feed.setThumbnail(Drawable.createFromPath(filePaths.get(i)));
feed.setMyFiles(previewsPath [i]);
//feed.setMyFiles(previewsPath [i]);
// intarray[i] = Integer.parseInt(previewsPath[i]);
// feed.setThumbnail(intarray[i]);
os_versions.add(feed);
}
recyclerView.setHasFixedSize(true);
// ListView
recyclerView.setLayoutManager(new LinearLayoutManager(this));
//Grid View
// recyclerView.setLayoutManager(new GridLayoutManager(this,2,1,false));
//StaggeredGridView
// recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2,1));
mAdapter = new CardViewDataAdapter(os_versions);
recyclerView.setAdapter(mAdapter);
}
}
模型类
public class FeedProperties {
private String title;
private String myfiles;
private int thumbnail;
public String getTitle() {
return title;
}
public String getMyFiles() {
return myfiles;
}
public void setMyFiles(String myfiles) {
this.myfiles = myfiles;
}
public void setTitle(String title) {
this.title = title;
}
public int getThumbnail() {
return thumbnail;
}
public void setThumbnail(int thumbnail) {
this.thumbnail = thumbnail;
}
}
适配器类
public class CardViewDataAdapter extends RecyclerView.Adapter<CardViewDataAdapter.ViewHolder> {
private static ArrayList<FeedProperties> dataSet;
public CardViewDataAdapter(ArrayList<FeedProperties> os_versions) {
dataSet = os_versions;
}
@Override
public CardViewDataAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
//create a new view
View itemLayoutView = LayoutInflater.from(viewGroup.getContext()).inflate(
R.layout.card_view, null);
//create ViewHolder
ViewHolder viewHolder = new ViewHolder(itemLayoutView);
return viewHolder;
}
@Override
public void onBindViewHolder(CardViewDataAdapter.ViewHolder viewHolder, int i) {
FeedProperties fp = dataSet.get(i);
viewHolder.tvVersionName.setText(fp.getTitle());
viewHolder.tvmyfilesName.setText(fp.getMyFiles());
viewHolder.iconView.setImageResource(fp.getThumbnail());
viewHolder.feed = fp;
}
@Override
public int getItemCount() {
return dataSet.size();
}
@Override
public long getItemId(int position) {
return position;
}
// inner class to hold a reference to each item of RecyclerView
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView tvVersionName;
public TextView tvmyfilesName;
public ImageView iconView;
public FeedProperties feed;
public ViewHolder(View itemLayoutView) {
super(itemLayoutView);
tvVersionName = (TextView) itemLayoutView.findViewById(R.id.tvVersionName);
tvmyfilesName = (TextView) itemLayoutView.findViewById(R.id.tvmyfilesName);
iconView = (ImageView) itemLayoutView.findViewById(R.id.iconId);
itemLayoutView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), SecondPage.class);
v.getContext().startActivity(intent);
Toast.makeText(v.getContext(), "os version is: " + feed.getTitle(), Toast.LENGTH_SHORT).show();
}
});
}
}
}
ArrayList<String> filePaths = new ArrayList<>();
for (String s : pathToFiles.list()) {
filePaths.add(s);
}
for (int i = 0; i < previewsPath.length; i++) {
FeedProperties feed = new FeedProperties();
feed.setThumbnail(Drawable.createFromPath(filePaths.get(i)));
feed.setMyFiles(previewsPath [i]);
//feed.setMyFiles(previewsPath [i]);
//intarray[i] = Integer.parseInt(previewsPath[i]);
//feed.setThumbnail(intarray[i]);
os_versions.add(feed);
}
答案 0 :(得分:0)