首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >单击文本视图时编辑SQLite数据

单击文本视图时编辑SQLite数据
EN

Stack Overflow用户
提问于 2019-03-31 16:17:29
回答 1查看 88关注 0票数 1

我想通过单击动态创建的文本视图来更新SQLite数据库中的数据。我的数据库显示了学期的名称、开始日期和结束日期。每个文本视图显示一个学期的数据。当我单击ID Textview时,我想转到一个不同的屏幕,在那里用户可以编辑该特定术语的数据。问题是我不知道如何将文本视图连接到术语ID。文本视图是动态创建的。当单击某个术语的ID文本视图时,用户将转到一个新屏幕,在此她可以编辑现有数据。但是,因为SetText覆盖了之前的术语,所以EditText值只显示最后一个术语。如果我使用append,两个ID都会填写在EditText中。我只想在编辑屏幕上显示一个术语的数据,并且我想要与用户单击的Textview ID相对应的术语。这是用户点击他们想要编辑的术语的代码。ID Textview具有将用户带到下一个屏幕的onclickListeners,在该屏幕中,EditText被所选术语数据填充(这就是它的工作方式) First block of code shows the first screen where the user clicks on the textvew that shows the id of each term

代码语言:javascript
复制
public void ViewAll() {
                         Cursor res = myDb.getAllData();
        if(res.getCount() == 0) {
                             showMessage("Error", "Nothing Found");
                             return;
                        }
                       StringBuffer buffer = new StringBuffer();
                         while(res.moveToNext()) {
                             final LinearLayout layout = (LinearLayout ) findViewById(R.id.LinearLayOut);
                             final TextView textView1 = new TextView(this);
                             final TextView textView2 = new TextView(this);
                             final TextView textView3 = new TextView(this);
                             final TextView textView4 = new TextView(this);
                             textView1.append(res.getInt(0)+"\n");
                             textView2.append("Term Title : "+ res.getString(1)+"\n");
                             textView3.append("Start Date : "+ res.getString(2)+"\n");
                             textView4.append("End Date : "+ res.getString(3)+"\n");
                             textView1.setOnClickListener(
                                   new View.OnClickListener() {
                                         @Override
                                         public void onClick(View v) {
                                             Intent myIntent = new Intent(ListTerms.this, EditTerms.class);
                                             startActivity(myIntent);
                                         }
                                     }
                             );
                             layout.addView(textView1);
                             layout.addView(textView2);
                             layout.addView(textView3);
                             layout.addView(textView4);
                             }
    }      public void onClick(View v) {
                                             Intent myIntent = new Intent(ListTerms.this, EditTerms.class);
                                             startActivity(myIntent);
                                         }
                                     }
                             );
                             layout.addView(textView1);
                             layout.addView(textView2);
                             layout.addView(textView3);
                             layout.addView(textView4);
                             }
代码语言:javascript
复制
public void UpdateData() {
        saveTermInfoBtn.setOnClickListener(
                    new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            boolean isUpdate = myDb.updateTerm(termId.getText().toString(), inputTerm.getText().toString(), inputStart.getText().toString(), inputEnd.getText().toString());
                            if (isUpdate == true) {
                                Toast.makeText(EditTerms.this, "Data Update", Toast.LENGTH_LONG).show();
                            } else {
                                Toast.makeText(EditTerms.this, "Data Not Updated", Toast.LENGTH_LONG).show();
                            }
                        }
                    }
            );
        }
    public void ViewAll() {
                        Cursor res = myDb.getAllData();
                        if(res.getCount() == 0) {
                            // show message
                            showMessage("Error","Nothing found");
                            return;
                        }
                        StringBuffer buffer = new StringBuffer();
                        while (res.moveToNext()) {
                                termId.setText(res.getString(0) + "\n");
                                inputTerm.setText(res.getString(1) + "\n");
                                inputStart.setText(res.getString(2) + "\n");
                                inputEnd.setText(res.getString(3) + "\n\n");
                                 }
                        // Show all data
                        showMessage("Data",buffer.toString());
    }

If I use setText only the data of the last term gets displayed If I use append the data of all the terms gets displayed.

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-01 08:57:33

我相信这做了您想要做的(而不是更新一行Toast的id列,然后可以通过intent和提取的数据传递)。

它利用每个TextView的标记来存储相应的id

使用DatabaseHelper.java的DatabaseHelper:

代码语言:javascript
复制
public class DatabaseHelper extends SQLiteOpenHelper {

    public static final String DBNAME = "school.db";
    public static final int DBVERSION = 1;

    public static final String TBL_TERM = "term";
    public static final String COL_TERM_ID = BaseColumns._ID;
    public static final String COL_TERM_TITLE = "title";
    public static final String COL_TERM_STARTDATE = "startdate";
    public static final String COL_TERM_ENDDATE = "enddate";

    SQLiteDatabase mDB;

    public DatabaseHelper(Context context) {
        super(context, DBNAME, null, 1);
        mDB = this.getWritableDatabase();
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String crt_term_sql = "CREATE TABLE IF NOT EXISTS " + TBL_TERM + "(" +
                COL_TERM_ID + " INTEGER PRIMARY KEY, " +
                COL_TERM_TITLE + " TEXT, " +
                COL_TERM_STARTDATE + " TEXT," +
                COL_TERM_ENDDATE + " TEXT" +
                ")";
        db.execSQL(crt_term_sql);
    }

    public long addTerm(String title, String startdate, String enddate) {
        ContentValues cv = new ContentValues();
        cv.put(COL_TERM_TITLE,title);
        cv.put(COL_TERM_STARTDATE,startdate);
        cv.put(COL_TERM_ENDDATE,enddate);
        return mDB.insert(TBL_TERM,null,cv);
    }

    public Cursor getAllData() {
        return mDB.query(TBL_TERM,null,null,null,null,null,COL_TERM_STARTDATE + " ASC");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}

活动内容:-

代码语言:javascript
复制
public class MainActivity extends AppCompatActivity {

    LinearLayout layout;
    DatabaseHelper myDB;
    Cursor mCsr;
    Context mContext;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        layout = this.findViewById(R.id.LinearLayout);
        mContext = this;
        myDB = new DatabaseHelper(this);
        addSomeData();

        mCsr = myDB.getAllData();
        while (mCsr.moveToNext()) {
            LinearLayout ll = new LinearLayout(this);
            ll.setOrientation(LinearLayout.HORIZONTAL);
            long currentID = mCsr.getLong(mCsr.getColumnIndex(DatabaseHelper.COL_TERM_ID));
            addtextView(ll,currentID,mCsr.getString(mCsr.getColumnIndex(DatabaseHelper.COL_TERM_ID)) + "   ");
            addtextView(ll,currentID,mCsr.getString(mCsr.getColumnIndex(DatabaseHelper.COL_TERM_TITLE))+ "   ");
            addtextView(ll,currentID,mCsr.getString(mCsr.getColumnIndex(DatabaseHelper.COL_TERM_STARTDATE))+ "   ");
            addtextView(ll, currentID,mCsr.getString(mCsr.getColumnIndex(DatabaseHelper.COL_TERM_ENDDATE))+ "   ");
            layout.addView(ll);
        }
    }

    private void addtextView(LinearLayout ll, long tag, String data) {
        TextView current_tv = new TextView(this);
        current_tv.setTag(String.valueOf(tag)); //<<<<<<<<<< SETS THE TAG with current_id
        current_tv.setText(data);
        ll.addView(current_tv);
        current_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                long id = Long.valueOf((String)v.getTag()); //<<<<<<<<<< RETRIEVES THE id FROM THE TAG
                Toast.makeText(mContext,"You clicked on ID " + String.valueOf(id),Toast.LENGTH_SHORT).show();
            }
        });
    }

    // JUST TO ADD SOME DATA FOR TESTING (if none exists)
    private void addSomeData() {
        if (DatabaseUtils.queryNumEntries(myDB.getWritableDatabase(),DatabaseHelper.TBL_TERM) < 1) {
            myDB.addTerm("Term 1","2019-01-01","2019-03-31");
            myDB.addTerm("Term 2","2019-04-01","2019-06-30");
            myDB.addTerm("Term 3","2019-07-01","2019-08-31");
            myDB.addTerm("Term 4","2019-09-01","2019-12-31");
        }
    }
}

  • 请注意,如果您在层次结构较低的活动中添加/删除/编辑行,则需要重新构建视图。
  • 已为每行添加了具有水平方向的LinearLayout。主线布局设置为垂直orientation.

替代方案(典型解决方案)

但是,更容易管理的是ListView (特别是关于更新的数据)。也许可以考虑以下几点作为替代方案。

在活动(本例中为Main2Activity)的布局中,添加一个ListView,例如activity_main2.xml is :-

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Main2Activity">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Terms"
        android:layout_marginBottom="20dp"/>

    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </ListView>
</LinearLayout>

因为你想要3个TextViews (id不是真正的用户友好,因为它没有任何意义,甚至可能会令人困惑),然后列表的每一行的另一种布局,例如termlist.xml :-

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView
        android:id="@+id/title"
        android:layout_width="0dp"
        android:layout_weight="6"
        android:layout_height="match_parent" />
    <TextView
        android:id="@+id/startdate"
        android:layout_width="0dp"
        android:layout_weight="2"
        android:layout_height="match_parent" />
    <TextView
        android:id="@+id/enddate"
        android:layout_width="0dp"
        android:layout_weight="2"
        android:layout_height="match_parent" />
</LinearLayout>

DatabaseHelper DatabaseJelper.java保持不变。

MainActivity2.java (作为MainActivity的替代,以便可以检查这两个活动) :-

代码语言:javascript
复制
public class Main2Activity extends AppCompatActivity {

    DatabaseHelper myDB;
    ListView mylistview;
    SimpleCursorAdapter sca;
    Cursor mCsr;
    Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        mContext = this;
        mylistview = this.findViewById(R.id.listview);
        myDB = new DatabaseHelper(this);
        addSomeData();
        manageListView();
    }

    private void manageListView() {
        mCsr = myDB.getAllData();
        if (sca == null) {
            sca = new SimpleCursorAdapter(
                    this,
                    R.layout.termlist,mCsr,
                    new String[]{DatabaseHelper.COL_TERM_TITLE,DatabaseHelper.COL_TERM_STARTDATE,DatabaseHelper.COL_TERM_ENDDATE},
                    new int[]{R.id.title,R.id.startdate,R.id.enddate},
                    0
            );
            mylistview.setAdapter(sca);
            mylistview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Toast.makeText(mContext,"Your clicked on ID " + String.valueOf(id),Toast.LENGTH_SHORT).show();
                }
            });
        } else {
            sca.swapCursor(mCsr);
        }
    }

    // JUST TO ADD SOME TEST DATA IF NOE EXISTS
    private void addSomeData() {
        if (DatabaseUtils.queryNumEntries(myDB.getWritableDatabase(),DatabaseHelper.TBL_TERM) < 1) {
            myDB.addTerm("Term 1","2019-01-01","2019-03-31");
            myDB.addTerm("Term 2","2019-04-01","2019-06-30");
            myDB.addTerm("Term 3","2019-07-01","2019-08-31");
            myDB.addTerm("Term 4","2019-09-01","2019-12-31");
        }
    }

    //ADDED as simply calling manageListView will rebuild the list according to the data
    //  as such when the activity is returned to from another the List is rebuilt
    // (basically the Cursor mCsr is overwritten and then as the SimpleCursorAdapter sca is instantiated
    //  the Cursor is swappped)
    @Override
    protected void onResume() {
        super.onResume();
        manageListView();
    }
}

结果

Fisrt (动态TextViews):

另一种选择是:-

在这两种情况下,单击一行都会导致id被吐司(而不是费力地去捕捉Toast)。

    • 都已在安卓10

    上进行了测试

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55439022

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档