Android从ArrayList获取值

时间:2014-02-18 20:14:11

标签: android listview

总体思路是根据放入其中的值更改ListView中TextView的背景。

E.g。如果输入的值为true,则颜色会发生变化。

但是我无法获取特定值而不是字符串

这是我的控制器

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

这被称为:

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

toast返回该位置的整个hashmap,而不是&#34; enabled&#34;我想要的价值。

所以我的问题是如何获取此值以及如何检查其值以及是否更改了输入的特定视图(R.id.enabled)的背景颜色。

1 个答案:

答案 0 :(得分:1)

我认为最好的办法是创建像

这样的POJO
public class CharacterSkills {

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

// TODO : make getters/setters

}

然后在你的函数中使用这个POJO。像这样的东西:

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

然后您将使用自定义适配器

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

}

最后,您可以使用自己的适配器。

List<CharacterSkills> characters = controller.getAllSkills();
CharacterAdapter adapter = new CharacterAdapter(characters);
setListAdapter(adapter);