无法在android中的ListView中操作List项

时间:2017-02-22 09:50:11

标签: listview android-arrayadapter listitem

我想在listView中使用以下功能:

1)我想动态显示项目(由我完成)

2)然后我想在每个列表项上运行倒数计时器(1分钟)并显示 每个项目的文本倒计时不同(由我完成),并在时间到期后从listView中删除该项目

3)然后我需要在点击任何项目时显示对话框(有两个选项,接受和拒绝)

4)然后,如果用户选择接受(来自对话框),我想显示'接受'在TextView

5)或者,如果用户选择拒绝,我想显示'拒绝'在TextView中,然后在1分钟后从列表中删除该项目(即显示被拒绝的项目,最多1分钟)

public class MainActivity extends Activity {


    //listview object
    private ListView lvView;
    //list to store data which will pass in adapter
    private List lstRoster;
    //view holder user to hold view from list-items
    ViewHolder holder;
    // forRandom number
    Random rand = new Random();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //initializing listView
        lvView = (ListView) findViewById(lvItems);

        //initializing list Products
        lstRoster = new ArrayList<>();
        //initializing custome adapter :passing-(context,list)
        final CountdownAdapter cda = new CountdownAdapter(MainActivity.this, lstRoster);

        //setting adapter to list view
        lvView.setAdapter(cda);
        //setting onlistItemClickListener on list
        lvView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView arg0, View view,
                    final int position, long id) {

                //Take action here.
                // Toast.makeText(MainActivity.this, position + " Clicked", Toast.LENGTH_SHORT).show();
                //lvItems.removeViewAt(position);
                new AlertDialog.Builder(MainActivity.this)
                .setTitle("Roster")
                .setMessage("Please Accept Or cancel roster")
                .setPositiveButton("Accept", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        // continue with delete
                        Toast.makeText(MainActivity.this, "Roster Accepted", Toast.LENGTH_SHORT).show();
                        lstRoster.get(position);
                        cda.notifyDataSetChanged();
                    }
                })
                .setNegativeButton("Reject", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        // do nothing
                        // Toast.makeText(MainActivity.this, "No Action Roster will end automatically", Toast.LENGTH_SHORT).show();

                        new AlertDialog.Builder(MainActivity.this)
                        .setTitle("Attention")
                        .setMessage("Do you Really Want to Reject the roster")
                        .setPositiveButton("YES", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                // continue with delete
                                //
                                // Toast.makeText(MainActivity.this, "Roster Removed", Toast.LENGTH_SHORT).show();

                                lstRoster.remove(position);
                                cda.notifyDataSetChanged();
                            }
                        })
                        .setNegativeButton("NO", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                // do nothing
                                //Toast.makeText(MainActivity.this, "No Action Roster will end automatically", Toast.LENGTH_SHORT).show();





                            }
                        })
                        .setIcon(android.R.drawable.ic_dialog_alert)
                        .show();



                    }
                })
                .setIcon(android.R.drawable.ic_dialog_alert)
                .show();
                // lstProducts.remove(position);
                // cda.notifyDataSetChanged();
            }
        });

        //i remove button for removing list items so i commented below code
        // Button btn=(Button)findViewById(R.id.button);
        // btn.setOnClickListener(new View.OnClickListener() {
        // @Override
        // public void onClick(View v) {
        // for(int i=0;i<1;i++){
        // try {
        // Thread.sleep(100);
        //// Object toRemove = adapter.getItem(i);
        //// adapter.remove(toRemove);
        // if(!lstProducts.isEmpty()) {
        // lstProducts.remove(i);
        // cda.notifyDataSetChanged();
        // }
        // else {
        // Toast.makeText(MainActivity.this, "No Roster in list", Toast.LENGTH_SHORT).show();
        // }
        // } catch (InterruptedException e) {
        // e.printStackTrace();
        // }
        // }
        // }
        // });
        Button add = (Button) findViewById(R.id.add);
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (int i = 0; i < 1; i++) {
                    try {
                        Thread.sleep(100);
                        // Object toRemove = adapter.getItem(i);
                        // adapter.remove(toRemove);
                        lstRoster.add(new Product("ID= " + rand.nextInt(100) + 1, System.currentTimeMillis() + 1000 * 60));
                        // lstProducts.add(new Product("O", System.currentTimeMillis() + 10000));

                        cda.notifyDataSetChanged();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }

    private class Product {
        String name;
        long expirationTime;

        public Product(String name, long expirationTime) {
            this.name = name;
            this.expirationTime = expirationTime;
        }
    }


    public class CountdownAdapter extends ArrayAdapter {

        private LayoutInflater lf;
        private List lstHolders;
        private Handler mHandler = new Handler();
        private Runnable updateRemainingTimeRunnable = new Runnable() {
            @Override
            public void run() {
                synchronized (lstHolders) {
                    long currentTime = System.currentTimeMillis();
                    for (ViewHolder holder : lstHolders) {
                        holder.updateTimeRemaining(currentTime);
                    }
                }
            }
        };

        public CountdownAdapter(Context context, List objects) {
            super(context, 0, objects);
            lf = LayoutInflater.from(context);
            lstHolders = new ArrayList<>();
            startUpdateTimer();
        }

        private void startUpdateTimer() {
            Timer tmr = new Timer();
            tmr.schedule(new TimerTask() {
                @Override
                public void run() {
                    mHandler.post(updateRemainingTimeRunnable);
                }
            }, 1000, 1000);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = lf.inflate(R.layout.list_item, parent, false);
                holder.tvProduct = (TextView) convertView.findViewById(R.id.tvProduct);
                holder.tvTimeRemaining = (TextView) convertView.findViewById(R.id.tvTimeRemaining);

                //buttons
                // holder.btnAccept = (Button) convertView.findViewById(R.id.btn_accept);
                // holder.btnReject = (Button) convertView.findViewById(R.id.btn_reject);


                // holder.btnAccept.setOnClickListener(new View.OnClickListener() {
                // @Override
                // public void onClick(View v) {
                //
                //
                // }
                // });
                // holder.btnAccept.setOnClickListener(new View.OnClickListener() {
                // @Override
                // public void onClick(View v) {
                //
                // }
                // });

                convertView.setTag(holder);
                synchronized (lstHolders) {
                    lstHolders.add(holder);
                }
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.setData(getItem(position));

            return convertView;
        }


    }

    private class ViewHolder {
        TextView tvProduct;
        TextView tvTimeRemaining;
        Button btnAccept;
        Button btnReject;

        Product mProduct;

        public void setData(Product item) {
            mProduct = item;
            tvProduct.setText(item.name);
            updateTimeRemaining(System.currentTimeMillis());

        }


        public void updateTimeRemaining(long currentTime) {
            long timeDiff = mProduct.expirationTime - currentTime;
            if (timeDiff > 0) {
                int seconds = (int) (timeDiff / 1000) % 60;
                int minutes = (int) ((timeDiff / (1000 * 60)) % 60);
                int hours = (int) ((timeDiff / (1000 * 60 * 60)) % 24);
                tvTimeRemaining.setText(hours + " hrs " + minutes + " mins " + seconds + " sec");
            } else {
                tvTimeRemaining.setText("Expired!!");
                tvTimeRemaining.setTextColor(Color.RED);
            }
        }
    }
}

0 个答案:

没有答案