我为GridView创建了一个自定义适配器,该适配器将显示一本书的图像(查看图像)及其名称(查看文本); ,我在字符串文件中声明了书名列表: [strings.xml]
<string-array name="bookLabels">
<item>android security</item>
<item>penetration_testing</item>
<item>red_team</item>
<item>Linux Firewalls</item>
<item>the_art_of_exploitation</item>
<item>web_application_hacker</item>
<item>Linux Basicsfor Hackers</item>
<item>Black Hat Python</item>
</string-array>
所以我想对图像做同样的事情,而不是像这样在代码中声明它们:
private Integer[] bookImages =
{
R.drawable.android_security,
R.drawable.penetration_testing,
R.drawable.red_team,
R.drawable.lf,
R.drawable.the_art_of_exploitation,
R.drawable.web_application_hacker,
R.drawable.lbh,
R.drawable.bhp
};
我在名为“ bookImages”的字符串数组中声明了图像的名称,
<string-array name="bookImages">
<item>@drawable/android_security</item>
<item>@drawable/penetration_testing</item>
<item>@drawable/red_team</item>
<item>@drawable/lf</item>
<item>@drawable/the_art_of_exploitation</item>
<item>@drawable/web_application_hacker </item>
<item>@drawable/lbh </item>
<item>@drawable/bhp </item>
</string-array>
然后我恢复了在“ bookimagesfromstring”数组中声明的所有图像的位置:
Resources resi = context.getResources();
bookImagesFromString = resi.getStringArray(R.array.bookImages);
System.out.println("bookImages Images test 1: " +
Arrays.toString(bookImagesFromString))
这是[主要活动]的源代码
public class main extends AppCompatActivity {
String[] labels;
String[ ] pages;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Resources res = getResources();
labels = res.getStringArray(R.array.bookLabels);
pages = res.getStringArray(R.array.web_pages);
GridView gridView = findViewById(R.id.gridView1);
BookGrid myAdapter = new BookGrid(getApplicationContext(), labels);
gridView.setAdapter(myAdapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Toast.makeText(
getApplicationContext(),
((TextView) v.findViewById(R.id.gridtextView))
.getText(), Toast.LENGTH_SHORT).show();
// Intent webPageIntent = new Intent(getApplicationContext(),
DisplayWebPage.class);
// webPageIntent.putExtra("WEB_PAGE", pages[ position ] );
// startActivity( webPageIntent );
}
});
}
}
和[BookGrid类]
public class BookGrid extends BaseAdapter {
private Context context;
private LayoutInflater inflater;
private String[] imageName;
private String[] bookImagesFromString;
private Integer[] bookImages =
{
R.drawable.android_security,
R.drawable.penetration_testing,
R.drawable.red_team,
R.drawable.lf,
R.drawable.the_art_of_exploitation,
R.drawable.web_application_hacker,
R.drawable.lbh,
R.drawable.bhp
};
public BookGrid(Context context, String[] imageName) {
this.context = context;
this.imageName = imageName;
this.inflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return bookImages.length;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
/*
Resources rest = context.getResources();
String[] labels = rest.getStringArray(R.array.bookLabels);
System.out.println("bookImagess Tex: " + Arrays.toString(labels));
*/
Resources resi = context.getResources();
bookImagesFromString = resi.getStringArray(R.array.bookImages);
System.out.println("bookImages Images test 1: " + Arrays.toString(bookImagesFromString));
if (convertView == null) {
convertView = inflater.inflate(R.layout.layout_grid_image, parent, false);
TextView txtv = convertView.findViewById(R.id.gridtextView);
ImageView imgv = convertView.findViewById(R.id.gridImageView);
//To Avoid ArrayIndexOutOfBoundsException
//boolean inBounds = (position >= 0) && (position <= imageName.length);
txtv.setText(imageName[position]);
//imgv.setImageResource(bookImagesFromString[position]);
imgv.setImageResource(bookImages[position]);
} else {
}
return convertView;
}
“ bookImagesFromString ”数组的结果如下:
({[res/drawable-v24/android_security.png, res/drawable-v24/penetration_testing.png, res/drawable-v24/red_team.png, res/drawable/lf.jpg, res/drawable-v24/the_art_of_exploitation.png, res/drawable-v24/web_application_hacker.png, res/drawable/lbh.png, res/drawable-v24/bhp.png]
)
所以问题是如何将“ bookImagesFromString ”数组转换为包含每个图像ID的数组? -然后在此代码部分中使用它:
imgv.setImageResource(bookImagesID[position]);
谢谢。
答案 0 :(得分:0)
尝试这样的字符串数组:
<string-array name="bookImages">
<item>android_security</item>
<item>penetration_testing</item>
<item>red_team</item>
<item>lf</item>
<item>the_art_of_exploitation</item>
<item>web_application_hacker</item>
<item>lbh</item>
<item>bhp</item>
</string-array>
和类似的适配器:
public class BookGrid extends BaseAdapter {
private Context context;
private LayoutInflater inflater;
private String[] imageName;
private Integer[] bookImages;
public BookGrid(Context context, String[] imageName) {
this.context = context;
this.imageName = imageName;
this.inflater = LayoutInflater.from(context);
Resources resi = context.getResources();
String[] bookImagesFromString = resi.getStringArray(R.array.bookImages);
bookImages = new Integer[bookImagesFromString.length];
for (int i = 0; i < bookImagesFromString.length; i++) {
bookImages[i] = (resi.getIdentifier(bookImagesFromString[i], "drawable", context.getPackageName()));
}
}
@Override
public int getCount() {
return imageName.length;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
/*
Resources rest = context.getResources();
String[] labels = rest.getStringArray(R.array.bookLabels);
System.out.println("bookImagess Tex: " + Arrays.toString(labels));
*/
if (convertView == null) {
convertView = inflater.inflate(R.layout.layout_grid_image, parent, false);
}
TextView txtv = convertView.findViewById(R.id.gridtextView);
ImageView imgv = convertView.findViewById(R.id.gridImageView);
txtv.setText(imageName[position]);
imgv.setImageResource(bookImages[position]);
return convertView;
}
}
希望有帮助!
答案 1 :(得分:0)
对于图像名称,如 I_A_Mok 所说,我只是:
<string-array name="bookImages">
<item>android_security</item>
<item>penetration_testing</item>
<item>red_team</item>
<item>lf</item>
<item>the_art_of_exploitation</item>
<item>web_application_hacker </item>
<item>lbh </item>
<item>bhp </item>
</string-array>
所以我有两个解决方案:
具有上下文的方法:
public static Integer[] getIds(Context context, String[] str) {
Integer[] bookID = new Integer[str.length];
for (int i = 0; i < str.length; i++) {
bookID[i] = context.getResources().getIdentifier(str[i], "drawable", context.getPackageName());
}
return bookID;
}
或者没有上下文(此方法比第一个Retrieving Resources in Android更快):
public static Integer[] getIds(String[] str) {
Integer[] bookID = new Integer[str.length];
try {
for (int i = 0; i < str.length; i++) {
Class res = R.drawable.class;
Field field = res.getField(str[i]);
int drawableId = field.getInt(null);
bookID[i] = drawableId;
}
} catch (Exception e) {
Log.e("MyTag", "Failure to get drawable id.", e);
e.printStackTrace();
}
return bookID;
}
这是完整的代码
package com.example.gridview.BookGridWebView;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.gridview.R;
import java.lang.reflect.Field;
import java.util.Arrays;
public class BookGrid extends BaseAdapter {
private Context context;
private LayoutInflater inflater;
private String[] imageName;
private String[] bookImagesFromStringArray;
private Integer[] bookImages =
{
R.drawable.android_security,
R.drawable.penetration_testing,
R.drawable.red_team,
R.drawable.lf,
R.drawable.the_art_of_exploitation,
R.drawable.web_application_hacker,
R.drawable.lbh,
R.drawable.bhp
};
public BookGrid(Context context, String[] imageName) {
this.context = context;
this.imageName = imageName;
this.inflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return bookImages.length;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
bookImagesFromStringArray =
context.getResources().getStringArray(R.array.bookImageName);
//the table contains the IDs of each Image
Integer[] bookIDs = getIds(context,bookImagesFromStringArray);
if (convertView == null) {
convertView = inflater.inflate(R.layout.layout_grid_image, parent, false);
TextView txtv = convertView.findViewById(R.id.gridtextView);
ImageView imgv = convertView.findViewById(R.id.gridImageView);
txtv.setText(imageName[position]);
// imgv.setImageResource(bookImages[position]);
imgv.setImageResource(bookIDs[position]);
} else {
}
return convertView;
}
public static Integer[] getIds(Context context, String[] str) {
Integer[] bookID = new Integer[str.length];
for (int i = 0; i < str.length; i++) {
bookID[i] = context.getResources().getIdentifier(str[i], "drawable", context.getPackageName());
}
return bookID;
}
public static Integer[] getIds(String[] str) {
Integer[] bookID = new Integer[str.length];
try {
for (int i = 0; i < str.length; i++) {
Class res = R.drawable.class;
Field field = res.getField(str[i]);
int drawableId = field.getInt(null);
bookID[i] = drawableId;
}
} catch (Exception e) {
Log.e("MyTag", "Failure to get drawable id.", e);
e.printStackTrace();
}
return bookID;
}
}
谢谢。