将唯一标记分配给列表中的列表项

时间:2014-09-10 16:11:27

标签: android android-listview listadapter

我需要帮助在viewHolder对象中设置标签。我有一个比屏幕更长的列表(在手机上),我需要为每个视图设置一个不同的标签。正如您在外部片段中的预览应用程序(下面的链接)中所看到的那样,第一个项目和最后一个项目绑定在一起。我不想要那样,我希望每个观点都是独一无二的。我怎么能这样做。

https://drive.google.com/file/d/0B2xlu23GjnfvTEgtbkprOThtMmM/edit?usp=sharing

感谢

我的InspectionListAdapter

public class InspectionListAdapter extends ArrayAdapter<String> {

private final Activity context;
private final String[] inspectionTitles;
private final String[] description;

public InspectionListAdapter(Activity context, String[] inspectionTitles,
        String[] description) {
    super(context, R.layout.inspection_item_layout, inspectionTitles);
    this.context = context;
    this.inspectionTitles = inspectionTitles;
    this.description = description;
}

public static class ViewHolder {
    public ImageView imageView, expandButton;
    public TextView txtTitle;
    public TextView txtDescription;
    public RelativeLayout expandLayout;
    public EditText ownDesc, estCost;
    public LayerDrawable listItem;
    public GradientDrawable color;
    public CheckBox checkRepair, checkDone;
    public RelativeLayout mainLayout;

}

@Override
public View getView(int position, View view, ViewGroup parent) {

    final ViewHolder viewHolder;

    View rowView = view;

    if (rowView == null) {
        LayoutInflater inflater = context.getLayoutInflater();
        rowView = inflater.inflate(R.layout.inspection_item_layout, null,
                true);

        viewHolder = new ViewHolder();



        viewHolder.mainLayout = (RelativeLayout) rowView
                .findViewById(R.id.item_layout_main);

        viewHolder.txtTitle = (TextView) rowView
                .findViewById(R.id.textViewItemTitle);

        viewHolder.txtDescription = (TextView) rowView
                .findViewById(R.id.textViewTip);

        viewHolder.listItem = (LayerDrawable) rowView.getResources()
                .getDrawable(R.drawable.layer_car_background);

        viewHolder.color = (GradientDrawable) (viewHolder.listItem
                .findDrawableByLayerId(R.id.item_list_background));

        viewHolder.imageView = (ImageView) rowView
                .findViewById(R.id.imageViewItem);

        viewHolder.expandButton = (ImageView) rowView
                .findViewById(R.id.imageViewTitle);

        viewHolder.ownDesc = (EditText) rowView
                .findViewById(R.id.editTextDescription);

        viewHolder.estCost = (EditText) rowView
                .findViewById(R.id.editTextEstCost);

        viewHolder.checkRepair = (CheckBox) rowView
                .findViewById(R.id.checkBox2);

        viewHolder.checkDone = (CheckBox) rowView
                .findViewById(R.id.checkBox1);

        viewHolder.checkRepair
                .setOnCheckedChangeListener(new OnCheckedChangeListener() {

                    @Override
                    public void onCheckedChanged(CompoundButton view,
                            boolean arg1) {
                        // TODO Auto-generated method stub

                        if (view.isChecked()) {
                            viewHolder.checkDone.setChecked(false);
                        }
                    }
                });
        viewHolder.checkDone
                .setOnCheckedChangeListener(new OnCheckedChangeListener() {

                    @Override
                    public void onCheckedChanged(CompoundButton view,
                            boolean arg1) {
                        // TODO Auto-generated method stub

                        if (view.isChecked()) {
                            viewHolder.checkRepair.setChecked(false);

                        }
                    }
                });

        viewHolder.expandLayout = (RelativeLayout) rowView
                .findViewById(R.id.relativeLayoutExpandable);

        viewHolder.expandLayout.setVisibility(View.GONE);

        viewHolder.expandButton
                .setOnClickListener(new OnClickListener() {

                    public void onClick(View v) {
                        // TODO Auto-generated method stub

                        if (viewHolder.expandLayout.getVisibility() == View.VISIBLE) {
                            viewHolder.expandButton
                                    .setImageResource(R.drawable.ic_action_expand);
                            viewHolder.expandLayout
                                    .setVisibility(View.GONE);
                            if (viewHolder.checkDone.isChecked()) {

                                viewHolder.mainLayout
                                        .setBackgroundResource(R.drawable.layer_car_background_green);

                            }
                            if (viewHolder.checkRepair.isChecked()) {

                                viewHolder.mainLayout
                                        .setBackgroundResource(R.drawable.layer_car_background_red);
                            }

                        } else {
                            viewHolder.expandButton
                                    .setImageResource(R.drawable.ic_action_collapse);
                            viewHolder.expandLayout
                                    .setVisibility(View.VISIBLE);

                            viewHolder.mainLayout
                                    .setBackgroundResource(R.drawable.layer_car_background);

                        }
                    }
                });


        rowView.setTag(viewHolder);

    } else {

        viewHolder = (ViewHolder) rowView.getTag();
    }


    viewHolder.txtTitle.setText(inspectionTitles[position]);
    viewHolder.txtDescription.setText("Tip: \n" + description[position]);
    viewHolder.imageView.setImageResource(R.drawable.ic_launcher);
    viewHolder.expandButton
            .setImageResource(R.drawable.ic_action_expand);

    return rowView;
}

}

1 个答案:

答案 0 :(得分:0)

错误在于onClickListener forViewHolder.expandbutton的onClick。在那里,你开始使用&#39; if(viewHolder .....)`。但这是错误的viewHolder,因为在用户点击视图时,许多viewHolders可能已被创建并死亡。所以你必须首先获得正确的viewHolder。像(未经测试的)

                View rowView = (View)v.getParent();

                ViewHolder viewHolder =(ViewHolder) rowView.getTag();

你把它放在onClick()方法中。 final ViewHolder viewHolder;您已将其设为最终版,以便在onClick()和onCheckedChanged中使用它。删除最后一个。不幸的是,getParent()在onCheckedChanged中不起作用。所以我们做的不同。

    viewHolder.checkDone.setTag(viewHolder.checkRepair);
    viewHolder.checkDone
            .setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton view,
                        boolean arg1) {

                    CheckBox checkBox = (CheckBox)view.getTag();

                    if (view.isChecked()) {
                        //viewHolder.checkRepair.setChecked(false);
                        checkBox.setChecked(false);
                    }
                }
            });

对另一个复选框执行相同的操作。

现在颜色会扩展到另一个视图。原因是您应该在设置新的txtTitle,txtDescription等时同时设置默认颜色。所以添加viewHolder.mainLayout.setBackgroundResource(R.drawable.layer_car_background);

其余的代码错过了保持状态。如果项目被展开并且用户上下滚动列表(我添加了更多项目以便能够滚动更多),则展开状态消失。此外,复选框也不会保持检查状态,颜色会消失(添加更多项目)。你应该添加一些保持状态的布尔arrrays(至少三个)。更新事件中的那些数组。在getView()中读取数组并相应地选中颜色和复选框。添加那些你会看到的数组并不是很多工作......