如何在android中的适配器的getView()中动态设置不同的视图

时间:2015-04-01 10:42:14

标签: java android android-listview

我是通过从服务器获取数据来创建提交表单,根据服务器的不同,此表单可能有不同的视图(EditTextCheckBoxRadioButtonRatingBar)建议。我已经在适配器中成功创建了该视图,但每次向下滚动或向上滚动时,用户的回答都会被清除(如已检查的项目自动取消选中,EditText被清除等)并且视图也会自动交换为从头到尾的位置或只是交换。我不知道如何解决这个问题,这是我的getView方法:

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
            LayoutInflater inflater = context.getLayoutInflater();
            rowView = inflater.inflate(R.layout.customratinglist, parent, false);
            final ViewHolder holder = new ViewHolder();
            holder.tvQuestion = (TextView) rowView.findViewById(R.id.txtquestion);
            holder.tvQuestion.setTag(list.get(position));
            holder.layOut = (LinearLayout) rowView.findViewById(R.id.lnQuestion);
            holder.layOut.setTag(position);
            rowView.setTag(holder);

        } else {

            rowView = convertView;
            ((ViewHolder) rowView.getTag()).layOut.setTag(position);
        }
        final ViewHolder viewHolder = (ViewHolder) rowView.getTag();
        viewHolder.tvQuestion.setTypeface(font);
        viewHolder.tvQuestion.setText(list.get(position).getQuestion());
        viewHolder.layOut.removeAllViews();
        switch (list.get(position).getAns_type_id()) {
            case "1":
                ratingBar = new RatingBar(context);
                ratingBar.setNumStars(5);
                ratingBar.setStepSize(0.1f);
                ratingBar.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT));

                viewHolder.layOut.addView(ratingBar);

                ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
                    @Override
                    public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {


                        answerSetter((int) (viewHolder.layOut.getTag()), String.valueOf(rating));


                    }
                });


                break;


            case "2":


                editText = new EditText(context);
                editText.setLayoutParams(new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.MATCH_PARENT));
                editText.setTextColor(Color.BLACK);

                viewHolder.layOut.addView(editText);

                editText.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {


                        answerSetter((int) (viewHolder.layOut.getTag()), String.valueOf(s));


                    }

                    @Override
                    public void afterTextChanged(Editable s) {


                    }
                });


                break;
}

return rowView;
}

任何人都可以帮忙吗?

2 个答案:

答案 0 :(得分:0)

解决方案是使用这两种方法:

@Override
public int getItemViewType(int position) {
    return Integer.parseInt(list.get(position).getAns_type_id());
}

@Override
public int getViewTypeCount() {
    return 2;
}

之后,您可以使用getItemViewType方法内的getView方法和position方法,getView方法在{{1}}方法中作为参数传递。

有关详细信息,请查看此tutorial

Here是这些方法的文档。

答案 1 :(得分:0)

你可以这样试试,

@Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        // TODO Auto-generated method stub


        LayoutInflater inflater = getActivity().getLayoutInflater();
        View row;


        switch (Maintain_List.get(position))
        {
            case 1:
                row = inflater.inflate(R.layout.row, parent, false);


                TextView tv = (TextView) row.findViewById(R.id.row_title);
                tv.setText("" + sliding_menu_text.get(position));


                ImageView iv1 = (ImageView) row.findViewById(R.id.row_image);
                iv1.setImageResource(sliding_menu_icon.get(position));
                break;

            case 2:
                row = inflater.inflate(R.layout.row2, parent, false);
                break;

            case 3:


                row = inflater.inflate(R.layout.row3, parent, false);
                TextView tv2 = (TextView) row.findViewById(R.id.tv_of_row3);
                tv2.setText("" + sliding_menu_text.get(position));


                break;

            case 4:

                row = inflater.inflate(R.layout.refresh_category, parent, false);


                final Button b = (Button) row.findViewById(R.id.refresh_btn_of_refresh_category);
                final WebView wv = (WebView) row.findViewById(R.id.wv_of_refresh_category);
                b.setVisibility(View.VISIBLE);
                b.setOnClickListener(new OnClickListener()
                {

                    @Override
                    public void onClick(View v)
                    {

                        NetworkCheck nc = new NetworkCheck(getActivity());
                        isInternetPresent = nc.isConnectingToInternet();
                        if (!isInternetPresent)
                        {
                            // Ntwork not avilabale
                            Toast.makeText(getActivity(), "Connection Error !!", Toast.LENGTH_SHORT).show();

                        }
                        else
                        {
                            // Ntwork Avilable
                            wv.loadUrl("file:///android_asset/m.html");
                            wv.setBackgroundColor(Color.TRANSPARENT);
                            wv.setVisibility(View.VISIBLE);
                            b.setVisibility(View.INVISIBLE);
                            new get_List_of_Drawer().execute();


                        }


                    }
                });

                break;


            default:
                row = null;
                break;

        }


        return row;

    }

对我来说很好...... !!!