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

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

11 Comments

Please note the site from which this code is from has a "Copyright © 2020 joshskeen.com". I did not find an actual license.
Yes. I added the reference to the site. I hope that is okay.
I see you have a reference, but.. How to reference material written by others 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.
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.
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.