代码之家  ›  专栏  ›  技术社区  ›  3iL Selvam Elangovan

在listview中为textview使用holder

  •  1
  • 3iL Selvam Elangovan  · 技术社区  · 7 年前

    我正在实施一个 ListView 有2个 TextViews 在它里面。第一个 TextView 是我从查询中填充的产品的名称。当我触摸屏幕上的任何项目时 列表视图 Dialog 出现。当我确认 对话 我想换第二个 文本到字符串,但每当我每秒这样做时 Textview 文本更改。例如,如果我在列表视图中有3个项目,并且我单击了第一个项目,那么只有该项目的文本视图应该更改,而不是全部三个。

    这是我的基本适配器代码:

    public class StrengthAdapter extends BaseAdapter {
        private ArrayList<Strengths> list;
        private LayoutInflater layoutInflater;
        private static HashMap<Integer, String> selectedStrengthsMap = new HashMap<Integer, String>();
        private String testing;
    
        public StrengthAdapter(Context context, ArrayList<Strengths> list, String test) {
            this.list = list;
            layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            this.testing = test;
            //HashMap<Integer, String> issuesStrengthsbrandNameIDMap = context
        }
    
        @Override
        public int getCount() {
            return list.size();
        }
    
        @Override
        public Object getItem(int position) {
            return list.get(position);
        }
    
        @Override
        public long getItemId(int position) {
            return position;
        }
    
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            final ViewHolder viewHolder;
            if (convertView == null){
                viewHolder = new ViewHolder();
                //viewHolder.position = position;
                convertView = layoutInflater.inflate(R.layout.strengthslistview, null);
                viewHolder.strengthCheckBox = (TextView) convertView.findViewById(R.id.strengthsCheckBox);
                viewHolder.chip = convertView.findViewById(R.id.chip);
    
                convertView.setTag(viewHolder);
            }else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            viewHolder.strengthCheckBox.setText(((Strengths) list.get(position)).getStrength_title());
            viewHolder.chip.setText(testing);
            //viewHolder.position = position;
            return convertView;
        }
    
        public static HashMap<Integer, String> sendSelectedStrengthMap(){
            return selectedStrengthsMap;
        }
    
        static class ViewHolder {
            TextView strengthCheckBox;
            TextView chip;
            //int position;
        }
    }
    

    这是我最初设置适配器的方式:

    strengthListView.setAdapter(new StrengthAdapter(StrengthOfDemandsView.this, strengthsList, "TEST"));
    

    这是我在关闭对话框后设置适配器的方式:

    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    strengthListView.setAdapter(new StrengthAdapter(StrengthOfDemandsView.this, strengthsList, "TEST123"));
    
                }
            });
    
    2 回复  |  直到 7 年前
        1
  •  3
  •   Yev Kanivets    7 年前

    您需要处理适配器内部的点击,而不是每次点击时都设置适配器。它必须看起来像这样:

    @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            final ViewHolder viewHolder;
            if (convertView == null){
                viewHolder = new ViewHolder();
                //viewHolder.position = position;
                convertView = layoutInflater.inflate(R.layout.strengthslistview, null);
                viewHolder.strengthCheckBox = (TextView) convertView.findViewById(R.id.strengthsCheckBox);
                viewHolder.chip = convertView.findViewById(R.id.chip);
    
                convertView.setTag(viewHolder);
            }else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            viewHolder.strengthCheckBox.setText(((Strengths) list.get(position)).getStrength_title());
            convertView.setOnClickListener(
                 // show dialog here and then in positiveButton callback call
                 //viewHolder.chip.setText(testing);
            );
            return convertView;
        }
    
        2
  •  1
  •   Muhammad Saad Rafique    7 年前

    首先,在创建支架之前对布局进行充气。 然后,您需要通过实现onclicklistener来定义onclick行为。我更新了getView方法,请按如下方式实现:

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;
        if (convertView == null){
            convertView = layoutInflater.inflate(R.layout.strengthslistview, null);
            viewHolder = new ViewHolder();
            //viewHolder.position = position;
    
            viewHolder.strengthCheckBox = (TextView) convertView.findViewById(R.id.strengthsCheckBox);
            viewHolder.chip = convertView.findViewById(R.id.chip);
    
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.strengthCheckBox.setText(((Strengths) list.get(position)).getStrength_title());
        viewHolder.chip.setText(testing);
        //viewHolder.position = position;
    convertView.setOnClickListener(
                 //you can do your job here what u want to do by clicking specific row
            );
        return convertView;
    }