Hello Everyone please help me with this code...
Here i am Retriving the Data from the database and displaying it on the listview with checkboxes...
The problem is, I am Retriving the Name of the Students from "attendrecords" table but its not displaying the student's name on listview,instead of its displaying the TextView in place of every name in listview. Below is My Code Please Help... Thanks in Advance..
File First ...
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);
}
});
}
}`
Second File ...
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;
}
}
Third File ....
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;
}
}
Ok , u need to create TextView for each record in your adapter . You are just getting only the check box . More over , you should get data from your attendent list for each row , and then just update the TextView with the name of student (every thing is inside the Adapter)
@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;
}
}
ok so you need to create your own SQLiteOpenHelper class , you can find tutorial for example here . Then you need to provide me some more data , when you want to save data to the datebase ? I'll show you how to do it on every check of CheckBox
, but you should rather do it on button click or something like that it would be more efficient way. I can see that you've got some DateBaseHandler but I'm not aware how your are using it . Please provide some more info , it'd easier to help you then.
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;
}
}
And change your Activity to :
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);
}
});
}
}`
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.