0

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

目标:当 ListView 在每行中创建一个 Button 的视图和 onClick 时,更新 SQLite DB 中的

第一次更新问题:紫色框中的数字成功从“能力”SQLite 表中提取数据(整数)(示例中为零,因为我刚刚重新安装)但是,我希望 ListView 的每一行都采用该数字并更新“技能”表创建视图时。我只将代码放在第一个“if”上以测​​试它是否有效,然后再重复其他 5 次。

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

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

任何帮助将不胜感激!请询问可能需要其他代码/信息的地方。

数据库处理程序类:

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

技能适配器类:

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

1 回答 1

0

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

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

于 2014-07-15T11:51:42.107 回答