2012-01-30 6 views
0

사용자가 버튼을 클릭 할 때 행을 추가하는 기본 확장 어댑터가 있습니다. 어댑터에서 항목 제거 버튼과 확인란이 있습니다. 목록에서 항목을 제거하려면 어떻게합니까?Android - 목록 항목을 삭제하는 방법?

public class main extends Activity{ 

ArrayList<String> noteList = new ArrayList<String>(); 
FancyAdapter aa = null; 

Button calculate; 
EditText result; 
String total; 
String name; 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    final Spinner spinner = (Spinner)findViewById(R.id.spinner); 
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
      this, R.array.spinner_array, android.R.layout.simple_spinner_item); 
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
    spinner.setAdapter(adapter); 

    ListView myListView = (ListView)findViewById(R.id.noteList); 
    aa = new FancyAdapter(); 

    final EditText price = (EditText)findViewById(R.id.price); 
    final EditText name1 = (EditText)findViewById(R.id.name); 
    myListView.setAdapter(aa); 

    myListView.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> arg0, View arg1, 
       int position, long arg3) { 

     } 
    }); 

    Button btnSimple = (Button)findViewById(R.id.btnSimple);   

    btnSimple.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) 
     { 
      try 
      { 
      double totalPrice = Double.parseDouble(price.getText().toString()); 
      int position = spinner.getSelectedItemPosition(); 
      name = name1.getText().toString(); 

      if(position == 0) 
      { 
       totalPrice = totalPrice * 1.07; 
       total = String.valueOf(totalPrice); 
       System.out.println(total); 
      } 
      else 
      { 
       totalPrice = (totalPrice * 1.1)*1.07; 
       total = String.valueOf(totalPrice); 
       System.out.println(total); 
      } 
      String wholeString = name + ":$" +total; 
      noteList.add(0, wholeString); 
      System.out.println(total); 
      name1.setText(""); 
      price.setText(""); 
      aa.notifyDataSetChanged(); 
      } 
      catch (Exception e) 
      { 

      } 
     } 
    });   

} 

class FancyAdapter extends ArrayAdapter<String> 
{ 
    Button calculate; 
    EditText price; 
    EditText result; 

    FancyAdapter() 
    { 
     super(main.this, android.R.layout.simple_list_item_1, noteList); 
    } 

    public View getView(int position, View convertView, ViewGroup parent) 
    { 
     View row = convertView;  

     if(row == null) 
     { 
      LayoutInflater inflater = getLayoutInflater(); 
      row = inflater.inflate(R.layout.custom_list_item, null); 
     } 

     StringTokenizer tokens = new StringTokenizer(noteList.get(position), ":"); 
     String first = tokens.nextToken(); 
     String second = tokens.nextToken(); 
     row.getTag(); 
     ((TextView)row.findViewById(R.id.nametv)).setText(first); 
     ((EditText)row.findViewById(R.id.result)).setText(second); 

     Button deleteButton = (Button) row.findViewById(R.id.button); 

     deleteButton.setTag(position); 

     deleteButton.setOnClickListener(
       new Button.OnClickListener() { 
        public void onClick(View v) { 

        } 
       } 
      ); 
     return (row); 
    } 
} 

} 
+0

가능한 중복 (http://stackoverflow.com/questions/2558591/remove-listview-items-in-android) – Oliver

답변

5

제거 목록 항목을 호출 한 후

noteList.remove(itemIndex); 

등을 사용하여 ArrayList의에서 항목을 제거하고 notifyDataSetChanged를 호출; [안드로이드 ListView에 항목을 제거]의

public View getView(int position, View convertView, ViewGroup parent) 
    { 
     View row = convertView;  

     if(row == null) 
     { 
      LayoutInflater inflater = getLayoutInflater(); 
      row = inflater.inflate(R.layout.custom_list_item, null); 
     } 

     StringTokenizer tokens = new StringTokenizer(noteList.get(position), ":"); 
     String first = tokens.nextToken(); 
     String second = tokens.nextToken(); 
     row.getTag(); 
     ((TextView)row.findViewById(R.id.nametv)).setText(first); 
     ((EditText)row.findViewById(R.id.result)).setText(second); 

     Button deleteButton = (Button) row.findViewById(R.id.button); 

     deleteButton.setTag(position); 

     deleteButton.setOnClickListener(
       new Button.OnClickListener() { 
        public void onClick(View v) { 
         noteList.remove(position); 
         notifyDataSetChanged(); 
        } 
       } 
      ); 
     return (row); 
    } 
+0

아 ... 난 ... 볼이 noteList입니다 .풀다. 나는 aa.remove와 myListView.remove를 시도했지만 그것을 얻을 수 없었다. 감사. – Hend

+0

해결되었거나 아직 열려 있습니다. – jeet

+0

애니메이션을 추가 할 수 있습니까? – Miguel

3

) (

aa.notifyDataSetChanged(); 
관련 문제