【问题标题】:How to Retrive Data From The Database and Display in listview with multiple checkboxes?如何从数据库中检索数据并在带有多个复选框的列表视图中显示?
【发布时间】:2016-04-05 19:12:14
【问题描述】:

大家好,请帮我处理这段代码...

我在这里从数据库中检索数据并使用复选框将其显示在列表视图中...

问题是,我正在从“attendrecords”表中检索学生的姓名,但它没有在 listview 上显示学生的姓名,而不是在 listview 中显示 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;

}

}

【问题讨论】:

    标签: java android listview android-listview


    【解决方案1】:

    好的,您需要为适配器中的每条记录创建 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);
            }
        });
    }
    }`
    

    【讨论】:

    • 非常感谢 Hubert 它的工作......你能帮我做一件事吗......我想将所有选定复选框的数据连同当前日期一起保存到数据库中时间...为此我需要做什么?
    • @Pavan 请查看我所做的编辑。由于您没有提供太多信息,因此很难回答,但我希望它会对您有所帮助。询问您是否有任何问题。
    • 详细来说,这是一个考勤系统的迷你项目,其中讲师按下 take_attendance_button,数据库中所有学生的列表将与我们所做的复选框一起显示在列表视图中到目前为止...现在当任何讲师出席时,如果学生在场,他/她将选中复选框,否则为空白....出席后讲师将按下保存按钮...这里我想保存将所有选中和未选中复选框的数据放入表名 studentrecord 中的数据库中,其中包含字段名 id 和状态...
    • 其中 id 代表学生的 id,状态 1 代表学生在场,状态 0 代表学生缺席....就像选中复选框一样,在状态字段 1 中将进入并且如果未选中该框,则在状态字段中将输入 0... 我必须在 save_button 上单击...
    猜你喜欢
    • 1970-01-01
    • 2014-04-24
    • 2014-05-20
    • 2017-02-07
    • 2017-12-15
    • 2017-07-31
    • 1970-01-01
    • 2021-01-05
    • 1970-01-01
    相关资源
    最近更新 更多