简体   繁体   English

如何在Android中编写用于管理资源的通用类(strings.xml)

[英]how to write generic class for managing resources(strings.xml) in Android

I'm have troubling about a design situation in my Hangman game in Android. 我对Android中的Hangman游戏中的设计情况感到不安。

public class ModeSelectionActivity extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modeselection);

        fillGridView();

    }

    private void fillGridView(){
        ClickHandler clickHandler = new ClickHandler();
        String[] categories = getResources().getStringArray(R.array.categories);

        CustomGridAdapter customGridAdapter = new CustomGridAdapter(this,clickHandler,categories);
        GridView modeSelection = (GridView) findViewById(R.id.modeSelection);
        modeSelection.setAdapter(customGridAdapter);
    }
}

In this activity, I'm designing a page that a user picks a game mode such like football words, city/country words etc.. This category names are coming from strings.xml file 在这个活动中,我正在设计一个用户选择游戏模式的页面,如足球词,城市/乡村词等。此类别名称来自strings.xml文件

<string-array name="categories">
        <item>FOOTBALL</item>
        <item>CELEBRITIES</item>
        <item>SERIES/MOVIE</item>
        <item>CITY/COUNTRY</item>
        <item>MIXED</item>
    </string-array>

And these array is passed into customgridviewadapter, and it creates buttons, and gives category name(like football) as a tag to each button. 这些数组传递到customgridviewadapter,它创建按钮,并为每个按钮提供类别名称(如足球)作为标记。

My trouble begins here. 我的麻烦从这里开始。 I do not want to control which button is clicked and accordingly to that i do not want to send id(for strings.xml) for gaming words for that category. 我不想控制点击哪个按钮,因此我不想为该类别的游戏单词发送id(对于strings.xml)。 What i want is, each button keeps corresponding next string-array id. 我想要的是,每个按钮保持相应的下一个字符串数组ID。 Basically, i dont want to if-else if-else or switch cases for solving these in same activity nor next game activity. 基本上,我不想if-else if-else或切换案例来解决相同的活动或下一个游戏活动。 If anyone give me any idea iwould be happy :) 如果有人给我任何想法,我会很高兴:)

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

        Button btn;
        if(convertView == null){
            btn = new Button(context);
            btn.setPadding(3,3,3,3);
        }else{
            btn = (Button)convertView;
        }
        btn.setText(getItem(position));
        btn.setTextColor(Color.WHITE);
        btn.setBackgroundResource(R.drawable.gridbuttonshape);
        //btn.setTag("btn_"+getItem(position));
        btn.setTag(getItem(position));


        btn.setOnClickListener(clickListener);

        return btn;
    }

This is my customgridadapter class' getView method. 这是我的customgridadapter类'getView方法。 .setTag method adds buttontexts as tag. .setTag方法将buttontexts添加为标记。 For example; 例如; FOOTBALL button has FOOTBALL tag. FOOTBALL按钮有FOOTBALL标签。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM