2016-04-22 68 views
0

我使用SugarORM來管理好友列表SugarORM更新

  • AndroidManifest.xml中

    <application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme" 
    android:name="com.orm.SugarApp"> 
    <meta-data android:name="DATABASE" android:value="myfriends.db" /> 
    <meta-data android:name="VERSION" android:value="2" /> 
    <meta-data android:name="QUERY_LOG" android:value="true" /> 
    <meta-data android:name="DOMAIN_PACKAGE_NAME" android:value="com.example" /> 
    <activity android:name=".MainActivity" > 
        <intent-filter> 
         <action android:name="android.intent.action.MAIN" /> 
    
         <category android:name="android.intent.category.LAUNCHER" /> 
        </intent-filter> 
    </activity> 
    </application> 
    
  • Friend.java

    public class Friend extends SugarRecord{ 
    
    int recID; 
    String name; 
    String phone; 
    
    public Friend(){} 
    public Friend(int rec, String n, String p) 
    { 
        this.recID = rec; 
        this.name = n; 
        this.phone = p; 
    } 
    
    @Override 
    public String toString() { 
        return "[" + recID + ", " + name + ", " + phone + "]"; 
    } 
    } 
    
  • activity_main.xml中

    <Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Insert" 
    android:id="@+id/buttonInsert" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentStart="true" /> 
    
    <Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Delete" 
    android:id="@+id/buttonDelete" 
    android:layout_alignTop="@+id/buttonInsert" 
    android:layout_toEndOf="@+id/buttonInsert" /> 
    
    <Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Update" 
    android:id="@+id/buttonUpdate" 
    android:layout_alignTop="@+id/buttonDelete" 
    android:layout_toEndOf="@+id/buttonDelete" /> 
    
    <EditText 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/editTextID" 
    android:hint="Enter ID to update or delete" 
    android:layout_below="@+id/buttonInsert" 
    android:layout_alignParentStart="true" /> 
    
    <EditText 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/editTextName" 
    android:hint="Enter new name" 
    android:layout_below="@+id/editTextID" 
    android:layout_alignParentStart="true" /> 
    
    <ScrollView 
    android:layout_width="match_parent" 
    android:layout_height="500dp" 
    android:background="@color/abc_search_url_text_normal" 
    android:id="@+id/scrollView" 
    android:layout_below="@+id/editTextName" 
    android:layout_alignParentStart="true" > 
    
    <TextView 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:layout_margin="10dp" 
        android:text="" 
        android:id="@+id/textView" /> 
    </ScrollView> 
    
    <Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Show" 
    android:id="@+id/buttonShow" 
    android:layout_alignBottom="@+id/buttonUpdate" 
    android:layout_toEndOf="@+id/buttonUpdate" /> 
    
    <EditText 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/editTextPhone" 
    android:hint="Enter new phone" 
    android:layout_above="@+id/scrollView" 
    android:layout_alignEnd="@+id/scrollView" /> 
    
  • MainActivity.java

    public class MainActivity extends Activity implements View.OnClickListener { 
    
    SQLiteDatabase db; 
    TextView txtMsg; 
    Button btnInsert; 
    Button btnDelete; 
    Button btnUpdate; 
    Button btnShow; 
    EditText edtID; 
    EditText edtName; 
    EditText edtPhone; 
    
    // id 
    static int id = 0; 
    
    // Friends list 
    List<Friend> friendList; 
    
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 
    
        //initializeTable(); 
        resetTable(); 
        getWidgets(); 
    } 
    
    private void resetTable() { 
        Friend.deleteAll(Friend.class); 
    } 
    
    // Get friend list 
    private List<Friend> getFriendList() 
    { 
        return Friend.listAll(Friend.class); 
    } 
    
    // Show to textView 
    private void show() 
    { 
        txtMsg.setText(""); 
        friendList = getFriendList(); 
        txtMsg.append("[ID, Name, Phone]"); 
        for (int i = 0; i < friendList.size(); i++) 
        { 
         txtMsg.append("\n[" + friendList.get(i).recID + ", " + 
         friendList.get(i).name + ", " + friendList.get(i).phone + "]"); 
        } 
    } 
    
    private void getWidgets() { 
        txtMsg = (TextView) findViewById(R.id.textView); 
        edtID = (EditText) findViewById(R.id.editTextID); 
        edtName = (EditText) findViewById(R.id.editTextName); 
        edtPhone = (EditText)findViewById(R.id.editTextPhone); 
        btnInsert = (Button) findViewById(R.id.buttonInsert); 
        btnDelete = (Button) findViewById(R.id.buttonDelete); 
        btnUpdate = (Button) findViewById(R.id.buttonUpdate); 
        btnShow = (Button) findViewById(R.id.buttonShow); 
        btnInsert.setOnClickListener(this); 
        btnDelete.setOnClickListener(this); 
        btnUpdate.setOnClickListener(this); 
        btnShow.setOnClickListener(this); 
    } 
    
    // insert 
    private void insert() 
    { 
        txtMsg.setText(""); 
        try 
        { 
         Friend f = new Friend(++id, edtName.getText().toString(), edtPhone.getText().toString()); 
         f.save(); 
         txtMsg.setText("Insert successfully"); 
         txtMsg.append(f.toString()); 
        } 
        catch (Exception e) 
        { 
         txtMsg.setText(e.toString()); 
        } 
    } 
    
    // delete 
    private void delete() 
    { 
        txtMsg.setText(""); 
        int pos = -1; 
        try 
        { 
         try 
         { 
          pos = Integer.parseInt(edtID.getText().toString()); 
         } 
         catch (Exception e) 
         { 
          txtMsg.setText(e.toString()); 
         } 
    
         Friend f = Friend.findById(Friend.class, pos); 
         f.delete(); 
         txtMsg.setText("Delete successfully"); 
        } 
        catch(Exception e) 
        { 
         txtMsg.setText(e.toString()); 
        } 
    } 
    
    // update 
    private void update() 
    { 
        txtMsg.setText(""); 
        long pos = -1; 
        try { 
         try 
         { 
          pos = Long.parseLong(edtID.getText().toString()); 
          Toast.makeText(this, String.valueOf(pos), Toast.LENGTH_SHORT).show(); 
         } 
         catch (Exception e) 
         { 
          txtMsg.setText(e.toString()); 
         } 
    
         Friend f = SugarRecord.findById(Friend.class, pos); 
         f.name = edtName.getText().toString(); 
         f.phone = edtPhone.getText().toString(); 
         f.save(); 
         txtMsg.setText("Update successfully"); 
         txtMsg.append(f.toString()); 
        } 
        catch (Exception e) 
        { 
         txtMsg.setText(e.toString()); 
        } 
    } 
    
    
    // Button click 
    @Override 
    public void onClick(View view) { 
        if (view.getId() == btnShow.getId()) { 
         show(); 
        } else if (view.getId() == btnInsert.getId()) { 
         insert(); 
        } else if (view.getId() == btnUpdate.getId()) { 
         update(); 
        } else if (view.getId() == btnDelete.getId()) { 
         delete(); 
        } 
    } 
    
  • 的問題是,當我嘗試更新,我得到了錯誤。例如,我插入6行像下面

enter image description here

現在我在這裏更新行數2,錯誤!

enter image description here

提前感謝!

回答

1

嘗試改變:

Friend f = SugarRecord.findById(Friend.class, pos); 

到:

Friend f = Friend.findById(Friend.class, pos); 
0

你的問題是在這裏我想: SugarRecord.findById(Friend.class, pos);必須Friend.findById(Friend.class, pos);

問題是指示它索引到一個對象的變量是「沒有對象」,所以試着用Friend.findById。