gpt4 book ai didi

安卓 : how to get value from assets and pass it thru array

转载 作者:太空狗 更新时间:2023-10-29 13:17:37 25 4
gpt4 key购买 nike

Problem image file我想通过使用数组来缩短我的代码长度。如果我有 100 个按钮或 ImageView ,我必须写 100 个。我想从 assets 文件夹中获取值(value)并将其传递给 array 。如何使用

String[] colorNames = getResources().getStringArray(R.array.colorList);
String[] animalNames = getResources().getStringArray(R.array.animalList);

当前工作代码:

public class Main extends Activity {

ImageView image;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.drag);
Intent intent = getIntent();
String key = intent.getStringExtra("YOUR_KEY");
image = (ImageView) findViewById(R.id.image);

Button btn1,btn2;
btn1 = (Button) findViewById(R.id.btn1);
btn2 = (Button) findViewById(R.id.btn2);

if(key.equals("animalIntent") )
{
btn1.setText("cat");
btn2.setText("dog");

//100 个按钮也一样......................这里我想缩短我的代码

} else {
btn1.setText("red");
btn2.setText("blue");
}
}

public void btn1(View view) {
Intent intent = getIntent();
String key = intent.getStringExtra("YOUR_KEY");
if(key.equals("animalIntent") )
{
image.setBackgroundResource(R.drawable.cat);

//100 个 ImageView 相同......................这里我想缩短我的代码

    } else {
image.setBackgroundResource(R.drawable.red);
}
}

public void btn2(View view) {
Intent intent = getIntent();
String key = intent.getStringExtra("YOUR_KEY");
if(key.equals("animalIntent") )
{
image.setBackgroundResource(R.drawable.dog);


} else {
image.setBackgroundResource(R.drawable.blue);
}
}

result with working code

最佳答案

要做到这一点,您应该使用 GridView,这将是最佳选择。试试下面的示例代码。Step-1 在你的 drag.xml 中添加 gridview

<GridView
android:id="@+id/grid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/edt"
android:numColumns="2"
android:stretchMode="columnWidth" >
</GridView>

Step-2 声明变量并获取 GridView 引用。

GridView gridview;
Integer[] drawableIds = { R.drawable.poster1, R.drawable.poster2, R.drawable.poster3, R.drawable.poster4,
R.drawable.poster5 };
String[] colorNames = { "poster one", "poster two", "poster three", "poster four", "poster five" };

//在OnCreate()方法中添加

gridview = (GridView) findViewById(R.id.grid);
gridview.setAdapter(new CustomGridAdapter(this, drawableIds, colorNames));
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

Toast.makeText(Main.this, "" + position, Toast.LENGTH_LONG).show();
}
});

Step-3 为网格适配器 CustomGridAdapter.class 创建一个类

public class CustomGridAdapter extends BaseAdapter {

private Context context;
private Integer[] drawableitems;
private String[] drawablelabel;

public CustomGridAdapter(Context context, Integer[] drawableitems, String[] drawablelabel) {

this.context = context;
this.drawablelabel = drawablelabel;
this.drawableitems = drawableitems;

}

@Override
public int getCount() {
return drawableitems.length;
}

@Override
public Object getItem(int position) {
return null;
}

@Override
public long getItemId(int position) {
return 0;
}

public View getView(int position, View convertView, ViewGroup parent) {

RecordHolder holder = null;

if (convertView == null) {

LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
holder = new RecordHolder();
convertView = inflater.inflate(R.layout.grid_item, null);
holder.txtTitle = (TextView) convertView.findViewById(R.id.grid_item_label);

holder.imageItem = (ImageView) convertView.findViewById(R.id.grid_item_image);

convertView.setTag(holder);

} else {
holder = (RecordHolder) convertView.getTag();
}

holder.txtTitle.setText(drawablelabel[position]);
holder.imageItem.setImageResource(drawableitems[position]);
return convertView;
}

static class RecordHolder {
TextView txtTitle;
ImageView imageItem;

}
}

在布局文件夹中为网格 grid_item.xml 添加自定义 View 。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/llback"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >

<ImageView
android:id="@+id/grid_item_image"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center" >
</ImageView>

<TextView
android:id="@+id/grid_item_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:textColor="@android:color/black"
android:textSize="18sp"
android:textStyle="bold" >
</TextView>

</LinearLayout>

希望对你有所帮助。

关于安卓 : how to get value from assets and pass it thru array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33074555/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com