简体   繁体   中英

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.

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

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

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. What i want is, each button keeps corresponding next string-array id. Basically, i dont want to if-else if-else or switch cases for solving these in same activity nor next game activity. 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. .setTag method adds buttontexts as tag. For example; FOOTBALL button has FOOTBALL tag.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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