user1903
user1903

Reputation: 800

Trouble adding items to listview custom BaseAdapter

I know that most of my code might be OK, but I can just add 1 item to the listview. When I try to add more items, pressing the button, nothing happens. The app keeps ok, but doing nothing. I've tried many things and nothing is working.

I'm leaving here some code:

Adapter

public class ConfigSubjectListAdapter extends BaseAdapter {


    private ArrayList<ConfigSubjectListItem> configSubjectItems;
    LayoutInflater inflater;

    public ConfigSubjectListAdapter (Context context, ArrayList<ConfigSubjectListItem> _configSubjectItems){
        this.configSubjectItems = _configSubjectItems;
        inflater = (LayoutInflater)
                context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return configSubjectItems.size();
    }

    @Override
    public Object getItem(int position) {       
        return configSubjectItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        Log.d("ConfigSubjectListAdapter: ", "0");
        final ViewHolderItem viewHolder;

        if (convertView == null) {                    

            Log.d("ConfigSubjectListAdapter: ", "1");

            viewHolder = new ViewHolderItem();

            convertView = inflater.inflate(R.layout.profile_configure_subjects_listitem, null);

            viewHolder.llItem = (LinearLayout) convertView.findViewById(R.id.profile_config_itemlayout);
            viewHolder.rlOptions = (RelativeLayout) convertView.findViewById(R.id.profile_config_rlOptionsLayout);
            viewHolder.textViewNameItem = (TextView) convertView.findViewById(R.id.profile_config_listName);
            viewHolder.textViewCodeItem = (TextView) convertView.findViewById(R.id.profile_config_listCode);
            viewHolder.textViewGroupItem = (TextView) convertView.findViewById(R.id.profile_config_listGroup);
            viewHolder.EditViewItem = (View) convertView.findViewById(R.id.profile_config_colorSubject);
            viewHolder.UpperViewItem = (View) convertView.findViewById(R.id.profile_config_listColored);

            convertView.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolderItem)convertView.getTag();
        }

        viewHolder.textViewNameItem.setText(configSubjectItems.get(position).getName());
        viewHolder.textViewCodeItem.setText(configSubjectItems.get(position).getCode());
        viewHolder.textViewGroupItem.setText(configSubjectItems.get(position).getGroup());
        viewHolder.EditViewItem.setBackgroundColor(configSubjectItems.get(position).getColor());
        viewHolder.UpperViewItem.setBackgroundColor(configSubjectItems.get(position).getColor());

        return convertView;
    }

    static class ViewHolderItem {
        LinearLayout llItem;
        RelativeLayout rlOptions;
        TextView textViewNameItem;
        TextView textViewCodeItem;
        TextView textViewGroupItem;
        View UpperViewItem;
        View EditViewItem;
    }
}

Activity where listview is:

public class ConfigureSubjects extends Activity {

    View colorSelector;
    ImageView addSubjectBtn;

    private ArrayList<ConfigSubjectListItem> confSubItems;
    private ConfigSubjectListAdapter adapter;

    DatabaseHandler db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.profile_configure_subjects);

        final ListView cSubjectList = (ListView) findViewById(R.id.profile_config_listViewSubject);

        confSubItems = new ArrayList<ConfigSubjectListItem>();
        adapter = new ConfigSubjectListAdapter(getApplicationContext(),
                confSubItems);
        cSubjectList.setAdapter(adapter);


        db = new DatabaseHandler(getApplicationContext());

        final TextView _code = (TextView)findViewById(R.id.profile_config_codeSubject);
        final TextView _ects = (TextView)findViewById(R.id.profile_config_ectsSubject);

        addSubjectBtn = (ImageView)findViewById(R.id.profile_config_addButtonSubject);
        addSubjectBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                String _asig = searchSubject.getText().toString();
                String _codigo = (String) _code.getText();

                confSubItems.add(new ConfigSubjectListItem(_asig, _codigo,"M2",Integer.valueOf((String) colorSelector.getTag())));
                Log.d("addSubjectBtn: ","1");
                adapter.notifyDataSetChanged();
                Log.d("addSubjectBtn: ","2");
            }
        });

    }
}

Upvotes: 0

Views: 617

Answers (3)

user1903
user1903

Reputation: 800

I finally found the problem. Nothing to do with code, it was a layout xml problem when declaring my ListView.

Thanks for the answers!

Upvotes: 0

Sudheesh Mohan
Sudheesh Mohan

Reputation: 2769

Put the code(from adding items to arraylist to listview.setadapter) to add items to the listview inside the onClick of the button in Activity. For dynamically adding items use customdialog with edittext inside onclick of button to get input during runtime and inside onclick of OK button of customdialog add the code to add items to arraylist(gettext from edittext of dialog and add to arraylist) , then add item to listview.

So whenever you click on the button, custom dialog with edittext will popup and when you press OK , new item will be added. Hope this will help you.

Upvotes: 0

Nadir Belhaj
Nadir Belhaj

Reputation: 12063

try this trick to refresh your arraylist and notify the Adapter

EDIT,

this solution will work

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            String _asig = searchSubject.getText().toString();
            String _codigo = (String) _code.getText();
            confSubItems.add(new ConfigSubjectListItem(_asig, _codigo,"M2",Integer.valueOf((String)                     colorSelector.getTag())));
            Log.d("addSubjectBtn: ","1");
            adapter = new ConfigSubjectListAdapter(getApplicationContext(),
            confSubItems);
            cSubjectList.setAdapter(adapter);
            adapter.notifyDataSetChanged();
            Log.d("addSubjectBtn: ","2");
        }
    });

Upvotes: 1

Related Questions