Java 如何从数据库中检索数据并在具有多个复选框的listview中显示?

Java 如何从数据库中检索数据并在具有多个复选框的listview中显示?,java,android,listview,android-listview,Java,Android,Listview,Android Listview,大家好,请帮我翻译这个代码 在这里,我从数据库中检索数据,并使用复选框将其显示在listview上 问题是,我正在从“AttendardRecords”表中检索学生的姓名,但它没有在listview中显示学生的姓名,而不是在listview中显示TextView来代替每个姓名。 下面是我的代码,请帮忙。。。 提前谢谢 先归档 takeattent.java package com.example.myattendance; import java.util.ArrayList; import

大家好,请帮我翻译这个代码

在这里,我从数据库中检索数据,并使用复选框将其显示在listview上

问题是,我正在从“AttendardRecords”表中检索学生的姓名,但它没有在listview中显示学生的姓名,而不是在listview中显示TextView来代替每个姓名。 下面是我的代码,请帮忙。。。 提前谢谢

先归档

takeattent.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);
        }
    });
}
}`
package com.example.myAttention;
导入java.util.ArrayList;
导入android.app.Activity;
导入android.content.Intent;
导入android.os.Bundle;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.widget.ArrayAdapter;
导入android.widget.Button;
导入android.widget.ListView;
导入android.widget.TextView;
导入android.widget.Toast;
公共课堂活动{
公共按钮保存考勤;
公共按钮取消;
公共列表查看参加列表;
公共文本视图文本视图;
公共阵列列表出席列表;
数据库处理程序;
MyAdapter适配器;
//阵列适配器;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
//TODO自动生成的方法存根
super.onCreate(savedInstanceState);
setContentView(右布局,参加);
保存考勤=(按钮)findViewById(R.id.submit_按钮);
cancel=(按钮)findviewbyd(R.id.cancel\u按钮);
take_Attement_list=(ListView)findViewById(R.id.take_Attement_list);
textView=(textView)findViewById(R.id.layout\u textView);
handler=newdatabasehandler(getApplicationContext());
attendList=新的ArrayList();
attendList=handler.getdata();
adapter=新的MyAdapter(takeAttain.this,attendList);
take_attendent_list.setAdapter(适配器);
//适配器=新阵列适配器(此,
//android.R.layout.simple_list_item_多选,attendList);
//take_Attention_list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
//take_attendent_list.setAdapter(适配器);
保存\u attention.setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图arg0){
//TODO自动生成的方法存根
Toast.makeText(takeattent.this,“Saved”,Toast.LENGTH_LONG.show();
//adapter.notifyDataSetChanged();
}
});
cancel.setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图arg0){
//TODO自动生成的方法存根
意向意向=新意向(takeattent.this,nextractivity.class);
星触觉(意向);
}
});
}
}`
第二个文件

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;
}

}
package com.example.myAttention;
导入java.util.ArrayList;
导入java.util.zip.Inflater;
导入android.net.wifi.WifiConfiguration.Status;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.webkit.WebView.FindListener;
导入android.widget.BaseAdapter;
导入android.widget.CheckBox;
导入android.widget.CompoundButton;
导入android.widget.CompoundButton.OnCheckedChangeListener;
导入android.widget.Toast;
公共类MyAdapter扩展了BaseAdapter{
公众参与;
ArrayList护理员;
充气机;
公共MyAdapter(TakeAttain TakeAttain,ArrayList attendList){
//TODO自动生成的构造函数存根
this.takeattain=takeattain;
this.attendList=attendList;
充气机=充气机从(TakeAttain);
}
@凌驾
public int getCount(){
//TODO自动生成的方法存根
int size=attendList.size();
返回大小;
}
@凌驾
公共对象getItem(int arg0){
//TODO自动生成的方法存根
返回null;
}
@凌驾
公共长getItemId(int arg0){
//TODO自动生成的方法存根
返回0;
}
@凌驾
公共视图getView(最终整数位置,视图v,视图组arg2){
//TODO自动生成的方法存根
v=充气机充气(R.layout.U布局,空);
复选框=(复选框)v.findViewById(R.id.layout_复选框);
setOnCheckedChangeListener(新的OnCheckedChangeListener(){
@凌驾
检查更改后的公共无效(复合按钮视图,布尔值已检查){
Toast.makeText(takeattain,“+位置,Toast.LENGTH_SHORT.show();
}
});
返回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;

}

}
package com.example.myAttention;
导入java.util.ArrayList;
导入android.content.ContentValues;
导入android.content.Context;
导入android.database.Cursor;
导入android.database.sqlite.SQLiteDatabase;
导入android.database.sqlite.SQLiteOpenHelper;
公共类DatabaseHandler扩展了SQLiteOpenHelper{
私有静态最终int数据库_VERSION=1;
私有静态最终字符串数据库\u NAME=“newAttention”;
//私有静态最终字符串表\u NAME=“records”;
公共数据库处理程序(上下文){
super(上下文、数据库名称、null、数据库版本);
//TODO自动生成的构造函数存根
}
@凌驾
public void onCreate(SQLiteDatabase db){
//TODO自动生成的方法存根
execSQL(“创建表attndrecords(id整数主键、firstname文本、middlename文本、lastname文本)”;
}
@凌驾
public void onUpgrade(SQLiteDatabase db,int arg1,int arg2){
//TODO自动生成的方法存根
db.execSQL(“如果存在,则删除表:attndrecords”);
onCreate(db);
//db.execSQL(“如果存在,则删除表:“+表名称”);
}
public void savedata(字符串fname、字符串mname、字符串lname){
//TODO自动生成的方法存根
SQLiteDatabase db=this.getReadableDatabase();
ContentValues=新的ContentValues();
value.put(“firstname”,fname);
值。put(“中间名”,mname);
value.put(“lastname”,lname);
db.insert(“attndrecords”,null,值);
db.close();
}
公共ArrayList getdata()
{
SQLiteDatabase db=this.getRea
@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;
}

}
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);
        }
    });
}
}`