动态地将列表项添加到android中的baseadapter

时间:2014-07-02 04:08:52

标签: java android listview

我想通过按下按钮来动态添加新的列表项。但我没有正确的方法来执行此操作。

BaseAdapter

public class CompetitionResultListAdapter extends BaseAdapter{

    private Activity activity;
    private ArrayList<Result> result;
    DataManager datamanager;


    @Override
    public int getCount() {
        return result!=null ? result.size() : 0;
    }

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView==null){
            // inflate the layout
            LayoutInflater inflater = activity.getLayoutInflater();
            convertView = inflater.inflate(R.layout.list_item_competition_result, parent, false);
        }
        EditText listItemDate = (EditText) convertView.findViewById(R.id.date);
        EditText listItemPlace = (EditText) convertView.findViewById(R.id.place);
        EditText listItemComment = (EditText) convertView.findViewById(R.id.comment);
        result.get(position).setDate(listItemDate.getText().toString());
        result.get(position).setPlace(listItemPlace.getText().toString());
        result.get(position).setComment(listItemComment.getText().toString());


        return convertView;
    }

    public void refreshResultList(ArrayList<Result> result){
        this.result = result;
        notifyDataSetChanged();

    }

}

java class

public class AddCompetitionResult extends Fragment {

    private CompetitionResultListAdapter competitionResultListAdapter;
    private DataManager datamanager;
    ArrayList<Result> result;
    ListView listView;
    private Horse horse;
    private Button addResults;
    int clickCounter=0;

    public AddHorseCompetitionResult(Horse horse) {
        this.horse = horse;
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.add_horse_competition_result,
                null);

        addResults = (Button) view.findViewById(R.id.btn_add_competition_result);

        listView = (ListView) view.findViewById(R.id.data_list);
        // result = datamanager.create(getActivity());

        competitionResultListAdapter = new CompetitionResultListAdapter();

        View footerView = ((LayoutInflater) getActivity().getSystemService(
                Context.LAYOUT_INFLATER_SERVICE)).inflate(
                R.layout.competition_result_listview_footer, null, false);

        listView.addFooterView(footerView);

        listView.setAdapter(competitionResultListAdapter);

        addResults.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String resultId = null;
        //      result.add();
                competitionResultListAdapter.notifyDataSetChanged();

            }
        });

        listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                if (competitionResultListAdapter.getCount() == position) {
                    MainActivity mainActivity = MainActivity.getInstance();
                    mainActivity.setFragment(new AddHorseDetailedInfo(horse),
                            "Add horse more info");
                    // datamanager.createResult(result);

                } else {
                    // ContactGroup cg = (ContactGroup)
                    // competitionResultListAdapter.getItem(position);
                    // MainActivity mainActivity = MainActivity.getInstance();
                    // mainActivity.setFragment(new
                    // ContactListView(cg.getGroupId()), "contacts");
                }
            }

        });

        return view;

    }
}

2 个答案:

答案 0 :(得分:1)

尝试用此替换适配器代码。

public class CompetitionResultListAdapter extends BaseAdapter {

        private Activity activity;
        private ArrayList<Result> result;
        DataManager datamanager;


        @Override
        public int getCount() {
            return result!=null ? result.size() : 0;
        }

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

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if(convertView==null){
                holder = new ViewHolder();
                convertView = LayoutInflater.from(activity).inflate(R.layout.list_item_competition_result, null, false);
                holder.listItemDate = (EditText) convertView.findViewById(R.id.date);
                holder.listItemPlace = (EditText) convertView.findViewById(R.id.place);
                holder.listItemComment = (EditText) convertView.findViewById(R.id.comment);
                convertView.setTag(holder);
            }else{
                holder = (ViewHolder)convertView.getTag();
            }
            holder.listItemDate = (EditText) convertView.findViewById(R.id.date);
            holder.listItemPlace = (EditText) convertView.findViewById(R.id.place);
            holder.listItemComment = (EditText) convertView.findViewById(R.id.comment);

            result.get(position).setDate(listItemDate.getText().toString());
            result.get(position).setPlace(listItemPlace.getText().toString());
            result.get(position).setComment(listItemComment.getText().toString());


            return convertView;
        }

        class ViewHolder {
            EditText listItemDate;
            EditText listItemPlace;
            EditText listItemComment;
        }

        public void refreshResultList(ArrayList<Result> result){
            this.result = result;
            notifyDataSetChanged();

        }
    }

答案 1 :(得分:1)

// on button CLick
{
// toast a meesaage here : onClick()
result.add(urObject)// simply put create a object of ur result type and put "" values
// finallly call  notifyDataSetChanged on adapter
 adapter.notifyDataSetChanged();
}