在自定义列表视图中编辑文本焦点行为不当

时间:2016-03-09 06:19:32

标签: android listview android-edittext

我已经创建了一个带有编辑文本的自定义列表视图,用于在列表视图中删除该项目的按钮,并在运行时通过单击按钮添加项目,仅对于项目,它可以正常工作但是如果列表中的项目添加然后编辑文本给焦点问题,我无法在任何编辑文本中写任何东西代码在模拟器上工作完美。

这是我的代码

活动类     public class Activity_Add_CustomArea extends BaseActivity实现了View.OnClickListener,AdapterAddCustomArea.DeletePosition {         ListView ListView_custom;         列出arraylist_addArea;

    private LinearLayout relativeLayout_textViewArea, linear_listview;
    AdapterAddCustomArea adapterAddCustomArea;
    private TextView txt_header;
    private Button customArea_back_btn;
    int i = 1;
    public static int starRating = 0;
    public static int starRatingPosition = 0;
    public static String ROOM_IMAGE = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_custom_area);
        relativeLayout_textViewArea = (LinearLayout) findViewById(R.id.Relatve_txtview);
        txt_header = (TextView) findViewById(R.id.customheader_tv);
        relativeLayout_textViewArea.setOnClickListener(this);
        arraylist_addArea = new ArrayList<CustomAreaModel>();
        ListView_custom = (ListView) findViewById(R.id.List_add_custonArea);
        customArea_back_btn = (Button) findViewById(R.id.addcustonarea_back_btn);
        linear_listview = (LinearLayout) findViewById(R.id.linear_listview);
        customArea_back_btn.setOnClickListener(this);
        String getRoomName = getIntent().getStringExtra("roomname");
        txt_header.setText(getRoomName);
        adapterAddCustomArea = new AdapterAddCustomArea(Activity_Add_CustomArea.this, arraylist_addArea, false);
        ListView_custom.setAdapter(adapterAddCustomArea);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.Relatve_txtview:
                CustomAreaModel text = new CustomAreaModel();
                text.setName("");
                arraylist_addArea.add(text);
            if(adapterAddCustomArea.getCount()>3){
                LinearLayout.LayoutParams param1 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, 0.20f);
                LinearLayout.LayoutParams param2 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, 1.80f);
                ListView_custom.setLayoutParams(param1);
                relativeLayout_textViewArea.setLayoutParams(param2);
                adapterAddCustomArea.notifyDataSetChanged();
            }else {
                adapterAddCustomArea.notifyDataSetChanged();
            }
                break;
            case R.id.addcustonarea_back_btn:
                finish();
                break;
        }
    }

    @Override
    public void deletePosition(int position) {
        arraylist_addArea.remove(position);
        if(adapterAddCustomArea.getCount()>3){
            LinearLayout.LayoutParams param1 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, 0.20f);
            LinearLayout.LayoutParams param2 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, 1.80f);
            ListView_custom.setLayoutParams(param1);
            relativeLayout_textViewArea.setLayoutParams(param2);
            adapterAddCustomArea.notifyDataSetChanged();
        }else {

            LinearLayout.LayoutParams param1 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT );
            LinearLayout.LayoutParams param2 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT );
            ListView_custom.setLayoutParams(param1);
            relativeLayout_textViewArea.setLayoutParams(param2);
            adapterAddCustomArea.notifyDataSetChanged();
        }
    }
    @Override
    public void setValueInModel(int position, String text) {
        if (arraylist_addArea.size() < 1) {
            return;

        } else {
            if (arraylist_addArea.size() == position) {
                arraylist_addArea.get(position - 1).setName(text);
                adapterAddCustomArea.notifyDataSetChanged();
            } else {
                arraylist_addArea.get(position).setName(text);
                adapterAddCustomArea.notifyDataSetChanged();
            }
        }
    }
    protected void onResume() {
        //adapterAddCustomArea = new AdapterAddCustomArea(Activity_Add_CustomArea.this, arraylist_addArea,true);
        //   ListView_custom.setAdapter(adapterAddCustomArea);
        adapterAddCustomArea.notifyDataSetChanged();
        super.onResume();
    }

    //    public void delete(int position)
//    {
//        arraylist_addArea.remove(position);
//        adapterAddCustomArea = new AdapterAddCustomArea(MainActivity.this, arraylist_addArea);
//        ListView_custom.setAdapter(adapterAddCustomArea);
//    }
    private void scrollMyListViewToBottom() {
        ListView_custom.post(new Runnable() {
            @Override
            public void run() {
                // Select the last row so it will scroll into view...
                ListView_custom.setSelection(adapterAddCustomArea.getCount() - 1);
            }
        });
    }
}

适配器类

public class AdapterAddCustomArea extends BaseAdapter {
    private Context mContext;
    List<CustomAreaModel> arraylist_addAreaAdapter;
    Activity_Add_CustomArea activity;
    public static final String MyPREFERENCES = "MyPrefs";

    String edt_value;
    String[] edtposition;
    public boolean isAdapterCalled = false;
    Map<String, String> areaName;

    public AdapterAddCustomArea(Context c, List<CustomAreaModel> addcustomArea, boolean isAdapterCalled) {
        this.mContext = c;
        activity = (Activity_Add_CustomArea) c;
        this.arraylist_addAreaAdapter = addcustomArea;
        this.isAdapterCalled = isAdapterCalled;
        areaName = new HashMap<>();
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return arraylist_addAreaAdapter.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return arraylist_addAreaAdapter;
    }
    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        final ViewHolder holder;
        LayoutInflater mInflater = (LayoutInflater) mContext
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.inflator_add_custom_area, null);
            holder = new ViewHolder();
            // holder.relatve_txtViewPlus = (RelativeLayout) convertView.findViewById(R.id.Relatve_txtview);
            holder.starLine_layout = (LinearLayout) convertView.findViewById(R.id.linear_starView);
            holder.layout_addArea_btn = (LinearLayout) convertView.findViewById(R.id.layout_add_button);
            holder.textView_addArea = (TextView) convertView.findViewById(R.id.addTextArea);
            holder.cross_image = (ImageView) convertView.findViewById(R.id.addcustom_cross_btn);
            holder.note_image = (ImageView) convertView.findViewById(R.id.AddcustomArea_Notes);
            holder.camera_image = (ImageView) convertView.findViewById(R.id.AddcustomArea_capture);
            holder.addArea_btn = (Button) convertView.findViewById(R.id.addArea_btnArea);
            holder.edit_addArea = (EditText) convertView.findViewById(R.id.AddcustomArea_txtview);
            holder.inflatorRoomDetail_firstStar = (ImageView) convertView.findViewById(R.id.inflatorRoomDetail_firstStar);
            holder.inflatorRoomDetail_twoStar = (ImageView) convertView.findViewById(R.id.inflatorRoomDetail_twoStar);
            holder.inflatorRoomDetail_threeStar = (ImageView) convertView.findViewById(R.id.inflatorRoomDetail_threeStar);
            holder.inflatorRoomDetail_fourStar = (ImageView) convertView.findViewById(R.id.inflatorRoomDetail_fourStar);
            holder.inflatorRoomDetail_fiveStar = (ImageView) convertView.findViewById(R.id.inflatorRoomDetail_fiveStar);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.cross_image.setTag(position);
        holder.edit_addArea.setText(arraylist_addAreaAdapter.get(position).getName());
        holder.edit_addArea.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (!hasFocus) {
                    //Toast.makeText(activity,holder.edit_addArea.getText().toString(),Toast.LENGTH_LONG).show();
                    activity.setValueInModel(position, holder.edit_addArea.getText().toString());
                }
            }
        });

        holder.addArea_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                arraylist_addAreaAdapter.get(position).setIsOpen(true);
                holder.starLine_layout.setVisibility(View.VISIBLE);
                holder.cross_image.setVisibility(View.GONE);
                holder.layout_addArea_btn.setVisibility(View.GONE);
                holder.edit_addArea.setVisibility(View.VISIBLE);
                holder.note_image.setVisibility(View.VISIBLE);
                holder.camera_image.setVisibility(View.VISIBLE);

            }
        });
        if (arraylist_addAreaAdapter.get(position).getNoOfRating() > 0) {
            switch (arraylist_addAreaAdapter.get(position).getNoOfRating()) {
                case 1:
                    holder.inflatorRoomDetail_firstStar.setImageResource(R.drawable.onestarred);
                    holder.inflatorRoomDetail_twoStar.setImageResource(R.drawable.twostarview);
                    holder.inflatorRoomDetail_threeStar.setImageResource(R.drawable.threestar);
                    holder.inflatorRoomDetail_fiveStar.setImageResource(R.drawable.fivestar);
                    holder.starLine_layout.setVisibility(View.VISIBLE);
                    holder.layout_addArea_btn.setVisibility(View.GONE);
                    holder.cross_image.setVisibility(View.GONE);
                    break;
                case 2:
                    holder.inflatorRoomDetail_twoStar.setImageResource(R.drawable.twostargray);
                    holder.inflatorRoomDetail_firstStar.setImageResource(R.drawable.onestar);
                    holder.inflatorRoomDetail_threeStar.setImageResource(R.drawable.threestar);
                    holder.inflatorRoomDetail_fiveStar.setImageResource(R.drawable.fivestar);
                    holder.starLine_layout.setVisibility(View.VISIBLE);
                    holder.layout_addArea_btn.setVisibility(View.GONE);
                    holder.cross_image.setVisibility(View.GONE);
                    break;
                case 3:
                    holder.inflatorRoomDetail_threeStar.setImageResource(R.drawable.threestarorang);
                    holder.inflatorRoomDetail_firstStar.setImageResource(R.drawable.onestar);
                    holder.inflatorRoomDetail_twoStar.setImageResource(R.drawable.twostarview);
                    holder.inflatorRoomDetail_fiveStar.setImageResource(R.drawable.fivestar);
                    holder.starLine_layout.setVisibility(View.VISIBLE);
                    holder.layout_addArea_btn.setVisibility(View.GONE);
                    holder.cross_image.setVisibility(View.GONE);
                    break;
            }
        } else {
            if (arraylist_addAreaAdapter.get(position).isOpen()) {
                holder.starLine_layout.setVisibility(View.VISIBLE);
                holder.cross_image.setVisibility(View.GONE);
                holder.layout_addArea_btn.setVisibility(View.GONE);
                holder.edit_addArea.setVisibility(View.VISIBLE);
                holder.note_image.setVisibility(View.VISIBLE);
                holder.camera_image.setVisibility(View.VISIBLE);

            } else {
                holder.starLine_layout.setVisibility(View.GONE);
                holder.layout_addArea_btn.setVisibility(View.VISIBLE);
                holder.cross_image.setVisibility(View.VISIBLE);
                holder.note_image.setVisibility(View.GONE);
                holder.camera_image.setVisibility(View.GONE);
                holder.inflatorRoomDetail_firstStar.setImageResource(R.drawable.onestar);
                holder.inflatorRoomDetail_twoStar.setImageResource(R.drawable.twostarview);
                holder.inflatorRoomDetail_threeStar.setImageResource(R.drawable.threestar);
                holder.inflatorRoomDetail_fourStar.setImageResource(R.drawable.fourstar);
                holder.inflatorRoomDetail_fiveStar.setImageResource(R.drawable.fivestarblue);
            }
        }
        holder.cross_image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                activity.deletePosition(position);
            }
        });
        holder.inflatorRoomDetail_firstStar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                arraylist_addAreaAdapter.get(position).setNoOfRating(1);
                Intent intent = new Intent(mContext, ActivityOpenGallery.class);
                intent.putExtra("flag", 1);
//              intent.putExtra("position",position);
                setRatingAndPosition(1, position);
                mContext.startActivity(intent);
                ((Activity) mContext).overridePendingTransition(R.anim.enter, R.anim.exit);
            }
        });
        holder.inflatorRoomDetail_twoStar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                arraylist_addAreaAdapter.get(position).setNoOfRating(2);
                Intent intent = new Intent(mContext, ActivityOpenGallery.class);
                intent.putExtra("flag", 2);
                setRatingAndPosition(2, position);
//              intent.putExtra("position",position);
                mContext.startActivity(intent);
                ((Activity) mContext).overridePendingTransition(R.anim.enter, R.anim.exit);
            }
        });
        holder.inflatorRoomDetail_threeStar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                arraylist_addAreaAdapter.get(position).setNoOfRating(3);
                Intent intent = new Intent(mContext, ActivityOpenGallery.class);
                intent.putExtra("flag", 3);
                setRatingAndPosition(3, position);
                mContext.startActivity(intent);
                ((Activity) mContext).overridePendingTransition(R.anim.enter, R.anim.exit);
            }
        });
        holder.inflatorRoomDetail_fourStar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final Dialog dialog = new Dialog(mContext);
                dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                dialog.setContentView(R.layout.inventry_dialog_fourstar);
                Button dialogButtonOk = (Button) dialog.findViewById(R.id.dilaog_button_cancel);
                dialogButtonOk.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });
                dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
                dialog.show();
            }
        });
        return convertView;
    }
    private class ViewHolder {
        //RelativeLayout relatve_txtViewPlus;
        TextView textView_addArea;
        ImageView cross_image, note_image, camera_image, inflatorRoomDetail_firstStar, inflatorRoomDetail_twoStar,
                inflatorRoomDetail_threeStar, inflatorRoomDetail_fourStar, inflatorRoomDetail_fiveStar;
        Button addArea_btn;
        LinearLayout starLine_layout,layout_addArea_btn;
        EditText edit_addArea;

    }
    public interface DeletePosition {
        public void deletePosition(int position);

        public void setValueInModel(int position, String text);
    }
    private void setRatingAndPosition(int rating, int position) {
        Activity_Add_CustomArea.starRating = rating;
        Activity_Add_CustomArea.starRatingPosition = position;
    }

}

0 个答案:

没有答案