0

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();
    
    
                }
            });
        }
Kingg
  • 250
  • 1
  • 12

1 Answers1

1

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.

Jeff
  • 151
  • 2
  • 8
  • Please note the site from which this code is from has a "Copyright © 2020 joshskeen.com". I did not find an actual license. – Scratte Sep 23 '20 at 17:34
  • Yes. I added the reference to the site. I hope that is okay. – Jeff Sep 23 '20 at 17:37
  • I see you have a reference, but.. [How to reference material written by others](https://stackoverflow.com/help/referencing) says "Do not copy the complete text of external sources; instead, use their words and ideas to support your own". On the copyright issue, I'm not a lawyer, but I'm not sure one can use copyrighted material even it one links to it. If it had been copyleft or CC-BY I'm sure there would be no legal problem. – Scratte Sep 23 '20 at 17:39
  • I see. How would you suggest I make changes to this answer without a legal issue? The content of the blog actually solves the OP's issue, which is why I added the link at first. – Jeff Sep 23 '20 at 17:47
  • Unfortunately I'm not an Android developer, so I can't direct you. I'd probably try to modify my solution by trying to do it without looking at the source. You can ping me here if you want my comments gone. – Scratte Sep 23 '20 at 17:52
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/221961/discussion-between-jeff-and-scratte). – Jeff Sep 23 '20 at 18:06
  • @Jeff thanks For ur time and ur Ans Bro But i have Error it says " METHOD DOESNOT OVERRIDE METHODFROM ITS SUPERCLASS " https://i.stack.imgur.com/YlYVv.png – Kingg Sep 25 '20 at 12:12
  • getting Error in `(RadioAdapter.java:35)` **`Attempt to invoke interface method 'int java.util.List.size()' on a null object reference`** on line 35 it was `return mItems.size();` – Kingg Sep 25 '20 at 12:50
  • @Kingg, I have updated my answer to fit in the activity concept you're using. – Jeff Sep 26 '20 at 18:04
  • @Jeff thanks bro, the entered Data was stored into Sqlite database but it doesn’t display in listview. – Kingg Sep 27 '20 at 08:50
  • Have you checked that your list has items, and you can confirm you call `radioAdapter.notifyDataSetChanged()` after adding items to your list? @Kingg – Jeff Sep 28 '20 at 10:27