这是我的MainActivity.java。我试图在mainactivity中显示它,但我无法获取数据。
public class MainActivity extends AppCompatActivity {
FloatingActionButton f;
ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BackgroundTask backgroundTask = new BackgroundTask( this);
backgroundTask.execute("get_info");
f=(FloatingActionButton)findViewById(R.id.fb);
f.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent a=new Intent(MainActivity.this,SecondActivity.class);
startActivity(a);
overridePendingTransition(R.anim.push_up, R.anim.push_down);
}
});
}
}
这是BackgroundTask:
class BackgroundTask extends AsyncTask<String,Listitems,String> {
DatabaseHelper myDb;
Context ctx;
Adapter adapter;
Activity activity;
ListView listView;
BackgroundTask(Context ctx)
{
this.ctx= ctx;
activity =(Activity)ctx;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
String method=params[0];
DatabaseHelper databasehelper =new DatabaseHelper(ctx);
if(method.equals("add_info")){
String name=params[1];
String time=params[2];
String date=params[3];
SQLiteDatabase db = databasehelper.getWritableDatabase();
databasehelper.insertData(db,name,time,date);
return "Inserted....";
}
else if(method.equals("get_info"))
{
listView=(ListView)activity.findViewById(R.id.hlist);
Cursor cursor =databasehelper.getalldata();
adapter =new Adapter(ctx,R.layout.adapter_layout);
String id;
String name;
String time;
String date;
while (cursor.moveToNext()){
id=cursor.getString(cursor.getColumnIndex(DatabaseHelper.S1));
name=cursor.getString(cursor.getColumnIndex(DatabaseHelper.S2));
time=cursor.getString(cursor.getColumnIndex(DatabaseHelper.S3));
date=cursor.getString(cursor.getColumnIndex(DatabaseHelper.S4));
Listitems list= new Listitems(id,name,time,date);
publishProgress(list);
}
}
return "get_info";
}
@Override
protected void onProgressUpdate(Listitems... values) {
adapter.add(values[0]);
}
@Override
protected void onPostExecute(String result) {
if(result.equals("get_info")){
listView.setAdapter(adapter);
}
else {
Toast.makeText(ctx,result,Toast.LENGTH_LONG).show();
}
}
}
这是我的DatabaseHelper.java:
package com.example.hems.todolist;
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "Notes.db";
public static final String TABLE_NAME = "Notes";
public static final String S1 = "ID";
public static final String S2 = "Task_Name";
public static final String S3 ="Task_Time";
public static final String S4 ="Task_Date";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("Create Table " + TABLE_NAME + "(" + S1 + " integer primary key AUTOINCREMENT," + S2 + " text," + S3 + " time," + S4 + " date)");
Log.d("Database Operations","Table Inserted Successfully");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public long insertData(SQLiteDatabase db, String name, String time, String date) {
db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(S2, name);
contentValues.put(S3, time);
contentValues.put(S4, date);
long result = db.insert(TABLE_NAME, null, contentValues);
Log.d("Database Operations","Inserted Successfully");
return result ;
}
public Cursor getalldata( ) {
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery(" select * from " + TABLE_NAME, null);
return res;
}
}
Listitems.java:
public class Listitems {
private String id,name;
private String time;
private String date;
public Listitems(String id, String name, String time, String date){
this.setId(id);
this.setName(name);
this.setTime(time);
this.setDate(date);
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
}
Adapter.java:
public class Adapter extends ArrayAdapter {
List list =new ArrayList();
public Adapter(Context context, int resource) {
super(context, resource);
}
public void add(Listitems object) {
list.add(object);
super.add(object);
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ListitemsHolder listitemsHolder;
if (row == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = layoutInflater.inflate(R.layout.adapter_layout, parent, false);
listitemsHolder = new ListitemsHolder();
listitemsHolder.h1 = (TextView) row.findViewById(R.id.r1);
listitemsHolder.h2 = (TextView) row.findViewById(R.id.r2);
listitemsHolder.h3 = (TextView) row.findViewById(R.id.r3);
listitemsHolder.h4 = (TextView) row.findViewById(R.id.r4);
row.setTag(listitemsHolder);
}
else {
listitemsHolder=(ListitemsHolder)row.getTag();
}
Listitems listitems=(Listitems)getItem(position);
listitemsHolder.h1.setText(listitems.getId());
listitemsHolder.h2.setText(listitems.getName());
listitemsHolder.h3.setText(listitems.getTime());
listitemsHolder.h4.setText(listitems.getDate());
return row;
}
static class ListitemsHolder{
TextView h1, h2,h3,h4;
}
}
我得到listview的布局,但不是我输入的数据是空白的。所以帮我获取数据。我在第二个活动中输入数据我觉得你不需要!!