Kingg
Kingg

Reputation: 270

Android : How to display a Sqlite Database With CheckBox in listview Automatically

In my Application userinput data from Diaglogbox was Stored into the Sqlite and want to Display those datas in listview along With Checkbox

Example :

enter image description here

I Stored Userinput in sqlite but i dont know how to Display those saved names along with Checkbox in In Listview

How to Achieve that i am new to Android !

Database :

//Display these Datas in Listview Along With CheckBox

 // Table 2
    private static final String TABLE2_NAME = "listitem_name";
    public static final String COLUMN1_ID = "I_ID";
    public static final String COLUMN2_TITLE = "LISTITEMS_NAME";

 onCreate(){
 String query1 =
                "CREATE TABLE IF NOT EXISTS " + TABLE2_NAME + "("
                        + COLUMN1_ID + " INTEGER PRIMARY KEY ,"
                        + COLUMN2_TITLE + "  TEXT ,"
                        +  COLUMN_ID + " INTEGER, " + "FOREIGN KEY("+
                        COLUMN_ID +") "
                        + "REFERENCES " + TABLE_NAME +"("+COLUMN_ID +")"+ ");";

        sqLiteDatabase.execSQL(query1);
    }

     Cursor readlistAllData() {
            String query = "SELECT * FROM " + TABLE2_NAME;
            SQLiteDatabase db = this.getReadableDatabase();
    
            Cursor cursor = null;
            if (db != null) {
                cursor = db.rawQuery(query, null);
            }
            return cursor;
        }

ActivityClass

 public class AddItems extends AppCompatActivity {
    
        Toolbar mToolbar;
        DatabaseHelper myDB;
        ArrayList<String> listitems;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
    
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_add_items);
    
            mToolbar = findViewById(R.id.toolbar);
            setSupportActionBar(mToolbar);
    
            FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab_button);
    
            fab.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
    
                    ShowPopup();
    
                }
            });
    
            myDB = new DatabaseHelper(AddItems.this);
    
            listitems = new ArrayList<>();
    
            DisplayList();
    
        }
    
        private void DisplayList(){
    
            Cursor cursor = myDB.readlistAllData();
            if (cursor.getCount() == 0) {
    
                Toast.makeText(this, "No Data.", Toast.LENGTH_SHORT).show();
    
            } else {
                while (cursor.moveToNext()) {
    
                    listitems.add(cursor.getString(1));
                }
            }
        }
    
        private void ShowPopup() {
            final Dialog dialog = new Dialog(this);
            dialog.setContentView(R.layout.custom_dialog);
            dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            dialog.show();
            final EditText lname = dialog.findViewById(R.id.list_Edit_txt);
            Button add = dialog.findViewById(R.id.add);
            Button cancel = dialog.findViewById(R.id.cancel);
            cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    dialog.dismiss();
                }
            });
    
            add.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    // Toast.makeText(AddItems.this, "add called", Toast.LENGTH_SHORT).show();
    
                    String name = lname.getText().toString();
                    if (!TextUtils.isEmpty(lname.getText().toString())) {
                        DatabaseHelper db = new DatabaseHelper(getApplicationContext());
                        db.itemlist(name);
                        Toast.makeText(AddItems.this, "Added Sucessfully !", Toast.LENGTH_SHORT).show();
                        ShowPopup();
    
                    } else
                        Toast.makeText(AddItems.this, "The name cannot be empty!", Toast.LENGTH_LONG).show();
    
    
                }
            });
        }

Upvotes: 0

Views: 202

Answers (1)

Jeff
Jeff

Reputation: 161

There is a ListAdapter class you can use to make this work. Also, you can use RecyclerView with a RecyclerView Adapter. Using a RecyclerView:

joshskeen.com provided a step-by-step approach using a RecyclerView in http://joshskeen.com/building-a-radiogroup-recyclerview/

Below is the adapted code to fit in your scenario.

RadioAdapter.java

public class RadioAdapter extends RecyclerView.Adapter<RadioAdapter.ViewHolder> {  
    public int mSelectedItem = -1;
    public List<String> mItems;
    private Context mContext;

    public RadioAdapter(Context context, List<String> items) {
        mContext = context;
        mItems = items;
    }

    @Override
    public void onBindViewHolder(RadioAdapter.ViewHolder viewHolder, final int i) {
        viewHolder.mRadio.setChecked(i == mSelectedItem);
        viewHolder.mText.setText(mItems.get(i));
    }

    @Override
    public int getItemCount() {
        return mItems.size();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        final View view = inflater.inflate(R.layout.list_view_item, viewGroup, false);
        return new ViewHolder(view);
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        public RadioButton mRadio;
        public TextView mText;

        public ViewHolder(final View inflate) {
            super(inflate);
            mText = (TextView) inflate.findViewById(R.id.text);
            mRadio = (RadioButton) inflate.findViewById(R.id.radio);
            View.OnClickListener clickListener = new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mSelectedItem = getAdapterPosition();
                    notifyDataSetChanged();
                }
            };
            itemView.setOnClickListener(clickListener);
            mRadio.setOnClickListener(clickListener);
        }
    }
}

list_view_item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="horizontal">

    <RadioButton
        android:id="@+id/radio"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
</LinearLayout>

In AddItems.java class

RadioAdapter RadioAdapter;
List<String> listItems;

@Override
protected final void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    RecyclerView recyclerView = (RecyclerView)findViewById(R.id.recycler_view);
    
    recyclerView.setLayoutManager(new LinearLayoutManager(this)); 
    listItems = new ArrayList<String>();
    radioAdapter = new RadioAdapter(this, listItems)  
    recyclerView.setAdapter(radioAdapter);

}

private void DisplayList(){
 Cursor cursor = myDB.readlistAllData();
 if (cursor.getCount() == 0) {
    Toast.makeText(this, "No Data.", Toast.LENGTH_SHORT).show();
   } else {
      while (cursor.moveToNext()) {
         listitems.add(cursor.getString(1));
      }
      radioAdapter.notifyDataSetChanged();
    }
 }
}

Reference: http://joshskeen.com/building-a-radiogroup-recyclerview/

Below are some other useful links:

Let me know if you need more help.

Upvotes: 1

Related Questions