mercredi 2 mars 2016

How to display checkbox on every entry in a listView

From this Activity i get text from textField and display it in a ListView. Now i want to to add check box on every entry in a listView Cell and also like to know how to display more than one text in a single ListView Cell. Help with code will be appreciated. Here is my code ....

public class AfterRegister extends AppCompatActivity
{
    ListView listView;
    EditText editText;
    Button insertItemButton;
    ArrayList<String> arrayList = new ArrayList<String>();
    ArrayAdapter<String> adapter;
    CheckBox checkBox;

    StoreRegistrationDataBase storeRegistrationDataBase;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_after_register);
        storeRegistrationDataBase = new StoreRegistrationDataBase(this);
        storeRegistrationDataBase = storeRegistrationDataBase.open();

        checkBox = (CheckBox) findViewById(R.id.checkbox);

        insertItemButton = (Button) findViewById(R.id.button4);
        insertItemButton.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                editText = (EditText) findViewById(R.id.editText2);
                listView = (ListView) findViewById(R.id.listView);
                String getEditTextString = editText.getText().toString();

                if(isAlphaNumeric(getEditTextString))
                {
                    if(!getEditTextString.equals(""))

                    {
                        arrayList.add(getEditTextString);

                        adapter = new ArrayAdapter<String>(getBaseContext(), R.layout.text_view_layout, R.id.achView1, arrayList);
                        listView.setAdapter(adapter);



                        adapter.notifyDataSetChanged();
                        editText.setText("");

                    }
                    else
                    {
                        Toast.makeText(AfterRegister.this, "You can not insert empty field", Toast.LENGTH_SHORT).show();
                    }
                }
                else
                {
                    Toast.makeText(AfterRegister.this, "Remove Space", Toast.LENGTH_SHORT).show();
                }
            }
        });
        listView.setOnTouchListener(new View.OnTouchListener()
        {
            @Override
            public boolean onTouch(View v, MotionEvent event)
            {

                return false;
            }
        });
    }

    public boolean isAlphaNumeric(String s)
    {
        String pattern= "^[a-zA-Z0-9]*$";
        if(s.matches(pattern))
        {
            return true;
        }
        return false;
    }
}




Aucun commentaire:

Enregistrer un commentaire