我正在创建一个数据库中保存数据的应用程序。但我缺乏使用游标适配器在列表视图中显示数据。不知道应该写什么代码。请检查。
这是我的代码: MainActivity.java
package com.example.employeedetailsnew;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import android.app.Activity;
import android.app.ActionBar;
import android.app.AlertDialog;
import android.app.Fragment;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import android.os.Build;
public class MainActivity extends Activity {
private EditText nme = null;
private EditText num = null;
private EmployeeDatabase empObj;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nme = (EditText) findViewById(R.id.name);
num = (EditText) findViewById(R.id.ageemp);
Button btnShowData = (Button) findViewById(R.id.button1);
btnShowData.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
onSaveClick();
} });
}
public void onButtonClicked(View view) {
Calendar c = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("dd:MMMM:yyyy HH:mm:ss a");
String strDate = sdf.format(c.getTime());
String varName = nme.getText().toString();
String varAge = num.getText().toString();
empObj = new EmployeeDatabase(getApplicationContext());
empObj.insert(varName,varAge,strDate);
AlertDialog alertDialog = null;
alertDialog = new AlertDialog.Builder(MainActivity.this).create();
alertDialog.setTitle("Message");
alertDialog.setMessage("You have been Registered");
alertDialog.setCancelable(true);
alertDialog.show();
nme.setText("");
num.setText("");
}
public void onSaveClick(){
Intent intent = new Intent(this, ShowData.class);
startActivity(intent);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
EmployeeDatabse.java
package com.example.employeedetailsnew;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class EmployeeDatabase extends SQLiteOpenHelper{
EmployeeDatabase (Context context) {
super(context, "empdb.db", null,3);
}
@Override
public void onCreate(SQLiteDatabase database)
{
// create your table here
database.execSQL(
"create table employeedetailnew" +
"(name TEXT , age TEXT, time TEXT)"
);
}
@Override
public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion)
{
database.execSQL("DROP TABLE IF EXISTS EMPLOYEEDETAILNEW");
onCreate(database);
// handle database schema upgrades in here
}
public Cursor getDetails()
{
SQLiteDatabase db = getReadableDatabase();
return db.rawQuery("select name, age from employeedetailnew", null);
}
public void insert(String name, String age, String time)
{
long rowId = 0;
try{
SQLiteDatabase db = getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("name", name);
contentValues.put("age", age);
contentValues.put("time", time);
rowId = db.insert("employeedetailnew", null, contentValues);
}catch(Exception e){
e.printStackTrace();
}
finally{
System.out.println("The rowId is "+rowId);
System.out.println("Name is "+name);
System.out.println("Age is "+age);
System.out.println("Time is "+time);
}
// insert into database here
}
public boolean deleteTitle(String name)
{
// incomplete delete code..:(
SQLiteDatabase db = getWritableDatabase();
return db.delete("employeedetailnew", name + "=" + name, null) > 0;
}
}
EmployeeDetailAdapter.java
package com.example.employeedetailsnew;
import java.util.ArrayList;
import android.content.Context;
import android.database.Cursor;
import android.support.v4.widget.CursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class EmployeeDetailAdapter extends CursorAdapter
{
public EmployeeDetailAdapter(Context context, Cursor c) {
super(context, c);
// TODO Auto-generated constructor stub
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 0;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int index, View view, ViewGroup parent) {
// TODO Auto-generated method stub
if (view == null)
{
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
view = inflater.inflate(R.layout.shows_view, parent, false);
}
EmployeeOther empDtl = times.get(index);
TextView nameTextView = (TextView) view.findViewById(R.id.name1);
nameTextView.setText(empDtl.getName());
TextView ageTextView = (TextView) view.findViewById(R.id.age2);
ageTextView.setText(empDtl.getAge());
TextView timeTextView = (TextView) view.findViewById(R.id.time3);
timeTextView.setText(empDtl.getTime());
return view;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
// TODO Auto-generated method stub
TextView nameTextView = (TextView) view.findViewById(R.id.name1);
nameTextView.setText(cursor.getString(cursor.getColumnIndex("name")));
TextView ageTextView = (TextView) view.findViewById(R.id.name1);
ageTextView.setText(cursor.getString(cursor.getColumnIndex("age")));
TextView timeTextView = (TextView) view.findViewById(R.id.name1);
timeTextView.setText(cursor.getString(cursor.getColumnIndex("time")));
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = LayoutInflater.from (parent.getContext());
View view = inflater.inflate(R.layout.shows_list, parent, false);
return view;
}
}
ShowData.java
package com.example.employeedetailsnew;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class ShowData extends ListActivity
{
private EmployeeDatabase databaseHelper;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EmployeeDatabase empClick = new EmployeeDatabase(getApplicationContext());
Cursor cursor = empClick.getDetails();
if (cursor.moveToFirst())
{
do
{
String name = cursor.getString(1);
String notes = cursor.getString(2);
String time = cursor.getString(3);
// something must be written here
} while (cursor.moveToNext());
}
if (!cursor.isClosed())
{
cursor.close();
}
}
}
activity_main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.employeedetailsnew.MainActivity$PlaceholderFragment" >
<EditText
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/empname"
android:layout_below="@+id/empname"
android:layout_marginTop="19dp"
android:ems="10"
android:inputType="textPersonName" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/sub"
android:layout_below="@+id/sub"
android:layout_marginTop="24dp" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1" />
<TextView
android:id="@+id/empname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="46dp"
android:layout_marginTop="27dp"
android:text="@string/name_of_the_employee" />
<TextView
android:id="@+id/age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/name"
android:layout_below="@+id/name"
android:layout_marginTop="16dp"
android:text="@string/age_of_the_employee" />
<EditText
android:id="@+id/ageemp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/age"
android:layout_below="@+id/age"
android:ems="10"
android:inputType="numberSigned" />
<Button
android:id="@+id/sub"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/button1"
android:layout_centerVertical="true"
android:onClick="onButtonClicked"
android:text="@string/sub" />
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/textView1"
android:layout_centerHorizontal="true"
android:text="@string/show_data" />
</RelativeLayout>
shows_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
shows_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/name1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="@+id/age2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="@+id/time3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
答案 0 :(得分:4)
使用以下代码
更改getDetails()
的{{1}}
EmployeeDatabase
并更改public Cursor getDetails()
{
SQLiteDatabase db = getReadableDatabase();
return db.rawQuery("select rowid _id,name, age,time from employeedetailnew", null);
}
ShowData.java's
,如下所示,
onCreate()
并浏览@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EmployeeDatabase empClick = new EmployeeDatabase(getApplicationContext());
Cursor cursor = empClick.getDetails();
if(cursor !=null)
getListView().setAdapter(
new android.support.v4.widget.SimpleCursorAdapter(EmptyClass.this, R.layout.shows_view, cursor, new String[] {
"name", "age", "time"
}, new int[] {R.id.name1,R.id.age2,R.id.time3}, 0));
}
和this example
答案 1 :(得分:0)
*使用以下代码*更改EmployeeDatabase的getDetails()
public Cursor getDetails() {
SQLiteDatabase db = getReadableDatabase();
return db.rawQuery("select rowid _id,name, age,time from employeedetailnew", null);}
并更改ShowData.java的onCreate(),如下所示
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EmployeeDatabase empClick = new EmployeeDatabase(getApplicationContext());
Cursor cursor = empClick.getDetails();
if(cursor !=null)
getListView().setAdapter(
new android.support.v4.widget.SimpleCursorAdapter(EmptyClass.this, R.layout.shows_view, cursor, new String[] {
"name", "age", "time"
}, new int[] {R.id.name1,R.id.age2,R.id.time3}, 0));
}