2013-10-31 86 views
0

我可能只是想念一些簡單的東西,但它在這裏。Android的ListView按鈕不更新SQLite行

目的:當ListView在每個row內創建視圖和onClick按鈕時,更新SQLite數據庫中的行。

第一次更新問題: 紫色框中的數字從「能力」SQLite表中成功拉取數據(整數)(因爲我剛剛重新安裝了示例中的零),但是,我希望ListView的每一行都取這個數字,創建視圖時更新「技能」表。我只把代碼放在第一個「if」上,以便在我重複其他5次之前測試它是否有效。

第二次更新問題: onClick的「+」按鈕,按鈕左側的數字增加1,然後將其與顏色數字相加並將其添加到最左邊的零。如果向下滾動,然後向上滾動,行將重置視圖。如果我在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 

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

回答

0

我知道這可能是晚了,但嘗試從內容刪除列ID值作爲數據庫更新的一部分,並查看是否解決了問題#2。

對於問題#1這似乎是在你的getView()方法,你是一旦與unmodified/non-instantiated值每次按下sButton時間更新數據庫即可。這是有原因的嗎?也許你可以首先實例化變量,或者只需按下所需的按鈕即可更新數據庫。