单击textview时编辑SQLite数据

时间:2019-03-31 08:17:29

标签: java android sqlite

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

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

1 个答案:

答案 0 :(得分:1)

我相信这可以满足您的要求(而不是更新Toast的id列,而是可以通过意图和提取的数据进行传递)。

它利用每个TextView的标签来存储各自的 id

DatabaseHelper使用了 DatabaseHelper.java :-

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) {

    }
}

活动:-

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。主要的Linearlayout设置为垂直方向。

替代(典型解决方案)

但是,ListView更易于管理(尤其是有关更新数据)。也许考虑以下方法。

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

<?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个TextView(id并不是真正的用户友好的,因为它没有任何意义,甚至可能造成混乱),然后为列表的每一行提供另一个布局,例如 termlist.xml :-

<?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,以便可以同时检查两个活动):-

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):-

enter image description here

Alternative结果为:-

enter image description here

在两种情况下,单击一行都会导致ID被烘烤(而不是试图赶上Toast的努力)。

  • 两个都已经在Android 10上签出