Android - 如何删除列表项?

时间:2012-01-30 11:51:41

标签: android listview adapter

我有一个主扩展适配器,当用户点击按钮时会添加行。在适配器中,我有一个删除项目按钮和一个复选框。如何从列表中删除该项?

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

}

2 个答案:

答案 0 :(得分:5)

删除列表项,并调用notifyDataSetChanged();

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

答案 1 :(得分:2)

使用

从arraylist中删除该项目
noteList.remove(itemIndex);

然后致电

aa.notifyDataSetChanged();