Android ListView按钮不更新SQLite行

时间:2013-10-31 00:34:37

标签: android sqlite listview

我可能只是错过了一些简单的东西,但现在就去了。

目标:当ListView在每个row内创建视图和onClick按钮时,更新SQLite DB中的行。

首次更新问题: 紫色框中的数字从"能力"中提取数据(整数)。 SQLite表成功(例如,因为我刚刚重新安装),但是,我希望ListView的每一行都能获取该数字并更新"技能"创建视图时的表。我只把代码放在第一个" if"在我重复其他5次之前测试它是否有效。

第二次更新问题: onClick" +"按钮,按钮左侧的数字增加1,然后将其与彩色数字和setText添加到最左边的零。如果我向下滚动然后向上滚动该行将重置视图。如果我在onClick()结束时发出notifyDataSetChanged(),则不会发生任何反应。在任何一种情况下,数据库都不会更新。

这些事件都没有更新表,我在编译器或运行时没有收到任何错误。

非常感谢任何帮助!请询问可能需要其他代码/信息的地方。

DatabaseHandler类:

private static final String tableSkill = "skill";
private static final String colSkillId = "sID"; // and so on
public int updateSkill(Skill skill) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(colSkillId, skill.getSkillId());
    values.put(colSkillMod, skill.getSkillMod());
    values.put(colSkillAbil, skill.getSkillAbil());
    values.put(colSkillRank, skill.getSkillRank());
    values.put(colSkillMisc, skill.getSkillMisc());
    // updating row
    return db.update(tableSkill, values, colSkillId + " = ?",
            new String[] { String.valueOf(skill.getSkillId()) });
}

技能课程:

public Skill(int skillId, int skillMod, int skillAbil, int skillRank, int skillMisc){
    this._skillId = skillId;
    this._skillMod = skillMod;
    this._skillAbil = skillAbil;
    this._skillRank = skillRank;
    this._skillMisc = skillMisc;
}
public int getSkillId(){
    return this._skillId;
}

public void setSkillID(int skillId){
    this._skillId = skillId;
} // and so on for the other 4

SkillAdapter类:

public class SkillAdapter extends BaseAdapter {
private Context context;
private ArrayList<Skill> skillList;
private DatabaseHandler db;
private int id;
private int mod;
private int rank;
private int abil;
private int misc;

public SkillAdapter(Context context, ArrayList<Skill> list) {
    this.context = context;
    skillList = list;
}

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

@Override
public Object getItem(int position) {
    return skillList.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup arg2) {
    Skill skill = skillList.get(position);
    ViewHolder holder = new ViewHolder();

    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.lay_skills_row, null);
    }

    db = new DatabaseHandler(context);
    holder.sID = (TextView)convertView.findViewById(R.id.skill_ID);
    holder.sName = (TextView)convertView.findViewById(R.id.skill_name);
    holder.sKey = (TextView)convertView.findViewById(R.id.skill_key);
    holder.sMod = (TextView)convertView.findViewById(R.id.skill_total);
    holder.sAbil = (TextView)convertView.findViewById(R.id.skill_abil);
    holder.sRanks = (TextView)convertView.findViewById(R.id.skill_ranks);
    holder.sMisc = (EditText)convertView.findViewById(R.id.skill_misc);
    holder.sButton = (Button)convertView.findViewById(R.id.skill_button);
    holder.sButton.setTag(holder);
    holder.sButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            ViewHolder holder1 = (ViewHolder)v.getTag();
            id = Integer.parseInt(holder1.sID.getText().toString());
            rank = Integer.parseInt(holder1.sRanks.getText().toString());
            misc = Integer.parseInt(holder1.sMisc.getText().toString());
            rank = rank + 1;
            abil = Integer.parseInt(holder1.sAbil.getText().toString());
            mod = rank + abil;
            holder1.sRanks.setText("" + String.valueOf(rank));
            holder1.sMod.setText(String.valueOf(mod));
            db.updateSkill(new Skill(id, mod, abil, rank, 0)); // ISSUE 2
        }
    });

    holder.sID.setText(String.valueOf(skill.getSkillId())); // OK
    holder.sName.setText(skill.getSkillName()); // OK
    holder.sKey.setText(skill.getSkillKey()); // OK
    holder.sMod.setText(String.valueOf(skill.getSkillMod())); 
    holder.sRanks.setText(String.valueOf(skill.getSkillRank()));
    holder.sMisc.setText(String.valueOf(skill.getSkillMisc()));

    if(skill.getSkillKey().contentEquals("STR") == true){
        holder.sAbil.setText(""+db.getAbilityMod("STR"));
        abil = Integer.parseInt(db.getAbilityMod("STR"));
        holder.sAbil.setBackgroundColor(Color.parseColor("#e60000"));
        db.updateSkill(new Skill(skill.getSkillId(), skill.getSkillMod(), abil, rank, misc));} // ISSUE 1
    else if (skill.getSkillKey().contentEquals("DEX") == true){
        holder.sAbil.setText(""+db.getAbilityMod("DEX"));
        holder.sAbil.setBackgroundColor(Color.parseColor("#0073e6"));}
        // will also have db.updates once I know the first one works
    else if (skill.getSkillKey().contentEquals("CON") == true){
        holder.sAbil.setText(""+db.getAbilityMod("CON"));
        holder.sAbil.setBackgroundColor(Color.parseColor("#00e673"));}
    else if (skill.getSkillKey().contentEquals("INT") == true){
        holder.sAbil.setText(""+db.getAbilityMod("INT"));
        holder.sAbil.setBackgroundColor(Color.parseColor("#e600e6"));}
    else if (skill.getSkillKey().contentEquals("WIS") == true){
        holder.sAbil.setText(""+db.getAbilityMod("WIS"));
        holder.sAbil.setBackgroundColor(Color.parseColor("#e67300"));}
    else if (skill.getSkillKey().contentEquals("CHA") == true){
        holder.sAbil.setText(""+db.getAbilityMod("CHA"));
        holder.sAbil.setBackgroundColor(Color.parseColor("#e6e600"));}      
    return convertView;
}

static class ViewHolder{
    Skill skill;
    TextView sID;
    TextView sName;
    TextView sKey;
    TextView sMod;
    TextView sAbil;
    TextView sRanks;
    EditText sMisc;
    Button sButton; 
}}

1 个答案:

答案 0 :(得分:0)

我知道这可能会迟到,但请尝试从内容值中删除列ID,作为更新数据库的一部分,看看是否能解决问题#2。

问题#1似乎在您的getView()方法中,您使用unmodified/non-instantiated值更新数据库,然后每次按下sButton。这有什么理由吗?也许你可以先实例化变量,或者只在按下所需的按钮时更新数据库。