如何将检索到的数据传递给 editText?
How to pass retrieved data to editText?
大家好。谁能告诉我如何检索从 SQLite
到 editText
的数据?我提到了 Android::Data are not retrieving to EditText field through Database 但它对我不起作用..
DisplayData.java
private void BuildTable(String names,String date1)
{
final String name = names;
final String date=date1;
sqlcon.open();
Cursor c = sqlcon.readEntry(name);
int rows = c.getCount();
int cols = c.getColumnCount();
c.moveToFirst();
TableRow rowDayLabels=new TableRow(this);
TextView weather=new TextView(this);
weather.setText("Weather");
weather.setTypeface(Typeface.SERIF, Typeface.BOLD);
TextView dater=new TextView(this);
dater.setText("Date");
dater.setTypeface(Typeface.SERIF, Typeface.BOLD);
TextView status=new TextView(this);
status.setText("Status");
status.setTypeface(Typeface.SERIF, Typeface.BOLD);
TextView timeIn=new TextView(this);
timeIn.setText("Time In");
timeIn.setTypeface(Typeface.SERIF, Typeface.BOLD);
TextView timeOut=new TextView(this);
timeOut.setText("Time Out");
timeOut.setTypeface(Typeface.SERIF, Typeface.BOLD);
rowDayLabels.addView(weather);
rowDayLabels.addView(dater);
rowDayLabels.addView(status);
rowDayLabels.addView(timeIn);
rowDayLabels.addView(timeOut);
table_layout.addView(rowDayLabels);
// outer for loop
for (int i = 0; i < rows; i++) {
TableRow row = new TableRow(this);
row.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
// inner for loop
for (int j = 0; j < cols; j++) {
TextView tv = new TextView(this);
tv.setLayoutParams(new TableRow.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
));
// tv.setBackgroundResource(R.drawable.cell_shape);
tv.setGravity(Gravity.CENTER);
tv.setTextSize(18);
tv.setPadding(0, 5, 0, 5);
tv.setText(c.getString(j));
row.addView(tv);
row.setBackgroundColor(Color.GREEN);
row.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(DisplayData.this, UpdatePage.class);
intent.putExtra("name", name);
intent.putExtra("date",date);
startActivity(intent);
}
});
}
c.moveToNext();
table_layout.addView(row);
}
sqlcon.close();
}
}
UpdateDetails.java
package com.example.project.project;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import com.example.project.project.API.InfoAPI;
import com.example.project.project.TimeSheet.Details;
import com.example.project.project.TimeSheet.Force;
import com.example.project.project.TimeSheet.Info;
import com.example.project.project.database.MyDatabaseHelper;
public class UpdatePage extends AppCompatActivity {
InfoAPI sqlcon;
private SQLiteDatabase database;
private MyDatabaseHelper dbHelper;
private Cursor c;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dbHelper = new MyDatabaseHelper(this);
setContentView(R.layout.updatepage);
final String name1 = getIntent().getExtras().getString("name");
final String date = getIntent().getExtras().getString("date");
RetrievePage(name1, date);
}
public void RetrievePage(String name, String date) {
final String name2 = name;
final String date2 = date;
final EditText name3 = (EditText) findViewById(R.id.editText9);
final EditText date3 = (EditText) findViewById(R.id.editText12);
name3.setText(name2);
date3.setText(date2);
//final Spinner weather3 = (Spinner) findViewById(R.id.spinner5);
//final Spinner status3 = (Spinner) findViewById(R.id.spinner7);
final EditText subC3 = (EditText) findViewById(R.id.editText17);
final EditText noP = (EditText) findViewById(R.id.editText18);
final EditText noH = (EditText) findViewById(R.id.editText19);
final Spinner poject3 = (Spinner) findViewById(R.id.spinner8);
database = dbHelper.getWritableDatabase();
c = database.rawQuery("SELECT w.Subcontractors, w.NumberOfPerson, w.NumberOfHours FROM Information i LEFT JOIN WorkForce w ON w.TInfo_id = i.ID WHERE i.Name = ? AND i.Date= ? ",
new String[]{String.valueOf(name2),String.valueOf(date2)}, null);
if (c != null) {
c.moveToFirst();
while (c.moveToNext()) {
Info I = new Info();
Details WD = new Details();
// String Weather = c.getString(c.getColumnIndexOrThrow(MyDatabaseHelper.Weather));
//String Status = c.getString(c.getColumnIndexOrThrow(MyDatabaseHelper.Status));
String SubC = c.getString(c.getColumnIndex(MyDatabaseHelper.Subcontractors));
String NoP = c.getString(c.getColumnIndex(MyDatabaseHelper.NumberOfPerson));
String NoH = c.getString(c.getColumnIndex(MyDatabaseHelper.NumberOfHours));
Force WF = new Force();
WF.setSubcontractors(SubC);
WF.setNoOfPerson(NoP);
WF.setNoOfHours(NoH);
subC3.setText(SubC);
noP.setText(NoP);
noH.setText(NoH);
}
}
c.close();
}
}
Force.java
public class Force {
private int id1;
private String subcontractors;
private String noOfPerson;
private String noOfHours;
public void setID(int id1)
{
this.id1=id1;
}
public int getID()
{
return this.id1;
}
public void setSubcontractors(String subcontractors)
{
this.subcontractors=subcontractors;
}
public String getSubcontractors()
{
return this.subcontractors;
}
public void setNoOfPerson(String noOfPerson)
{
this.noOfPerson=noOfPerson;
}
public String getNoOfPerson()
{
return this.noOfPerson;
}
public void setNoOfHours(String noOfHours)
{
this.noOfHours=noOfHours;
}
public String getNoOfHours()
{
return this.noOfHours;
}
}
我错过了什么吗?请告诉我!任何建议都会很棒。谢谢
如果您希望游标中只返回一行,则不应在执行操作之前立即调用 moveTofirst 和 moveToNext。通过立即调用这些函数,您将移动到光标中的第二行。我的猜测是你的游标只有一行,而你在 while 循环中的代码没有被执行。错误可能在这里
if (c != null) {
c.moveToFirst();
while (c.moveToNext()) {
Info I = new Info();
}
}
你应该这样做
if (c != null) {
while (c.moveToNext()) {
Info I = new Info();
}
}
大家好。谁能告诉我如何检索从 SQLite
到 editText
的数据?我提到了 Android::Data are not retrieving to EditText field through Database 但它对我不起作用..
DisplayData.java
private void BuildTable(String names,String date1)
{
final String name = names;
final String date=date1;
sqlcon.open();
Cursor c = sqlcon.readEntry(name);
int rows = c.getCount();
int cols = c.getColumnCount();
c.moveToFirst();
TableRow rowDayLabels=new TableRow(this);
TextView weather=new TextView(this);
weather.setText("Weather");
weather.setTypeface(Typeface.SERIF, Typeface.BOLD);
TextView dater=new TextView(this);
dater.setText("Date");
dater.setTypeface(Typeface.SERIF, Typeface.BOLD);
TextView status=new TextView(this);
status.setText("Status");
status.setTypeface(Typeface.SERIF, Typeface.BOLD);
TextView timeIn=new TextView(this);
timeIn.setText("Time In");
timeIn.setTypeface(Typeface.SERIF, Typeface.BOLD);
TextView timeOut=new TextView(this);
timeOut.setText("Time Out");
timeOut.setTypeface(Typeface.SERIF, Typeface.BOLD);
rowDayLabels.addView(weather);
rowDayLabels.addView(dater);
rowDayLabels.addView(status);
rowDayLabels.addView(timeIn);
rowDayLabels.addView(timeOut);
table_layout.addView(rowDayLabels);
// outer for loop
for (int i = 0; i < rows; i++) {
TableRow row = new TableRow(this);
row.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
// inner for loop
for (int j = 0; j < cols; j++) {
TextView tv = new TextView(this);
tv.setLayoutParams(new TableRow.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
));
// tv.setBackgroundResource(R.drawable.cell_shape);
tv.setGravity(Gravity.CENTER);
tv.setTextSize(18);
tv.setPadding(0, 5, 0, 5);
tv.setText(c.getString(j));
row.addView(tv);
row.setBackgroundColor(Color.GREEN);
row.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(DisplayData.this, UpdatePage.class);
intent.putExtra("name", name);
intent.putExtra("date",date);
startActivity(intent);
}
});
}
c.moveToNext();
table_layout.addView(row);
}
sqlcon.close();
}
}
UpdateDetails.java
package com.example.project.project;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import com.example.project.project.API.InfoAPI;
import com.example.project.project.TimeSheet.Details;
import com.example.project.project.TimeSheet.Force;
import com.example.project.project.TimeSheet.Info;
import com.example.project.project.database.MyDatabaseHelper;
public class UpdatePage extends AppCompatActivity {
InfoAPI sqlcon;
private SQLiteDatabase database;
private MyDatabaseHelper dbHelper;
private Cursor c;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dbHelper = new MyDatabaseHelper(this);
setContentView(R.layout.updatepage);
final String name1 = getIntent().getExtras().getString("name");
final String date = getIntent().getExtras().getString("date");
RetrievePage(name1, date);
}
public void RetrievePage(String name, String date) {
final String name2 = name;
final String date2 = date;
final EditText name3 = (EditText) findViewById(R.id.editText9);
final EditText date3 = (EditText) findViewById(R.id.editText12);
name3.setText(name2);
date3.setText(date2);
//final Spinner weather3 = (Spinner) findViewById(R.id.spinner5);
//final Spinner status3 = (Spinner) findViewById(R.id.spinner7);
final EditText subC3 = (EditText) findViewById(R.id.editText17);
final EditText noP = (EditText) findViewById(R.id.editText18);
final EditText noH = (EditText) findViewById(R.id.editText19);
final Spinner poject3 = (Spinner) findViewById(R.id.spinner8);
database = dbHelper.getWritableDatabase();
c = database.rawQuery("SELECT w.Subcontractors, w.NumberOfPerson, w.NumberOfHours FROM Information i LEFT JOIN WorkForce w ON w.TInfo_id = i.ID WHERE i.Name = ? AND i.Date= ? ",
new String[]{String.valueOf(name2),String.valueOf(date2)}, null);
if (c != null) {
c.moveToFirst();
while (c.moveToNext()) {
Info I = new Info();
Details WD = new Details();
// String Weather = c.getString(c.getColumnIndexOrThrow(MyDatabaseHelper.Weather));
//String Status = c.getString(c.getColumnIndexOrThrow(MyDatabaseHelper.Status));
String SubC = c.getString(c.getColumnIndex(MyDatabaseHelper.Subcontractors));
String NoP = c.getString(c.getColumnIndex(MyDatabaseHelper.NumberOfPerson));
String NoH = c.getString(c.getColumnIndex(MyDatabaseHelper.NumberOfHours));
Force WF = new Force();
WF.setSubcontractors(SubC);
WF.setNoOfPerson(NoP);
WF.setNoOfHours(NoH);
subC3.setText(SubC);
noP.setText(NoP);
noH.setText(NoH);
}
}
c.close();
}
}
Force.java
public class Force {
private int id1;
private String subcontractors;
private String noOfPerson;
private String noOfHours;
public void setID(int id1)
{
this.id1=id1;
}
public int getID()
{
return this.id1;
}
public void setSubcontractors(String subcontractors)
{
this.subcontractors=subcontractors;
}
public String getSubcontractors()
{
return this.subcontractors;
}
public void setNoOfPerson(String noOfPerson)
{
this.noOfPerson=noOfPerson;
}
public String getNoOfPerson()
{
return this.noOfPerson;
}
public void setNoOfHours(String noOfHours)
{
this.noOfHours=noOfHours;
}
public String getNoOfHours()
{
return this.noOfHours;
}
}
我错过了什么吗?请告诉我!任何建议都会很棒。谢谢
如果您希望游标中只返回一行,则不应在执行操作之前立即调用 moveTofirst 和 moveToNext。通过立即调用这些函数,您将移动到光标中的第二行。我的猜测是你的游标只有一行,而你在 while 循环中的代码没有被执行。错误可能在这里
if (c != null) {
c.moveToFirst();
while (c.moveToNext()) {
Info I = new Info();
}
}
你应该这样做
if (c != null) {
while (c.moveToNext()) {
Info I = new Info();
}
}