我是Android开发的学习者,我在需要你帮助的程序中学习SQLite数据库。
我开发的程序从用户那里获取输入并将值存储在SQLite数据库中,我还制作了一个可点击的文本视图,用于显示存储的数据。
我面临的问题是,当我运行程序并直接点击显示数据(Textview)时,它会打开警报对话框并显示数据,但是当我输入数据时,如果我尝试点击&# 34;显示数据"文本视图被禁用。
请帮忙。
以下是我的XML和Java文件。
XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.junaidmalik.sqlitedemo.SqliteDemo">
<EditText
android:id="@+id/rollnum"
android:hint="Enter Roll Number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:inputType="number"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.503"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.16" />
<EditText
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:inputType="textPersonName"
android:hint="Enter Name"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.503"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/rollnum"
app:layout_constraintVertical_bias="0.018" />
<EditText
android:id="@+id/marks"
android:hint="Enter Marks"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:inputType="number"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.503"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/name"
app:layout_constraintVertical_bias="0.03" />
<Button
android:id="@+id/btnupdt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Update"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/marks"
app:layout_constraintVertical_bias="0.125" />
<TextView
android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
android:id="@+id/search"
android:clickable="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Show Data >>"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnupdt" />
</android.support.constraint.ConstraintLayout>
以下是Java文件
package com.example.junaidmalik.sqlitedemo;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class SqliteDemo extends AppCompatActivity {
EditText et1, et2, et3;
TextView search;
SqliteDemoHelper myData;
Button btnupdt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sqlite_demo);
et1 = (EditText) findViewById(R.id.rollnum);
et2 = (EditText) findViewById(R.id.name);
et3 = (EditText) findViewById(R.id.marks);
search = (TextView) findViewById(R.id.search);
btnupdt = (Button) findViewById(R.id.btnupdt);
btnupdt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int rn = Integer.parseInt(et1.getText().toString());
int mks = Integer.parseInt(et3.getText().toString());
String name = (String) et2.getText().toString();
SqliteDemoHelper demoHelper = new SqliteDemoHelper(SqliteDemo.this);
SQLiteDatabase db = demoHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("sid", rn);
values.put("sname", name);
values.put("marks", mks);
long row = db.insert("Students", null, values);
demoHelper.getReadableDatabase();
String projection[] = {"sid", "sname", "marks"};
Cursor c = db.query("Students", projection, null, null, null, null, null);
c.moveToLast();
System.out.println("Name is : " + c.getString(1) + " and " + "Roll Number is :" + c.getString(0));
Toast.makeText(SqliteDemo.this, "Database Updated", Toast.LENGTH_SHORT).show();
setContentView(R.layout.activity_sqlite_demo);
}
});
search.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
setContentView(R.layout.activity_sqlite_demo);
SqliteDemoHelper demoHelper = new SqliteDemoHelper(SqliteDemo.this);
SQLiteDatabase db = demoHelper.getWritableDatabase();
Cursor res = db.rawQuery("Select * from Students",null);
if (res.getCount() == 0) {
showMessage("Error", "Nothing Found");
return;
}
StringBuffer buffer = new StringBuffer();
while (res.moveToNext()) {
buffer.append("sid :" + res.getString(0) + "\n");
buffer.append("name : " + res.getString(1) + "\n");
buffer.append("Marks :" + res.getString(2) + "\n\n");
}
showMessage("Data is - ", buffer.toString());
}
});}
public void showMessage(String title, String message) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(message);
builder.show();
}
}
答案 0 :(得分:0)
search.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
setContentView(R.layout.activity_sqlite_demo);
....
..
}});
从搜索点击事件中删除setcontentView。
如果再次调用方法setcontentview,可能在布局中查看activity_sqlite_demo需要重新信任。