您好每个人都可以帮我解决这个问题...
我在这里从数据库中重新获取数据,并在列表视图中显示复选框......
问题是,我正在从“参与记录”表中查找学生姓名,但它没有在列表视图中显示学生的姓名,而是在列表视图中显示TextView代替每个名字。 以下是我的代码请帮助... 在此先感谢..
文件优先......
TakeAttend.java
package com.example.myattendance;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class TakeAttend extends Activity {
public Button save_attendance;
public Button cancel;
public ListView take_attend_list;
public TextView textView;
public ArrayList<String> attendList;
DatabaseHandler handler;
MyAdapter adapter;
//ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.take_attend);
save_attendance = (Button) findViewById(R.id.submit_button);
cancel = (Button) findViewById(R.id.cancel_button);
take_attend_list = (ListView) findViewById(R.id.take_attend_list);
textView = (TextView) findViewById(R.id.layout_textview);
handler = new DatabaseHandler(getApplicationContext());
attendList = new ArrayList<String>();
attendList = handler.getdata();
adapter = new MyAdapter(TakeAttend.this,attendList);
take_attend_list.setAdapter(adapter);
//adapter = new ArrayAdapter<String>(this,
// android.R.layout.simple_list_item_multiple_choice,attendList);
//take_attend_list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
//take_attend_list.setAdapter(adapter);
save_attendance.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Toast.makeText(TakeAttend.this, "Saved", Toast.LENGTH_LONG).show();
//adapter.notifyDataSetChanged();
}
});
cancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(TakeAttend.this,NextActivity.class);
startActivity(intent);
}
});
}
}`
第二档......
... MyAdapter.java
package com.example.myattendance;
import java.util.ArrayList;
import java.util.zip.Inflater;
import android.net.wifi.WifiConfiguration.Status;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Toast;
public class MyAdapter extends BaseAdapter {
public TakeAttend takeAttend;
ArrayList<String> attendList;
LayoutInflater inflater;
public MyAdapter(TakeAttend takeAttend, ArrayList<String> attendList) {
// TODO Auto-generated constructor stub
this.takeAttend = takeAttend;
this.attendList = attendList;
inflater = inflater.from(takeAttend);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
int size = attendList.size();
return size;
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(final int position, View v, ViewGroup arg2) {
// TODO Auto-generated method stub
v=inflater.inflate(R.layout.attend_layout, null);
CheckBox box = (CheckBox) v.findViewById(R.id.layout_checkBox);
box.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Toast.makeText(takeAttend, "" + position,Toast.LENGTH_SHORT).show();
}
});
return v;
}
}
第三档......
DatabaseHandler.java
package com.example.myattendance;
import java.util.ArrayList;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "NewAttendance";
//private static final String TABLE_NAME = "records";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE attndrecords(id INTEGER PRIMARY KEY,firstname TEXT,middlename TEXT,lastname TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXIST : attndrecords");
onCreate(db);
//db.execSQL("DROP TABLE IF EXIST :"+TABLE_NAME);
}
public void savedata(String fname,String mname,String lname) {
// TODO Auto-generated method stub
SQLiteDatabase db = this.getReadableDatabase();
ContentValues values = new ContentValues();
values.put("firstname", fname);
values.put("middlename", mname);
values.put("lastname", lname);
db.insert("attndrecords", null, values);
db.close();
}
public ArrayList<String> getdata()
{
SQLiteDatabase db = this.getReadableDatabase();
ArrayList<String> list = new ArrayList<String>();
Cursor cursor = db.rawQuery("SELECT * FROM attndrecords", null);
//Cursor cursor = db.rawQuery("SELECT firstname,lastname FROM attndrecords", null);
if(cursor.moveToFirst())
{
do
{
list.add(cursor.getString(1));
}while(cursor.moveToNext());
}
return list;
}
}
答案 0 :(得分:0)
好的,您需要为适配器中的每条记录创建TextView。你只是得到复选框。此外,您应该从每个行的关注列表中获取数据,然后只需使用学生名称更新TextView(每个都在适配器内)
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(final int position, View v, ViewGroup arg2) {
// TODO Auto-generated method stub
v=inflater.inflate(R.layout.attend_layout, null);
// Get current value
String currentAttendent = attendList.Get(position);
CheckBox box = (CheckBox) v.findViewById(R.id.layout_checkBox);
// Create TextView
TextView txt = (TextView) v.findVIewByID(R.id.layout_YourTextViewName)
//Set value
txt.SetText(currentAttendent);
box.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Toast.makeText(takeAttend, "" + position,Toast.LENGTH_SHORT).show();
}
});
return v;
}
}
好,所以你需要创建自己的SQLiteOpenHelper类,你可以找到教程for example here。那么当你想将数据保存到数据库时,你需要提供更多的数据吗?
我将在CheckBox
的每次检查中向您展示如何执行此操作,但您应该在按钮单击或其他更有效的方式上执行此操作。我可以看到你有一些DateBaseHandler,但我不知道你是如何使用它的。请提供更多信息,这样可以更方便地为您提供帮助。
public class MyAdapter extends BaseAdapter {
public TakeAttend takeAttend;
ArrayList<String> attendList;
LayoutInflater inflater;
//Add new variables Context and your custom SQLiteOpenHelper
Context context ;
DateBaseHelper DBHelper; // you need to create this class first
public MyAdapter(TakeAttend takeAttend, ArrayList<String> attendList , Context context) {
// TODO Auto-generated constructor stub
this.takeAttend = takeAttend;
this.attendList = attendList;
inflater = inflater.from(takeAttend);
this.context = Context;
DBHelper = new DateBaseHelper(context); // initialize DateBase helper
}
@Override
public int getCount() {
// TODO Auto-generated method stub
int size = attendList.size();
return size;
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(final int position, View v, ViewGroup arg2) {
// TODO Auto-generated method stub
v=inflater.inflate(R.layout.attend_layout, null);
// Get current value
final String currentAttendent = attendList.Get(position);
CheckBox box = (CheckBox) v.findViewById(R.id.layout_checkBox);
// Create TextView
TextView txt = (TextView) v.findVIewByID(R.id.layout_YourTextViewName)
//Setvalue
txt.SetText(currentAttendent);
box.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked == true){
DBHelper.insert(currentAttendent);
}
else{
// it depends on how you'd implement DateBaseHelper
long ID = DBHelper.GetID(currentAttendent);
DBHelper.delete(ID);
}
}
});
return v;
}
}
并将您的活动更改为:
package com.example.myattendance;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class TakeAttend extends Activity {
public Button save_attendance;
public Button cancel;
public ListView take_attend_list;
public TextView textView;
public ArrayList<String> attendList;
DatabaseHandler handler;
MyAdapter adapter;
//ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.take_attend);
save_attendance = (Button) findViewById(R.id.submit_button);
cancel = (Button) findViewById(R.id.cancel_button);
take_attend_list = (ListView) findViewById(R.id.take_attend_list);
textView = (TextView) findViewById(R.id.layout_textview);
handler = new DatabaseHandler(getApplicationContext());
attendList = new ArrayList<String>();
attendList = handler.getdata();
adapter = new MyAdapter(TakeAttend.this,attendList,this); //'this' means context
take_attend_list.setAdapter(adapter);
//adapter = new ArrayAdapter<String>(this,
// android.R.layout.simple_list_item_multiple_choice,attendList);
//take_attend_list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
//take_attend_list.setAdapter(adapter);
save_attendance.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Toast.makeText(TakeAttend.this, "Saved", Toast.LENGTH_LONG).show();
//adapter.notifyDataSetChanged();
}
});
cancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(TakeAttend.this,NextActivity.class);
startActivity(intent);
}
});
}
}`