开发者

Adding a new row on button click to a list view

I want to add a new row on clicking add button in a list view in android.

Can someone help me out?

Here is the code I have so far:

public class prsnlhstry<EventArgs> extends Activity implements OnItemClickListener {

    public static ArrayList<String> arr_sort_textview1= null; 
    public static ArrayList<String> arr_sort_textview2=null;
    public static ArrayList<String> arr_sort_textview3=null;
    public Resources ApptResources= null;
    private ContextWrapper mycontext;
    tododata todo;
    int clickcounter = 0;



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ListView lv= (ListView)findViewById(R.id.listview);
        todo = new tododata(this);

        List<listObject> List = new ArrayList<listObject>();
             for(int i = 0; i <= 5; i++){
                listObject lo = new listObject();
                lo.grade =  Integer.toString(i);
                lo.reason = Integer.toString(i);
                lo.school = Integer.toString(i);
                List.add(lo);
             }

        CustomAdapter ca = new CustomAdapter(this); 
        lv = (ListView)findViewById(R.id.listview);
        lv.setAdapter(ca);
        lv.setOnItemClickListener(this);
        ca.notifyDataSetChanged();



        Button button = (Button) findViewById(R.id.btn);
        button.setOnClickListener(new OnClickListener(){
        public void onClick(View v) {
               Toast.makeText(prsnlhstry.this, "Row Added Successfully", Toast.LENGTH_SHORT).show();
            }
        });

   }  

    private class listObject {
    String grade;
    String school; 
    String reason;
    }
    public class CustomAdapter extends BaseAdapter {
        private LayoutInflater mInflater;

          public CustomAdapter(Context context) {

            mInflater = LayoutInflater.from(context);

                 }

          @Override
         public int getCount() {
              return todo.getLength();
        }
          @Override
            public Object getItem(int position) {
            return position;
        }
          @Override
            public long getItemId(int position) {
            return position;
        }
          @Override
            public View getView(int position, View convertView, ViewGroup parent) {

            ViewHolder holder;
            String temp = null;

            // to reinflate it. We only inflate a new View when the convertView supplied
            // by ListView is null.
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.grid_item, null);

           // Creates a ViewHolder and store references to the views
           // we want to bind data to.
                    holder = new ViewHolder();
                holder.textview1 = (TextView) convertView.findViewById(R.id.item2);
                holder.textview2 = (TextView) convertView.findViewById(R.id.item3);
                holder.textview3 = (TextView) convertView.findViewById(R.id.item4);


                convertView.setTag(holder);
            } else {

                holder = (ViewHolder) convertView.getTag();
            }

            holder.textview1.setText(todo.getgrade(position));
            holder.textview2.setText(todo.getschool(position));
            holder.textview3.setText(todo.getreason(position));

           return convertView;
        }

        }

        static class ViewHolder {
            TextView textview1;
            TextView textview2;
            TextView textview3;     
        }

        @Overri开发者_如何学Cde
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub

        }
       } 


Just add the item to your list and refresh the adapter, nothing special.


You need to increment your "tododata" data structure on button click and then call to notifyDataSetChanged() method:

    Button button = (Button) findViewById(R.id.btn);
            button.setOnClickListener(new OnClickListener(){
            public void onClick(View v) {
                   todo.addNewRow();
                   ca.notifyDataSetChanged();

                   Toast.makeText(prsnlhstry.this, "Row Added Successfully", Toast.LENGTH_SHORT).show();
                }
            });

To compile the above code you will need to define "ca" variable as the final one, or make it class member.

I hope it helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜