无法将项目添加到listview自定义BaseAdapter

时间:2014-09-20 11:12:09

标签: android android-listview baseadapter notifydatasetchanged

我知道我的大部分代码都可以,但我可以在listview中添加1个项目。当我尝试添加更多项目时,按下按钮,没有任何反应。该应用程序保持正常,但什么都不做。我尝试了很多东西,没有任何工作。

我要离开这里一些代码:

适配器

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

listview所在的活动:

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

    }
}

3 个答案:

答案 0 :(得分:1)

尝试这个技巧来刷新你的arraylist并通知适配器

EDIT,

此解决方案将起作用

        @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");
        }
    });

答案 1 :(得分:0)

将代码(从添加项目添加到arraylist到listview.setadapter)将项目添加到Activity中按钮的onClick内的listview中。 对于动态添加项目,使用带有edittext的customdialog在按钮内单击以在运行时获取输入,在内部单击onclick的customdialog的OK按钮添加代码以向arraylist添加项目(从对话框的edittext获取gettext并添加到arraylist),然后将项目添加到listview 。

因此,无论何时单击按钮,都会弹出带有edittext的自定义对话框,当您按下确定时,将添加新项目。 希望这会对你有所帮助。

答案 2 :(得分:0)

我终于找到了问题。与代码无关,在声明我的ListView时,它是一个布局xml问题。

感谢您的回答!