hmkcode / Android

Android related examples
3.52k stars 3.41k forks source link

SQLITE #13

Open ryanmunene79 opened 8 years ago

ryanmunene79 commented 8 years ago

how would you use the custom adapter if you are fetching data from the db for the first textview and instead of the second textview,you use an edittext which should set data to the db

ishtdeephora commented 6 years ago

Hi @ryanmunene79 . For this case, you can use the combination of ArrayAdapter and DialogFragment. List down the first TextView using the array adapter. To edit the particular item in the list, you can add item.onClickListener and open a Dialog to show the editText with a prefilled setText to the item. Once you change the editText, the changes can be maintained at the db level.

Because when you define your custom layout you will add an editText in your case, which may have only one ID and if you target this editText, the changes will always go for the first item in the list but not the corresponding TextView.

ChinmaySahu1357 commented 10 months ago

1 .Create a Custom Adapter Class: Create a new Java/Kotlin class that extends ArrayAdapter or BaseAdapter public class CustomAdapter extends ArrayAdapter { // Constructor and methods }

  1. getView Method: public View getView(int position, View convertView, ViewGroup parent) { // Inflate your layout for each item View view = LayoutInflater.from(getContext()).inflate(R.layout.item_layout, parent, false);

    // Get references to your TextView and EditText TextView textView = view.findViewById(R.id.text_view); EditText editText = view.findViewById(R.id.edit_text);

    // Get the data for this position YourDataType data = getItem(position);

    // Set data to the TextView textView.setText(data.getText()); // Assuming 'getText()' is a method in YourDataType

    // Set data to the EditText editText.setText(data.getEditTextValue()); // Assuming 'getEditTextValue()' is a method in YourDataType

    return view; }

  2. Create your Layout : XML
  3. Use the Custom Adapter in Your Activity: CustomAdapter adapter = new CustomAdapter(this, R.layout.item_layout, yourDataList); ListView listView = findViewById(R.id.list_view); // Assuming you have a ListView with this id listView.setAdapter(adapter);
  4. Handle EditText Changes:
  5. Update the DataBase: on textChange method