0

The overall idea is to change the background of a TextView in a ListView based on the value that is put into it.

E.g. if the value entered is true, then the colour changes.

however I having trouble getting the specific value rather than the string

Here is my controller

public ArrayList<HashMap<String, String>> getAllSkills () {
    Log.d(LOGCAT, "getAll");
    ArrayList<HashMap<String, String>> wordList;
    wordList = new ArrayList<HashMap<String, String>>();
    String selectQuery = "SELECT  * FROM " + "characterSkills";
    SQLiteDatabase database = this.getWritableDatabase();
    Cursor cursor = database.rawQuery(selectQuery, null);
    if (cursor.moveToFirst()) {
        do {
            HashMap<String, String> map = new HashMap<String, String>();
            map.put("skillId", cursor.getString(0));
            map.put("skillName", cursor.getString(1));
            map.put("ability", cursor.getString(2));
            map.put("rank", cursor.getString(3));
            map.put("enabled", cursor.getString(4));
            wordList.add(map);
        } while (cursor.moveToNext());
    }
    return wordList;
}

Which is being called by this:

ArrayList<HashMap<String, String>> characterSkills = controller
            .getAllSkills();

    ListAdapter adapter = new SimpleAdapter(CharacterSheet.this,
            characterSkills, R.layout.dd_skill_entry, new String[] {
                    "skillId", "skillName", "ability", "rank", "enabled" }, 
            new int[] { R.id.skillid, R.id.skillname, R.id.ability,
                    R.id.rank, R.id.enabled });


    Toast.makeText(getApplicationContext(), "" + characterSkills.get(4), Toast.LENGTH_LONG).show();

    setListAdapter(adapter);

The toast returns the entire hashmap at that location rather than the "enabled" value that I want.

So my question is how do I get this value and how do I check its true and if it is change the background colour of the specific Views (R.id.enabled) it is entered into.

2
  • i think for changing background color of the specific Views you will need to create custom Adapter instead of using default Commented Feb 18, 2014 at 20:18
  • why do you have a HashMap inside an ArrayList? In any case, you first need to access the specific HashMap within the ArrayList's, and then get the specific value from the HashMap Commented Feb 18, 2014 at 20:18

1 Answer 1

1

I think the best things to do is create a POJO like

public class CharacterSkills {

private long skillId; 
private String skillName; 
private boolean enabled;

// TODO : make getters/setters

}

and then used this POJO in your function. Stuff like this :

public List<CharacterSkills> getAllSkills () {
    Log.d(LOGCAT, "getAll");
    List<CharacterSkills> wordList;
    wordList = new ArrayList<CharacterSkills>();
    String selectQuery = "SELECT  * FROM " + "characterSkills";
    SQLiteDatabase database = this.getWritableDatabase();
    Cursor cursor = database.rawQuery(selectQuery, null);
    if (cursor.moveToFirst()) {
        do {
            CharacterSkills character = new CharacterSkills();
            character.setSkillId(cursor.getLong(0));
            character.setSkillName(cursor.getString(1));
            character.setEnabled(cursor.getInt(2) == 1 ? true : false);  // there is no boolean in SQLite
            // TODO : all informations about character 

            wordList.add(map);
        } while (cursor.moveToNext());
    }
    return wordList;
}

Then you will use a custom adapter

public class CharacterAdapter extends BaseAdapter {
    private List<CharacterSkills> characters;

    public CharacterAdapter(List<CharacterSkills> characters) {
        this.characters = characters;
    }

    @Override
    public int getCount() {
        return characters.size();
    }

    @Override
    public CharacterSkills getItem(int position) {
        return characters.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // You can use ViewHolder pattern to optimize your listview 
        convertView = LayoutInfalter.from(ctx).inflate(R.layout.dd_skill_entry, null);
        TextView myCharacter = (TextView) convertView.findViewById(R.id.enabled);
        CharacterSkills character = characters.get(position);
        // TODO : define your background
        if (character.isEnabled()) {
             myCharacter.setBackgroundResource(R.drawable.yourdrawableenable);
        } else {
             myCharacter.setBackgroundResource(R.drawable.yourdrawabledisable);
        }
        return convertView;
    }

}

And finally, you can use your own adapter.

List<CharacterSkills> characters = controller.getAllSkills();
CharacterAdapter adapter = new CharacterAdapter(characters);
setListAdapter(adapter);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.