gpt4 book ai didi

java - SugarORM 更新

转载 作者:行者123 更新时间:2023-12-01 10:01:14 25 4
gpt4 key购买 nike

我正在使用 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

提前致谢!

最佳答案

尝试改变:

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

至:

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

关于java - SugarORM 更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36789172/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com