Android的:如何从资产中获得价值,并将其传递直通阵列 [英] Android : how to get value from assets and pass it thru array

查看:151
本文介绍了Android的:如何从资产中获得价值,并将其传递直通阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题映像文件
我用array.If我有100个按钮或图像视图我必须写100要短我的code的长度。我想从资产价值的文件夹,并把它传递到阵列。如何使用

I want to short my code length by using array.If I have 100 button or image view I have to write for 100 . I want to get value from assets folder and pass it in to array . how to use

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

当前工作code:

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按钮.............在这里,我想我的短code

// same for 100 button ............. here i want to short my code

} 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图像视图.............在这里,我想我的短code

// same for 100 image view ............. here i want to short my code

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

导致有工作code

推荐答案

要做到这一点,你应该使用GridView控件,这将是最好的选择。试试下面的示例code。
步骤1在drag.xml添加在GridView

To do that you should use GridView,it would be best option. try the below sample code. Step-1 add the gridview in your drag.xml

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

步骤2声明变量并获得网格视图参考。

Step-2 Declare the variables and get the grid view reference.

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()方法添加

//In OnCreate() method add

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

步骤3创建网格适配器CustomGridAdapter.class类

Step-3 create a class for grid adapter 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布局文件夹中。

Add custom view for grid grid_item.xml in layout folder.

<?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>

希望它会帮助你。

Hope it will help you.

这篇关于Android的:如何从资产中获得价值,并将其传递直通阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆