notifyDataSetChanged()的问题。所有edittext的值都在变化

时间:2015-11-25 14:30:46

标签: android android-listview android-edittext adapter delete-row

我有搜索字段,当我打开活动时我得到一些数据,我将这些数据填充到列表视图自定义行中。在自定义行中,我有文本视图,编辑文本,textViewCancelImage。当我从服务器得到响应,解析这些响应并获取自定义行。之后我在搜索字段中输入一些数据,我会得到一些建议。如果我选​​择任何建议,那么我将相同的自定义行填充到列表视图。当我点击任何建议我得到选定的值并填充相同的自定义行。但所有以前的自定义行编辑文本的值也更改为0.当我单击取消图像时,我无法删除行。 请查看活动

public class MaintainActivity extends AppCompatActivity implements View.OnClickListener { 
private ProgressDialog pDialog;
private ResultVO resLogin;
private Button updateMRL,clearMRL;
@InjectView(R.id.listViewReorderLevel)
protected ListView reorderLevelListView;
private AutoCompleteTextView searchField;
OrderAdapter reorderAdapter;
ArrayList<OrderLevelsListBO> OrderLevelsListBOs;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.reorder_level);
    new GetTradeNamesListAsyncTask().execute();
    toolbar = (Toolbar) findViewById(R.id.tool_bar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowTitleEnabled(false);
    getSupportActionBar().setIcon(android.R.color.transparent);
    updateMRL = (Button) findViewById(R.id.btnUpdateMRL);
    clearMRL = (Button) findViewById(R.id.btnClearMRL);        
    searchField = (AutoCompleteTextView) findViewById(R.id.searchAutoCompleteTextViewMRL);       
    searchField.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(searchField.getWindowToken(), 0);
            SimpleDataBO simpleDataBO = (SimpleDataBO) adapterView.getAdapter().getItem(position);
            String drugName = simpleDataBO.getData();
            OrderLevelsListBO OrderLevelsListBO = new OrderLevelsListBO();
            OrderLevelsListBO.setTradeCompositeId(drugName);
            OrderLevelsListBO.setReorderLevelInBaseUnit("0");
            OrderLevelsListBOs.add(OrderLevelsListBO);
            reorderAdapter.notifyDataSetChanged();
            searchField.setText("");
        }
    }); 
class GetTradeNamesListAsyncTask extends AsyncTask<String, String, String> {
    private ResultVO resGetTCI;     
    @Override
    protected void onPreExecute() {
                }
    @Override
    protected String doInBackground(String... args) {
        try {
            HeaderParms HeaderParms = ICommonMethods.setHeaderParams(MaintainReorderLevelActivity.this);
            APIServicesImpl services = new APIServicesImpl();
            resGetTCI = services.get(SimpleDataArrayListBO.class, IUrlsUtil.URL_KYM_GET_TCI, HeaderParms);
        } catch (Exception e) {
            e.printStackTrace();
            cancel(true);
        }
        return null;
    }
    protected void onPostExecute(String file_url) {            
        if (resGetTCI != null) {               
                           int appStatusCode = resGetTCI.getAppStatusCode();
            if (appStatusCode == Constants.APP_STATUS_CODE_SUCCESS) {
                SimpleDataArrayListBO simpleDataArrayListBO = (SimpleDataArrayListBO) resGetTCI.getPayload();
                ArrayList<SimpleDataBO> simpleDataBOArrayList = simpleDataArrayListBO.getSimpleDataBOList();
                if(simpleDataBOArrayList!=null) {
                    AutocomleteAdapter autocompleteAdapter = new AutocomleteAdapter(MaintainReorderLevelActivity.this, R.layout.drug_list_row_billing, simpleDataBOArrayList);
                    searchField.setAdapter(autocompleteAdapter);
                    searchField.setThreshold(1);
                    new OrderListAsy().execute();
                }
            }  else {
                String resMessage = resGetTCI.getMessages().get(0);
                Toast.makeText(MaintainReorderLevelActivity.this, resMessage, Toast.LENGTH_SHORT).show();
            }
        } else {
            ExceptionMessages.showAlertDialog(MaintainReorderLevelActivity.this, IExceptionUtil.NULL_RESPONSE_TITLE, IExceptionUtil.NULL_RESPONSE_MESSAGE, true);
        }
    }
}    
class OrderListAsy extends AsyncTask<String, String, String> {
    ResultVO resGetRL;
    ProgressDialog pDialog;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(MaintainReorderLevelActivity.this);
        pDialog.setMessage(Utility.USER_ID_EXISTS_MESSAGE);
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }
    @Override
    protected String doInBackground(String... args) {
        try {
            HeaderParms HeaderParms = ICommonMethods.setHeaderParams(MaintainReorderLevelActivity.this);
            APIServicesImpl services = new APIServicesImpl();
            resGetRL = services.get(ReorderLevelsArrayListBO.class, IUrlsUtil.URL_RL_GET_STOCKS, HeaderParms);
        } catch (Exception e) {
            e.printStackTrace();
            cancel(true);
        }
        return null;
    }
    protected void onPostExecute(String file_url) {
        if (pDialog.isShowing()) {
            pDialog.dismiss();
        }
        if (resGetRL != null) {
                            int appStatusCode = resGetRL.getAppStatusCode();
            if (appStatusCode == Constants.APP_STATUS_CODE_SUCCESS) {
                ReorderLevelsArrayListBO reorderLevelsArrayListBO = (ReorderLevelsArrayListBO) resGetRL.getPayload();
                OrderLevelsListBOs = reorderLevelsArrayListBO.getReorderLevelsList();
                reorderAdapter = new OrderAdapter(MaintainReorderLevelActivity.this, OrderLevelsListBOs);
                reorderLevelListView.setAdapter(reorderAdapter);
            } else {
                String resMessage = resGetRL.getMessages().get(0);
                Toast.makeText(MaintainReorderLevelActivity.this, resMessage, Toast.LENGTH_SHORT).show();
            }
        } else {
            ExceptionMessages.showAlertDialog(MaintainReorderLevelActivity.this, IExceptionUtil.NULL_RESPONSE_TITLE, IExceptionUtil.NULL_RESPONSE_MESSAGE, true);
        }
    }
}
}

MyAdapter类:

public class OrderAdapter extends BaseAdapter {
    private Activity context;
    ArrayList<OrderLevelsListBO> data;
    OrderLevelsListBO OrderLevelsListBO;
    public OrderAdapter(Activity context, ArrayList<OrderLevelsListBO> data) {
        super();
        this.context = context;
        this.data = data;
    }
    @Override
    public int getCount() {
        return data.size();
    }
    @Override
    public Object getItem(int position) {
        return position;
    }
    @Override
    public long getItemId(int position) {
        return position;
    }
    private class ViewHolder {
        EditText etReorderLevel;
        TextView txtRLTradeName;
        TextView txtDeleteMRL;
    }
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        LayoutInflater inflater = context.getLayoutInflater();
        OrderLevelsListBO = data.get(position);
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.reorder_level_row, null);
            holder = new ViewHolder();
            holder.etReorderLevel = (EditText) convertView.findViewById(R.id.edtReorderLevel);
            holder.txtRLTradeName = (TextView) convertView.findViewById(R.id.txtRLTradeName);
            holder.txtDeleteMRL = (TextView) convertView.findViewById(R.id.txtDeleteMRL);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (position % 2 == 1) {
            convertView.setBackgroundColor(context.getResources().getColor(R.color.UPDATE_STOCK_LINE));
        } else {
            convertView.setBackgroundColor(context.getResources().getColor(R.color.WHITE));
        }
        if (OrderLevelsListBO.getTradeCompositeId() != null) {
            holder.txtRLTradeName.setText(OrderLevelsListBO.getTradeCompositeId());
        }
        if (OrderLevelsListBO.getReorderLevelInBaseUnit() != null) {
            holder.etReorderLevel.setText(OrderLevelsListBO.getReorderLevelInBaseUnit());
        }
        holder.etReorderLevel.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) {
                if (!holder.etReorderLevel.getText().toString().equals("")) {
                    String reorderQuantity = holder.etReorderLevel.getText().toString();
                    data.get(position).setReorderLevelInBaseUnit(reorderQuantity);
                }
            }
            @Override
            public void afterTextChanged(Editable s) {
            }
        });
        holder.txtDeleteMRL.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                data.remove(position);
                notifyDataSetChanged();
            }
        });
       // OrderLevelsListBOs = data;
        return convertView;
    }
}

1 个答案:

答案 0 :(得分:-1)

为什么不使用recyclerview而不是listview?

无论如何,如果你打算使用listview,你要做的就是在自定义适配器中创建一个方法。

这样的事情:

public void addData(OrderLevelsListBO orderLevelsListBO) {
    data.add(orderLevelsListBO);
}

在添加项目时调用此方法。

reorderAdapter.addData(orderLevelsListBO);