如何在按下时添加项目标签,并在再次按下项目分隔符(,)时将其删除?

时间:2016-01-06 09:20:20

标签: android

将帖子 像这样的东西...... 当按下时,如果有多于两个项目,则用逗号添加项目标签[并且当再次按下时用逗号删除项目标签 这是我到目前为止所做的 注意SeatP是textView,seat_n是列表中的选定项目标签

List<Seats> seatsList;
GridView myGrid;
MyGridAdapter adapter;
TextView seatlabel,pricelabel;
ImageView im;
String st,seat_n,seatp;

myGrid =(GridView)findViewById(R.id.grid);

    seatlabel = (TextView) findViewById(R.id.labelSeat);
    pricelabel = (TextView) findViewById(R.id.labelPrice);

public void updateMessage(){

    adapter = new MyGridAdapter(getApplicationContext(),R.layout.seat_item,seatsList);
    myGrid.setAdapter(adapter);
    myGrid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            st = seatsList.get(position).getStatus();
            seat_n = seatsList.get(position).getSeat_no();
            p = seatsList.get(position).getPrice();
            Log.d("price: ", p + "");

            //Log.d("status: ", seat_n + "");
            im = (ImageView) view.findViewById(R.id.seat1);

            if (st.equalsIgnoreCase("available")) {

                im.setImageResource(R.drawable.seat);
                myStatus = true;
                seatsList.get(position).setStatus("select");
                Log.d("status 1: ",myStatus+"");
                if (seatp.length()>1){
                    seatp = seatp+"," + seat_n;
                }else {
                    seatp = seatp+ seat_n;
                }
                pr = pr+p;

                seatlabel.setText(seatp.trim());
                pricelabel.setText(Integer.toString(pr));

            }
            if(st.equalsIgnoreCase("select")) {
               // Toast.makeText(getApplicationContext(), "seat: " + seatsList.get(position).getStatus(), Toast.LENGTH_SHORT).show();

                im.setImageResource(R.drawable.seat_avv);
                seatsList.get(position).setStatus("available");
                pr = pr-p;
                pricelabel.setText(Integer.toString(pr));
                seatp= seatlabel.getText().toString();
                //Log.d("seatlabel: ",ab);
                //seatp= seatp.replace(seat_n, "");
                if(seatp.length()>=5){
                    seatp= seatp.replace(","+seat_n, "");
                }else {
                    seatp= seatp.replace(seat_n+",", "");
                }
                Log.d("seatp Length: ",seatp.length()+"");
                /*if(seatp.length()>2){
                    seatp= seatp.replace(", "+seat_n, "");
                    Log.d("seatp Length2: ",seatp.length()+"");
                }else{
                    seatp= seatp.replace(seat_n+", ", "");
                    Log.d("Deleting: ",seat_n+"");
                    seatp = "";
                }*/
                Log.d("seatabzz: ",seatp);
                seatlabel.setText(seatp.trim());
            }
        }
    });

}

这里的逻辑适用于第二项或更多项但是没有为第一项工作

1 个答案:

答案 0 :(得分:0)

如果我理解你,你可以使用ArrayList 点击标签时调用addOrRemoveItem(),传递您需要添加的文本 当点击savenext或其他任何内容时,请致电getAsCommaSeparator()

ArrayList<String> items = new ArrayList<>();

public void addOrRemoveItem(String item){
    if(items.contains(item)){
        items.remove(item);
    }else{
        items.add(item);
    }
}

public String getAsCommaSeparator(){
    return android.text.TextUtils.join(",",items);
}