2011-05-28 166 views
-3

我想在android中的列表視圖中單擊添加按鈕添加新行。在按鈕上添加新行點擊列表視圖

有人可以幫我嗎?

這裏是我到目前爲止的代碼:

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

     @Override 
     public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 
       long arg3) { 
      // TODO Auto-generated method stub 

     } 
     } 

回答

1

只需將項目添加到列表中,並刷新適配器,沒什麼特別的。

0

你需要增加按鈕點擊你的「tododata」數據結構,然後調用notifyDataSetChanged()方法:

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

編譯上面的代碼,你需要定義「CA」變量爲最終之一,或使其成爲班級成員。

我希望它有幫助。

相關問題