Android - 如何删除列表项?

发布于 01-01 09:34 字数 3515 浏览 3 评论 0原文

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

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

}

I have got a main extending adapter which adds rows when user clicks on a button. In the adapter, I got a remove item button and a checkbox. How do I remove the item from the list?

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

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱,才寂寞2025-01-08 09:34:20

删除列表项,并调用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);
    }

remove list item, and invoke 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);
    }
囚你心2025-01-08 09:34:20

使用从数组列表中删除项目

noteList.remove(itemIndex);

,然后调用

aa.notifyDataSetChanged();

Remove the item from the arraylist using

noteList.remove(itemIndex);

and then call

aa.notifyDataSetChanged();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文