notifyDataSetChanged() 的问题。所有 edittext 的值都在改变
Problems with notifyDataSetChanged(). All edittext's value is changing
我有搜索字段,当我打开 activity 我正在获取一些数据时,我将这些数据填充到列表视图自定义行中。在自定义行中,我有文本视图、编辑文本、textViewCancelImage。当我从服务器收到响应时,解析这些响应并提取到自定义 rows.After 中,我在搜索字段中输入一些数据,我会得到一些 suggestions.If 我 select 任何建议然后我填充相同自定义行到列表视图。当我单击任何建议时,我得到 selected 值并填充相同的自定义行。但是所有以前的自定义行编辑文本的值也更改为 0。而且当我单击取消图像时我无法删除行。
请检查 Activity
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);
}
}
}
}
我的适配器class:
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;
}
}
为什么不使用 recyclerview 而不是 listview?
无论如何,如果您打算使用列表视图,您要做的就是在您的自定义适配器中创建一个方法。
像这样:
public void addData(OrderLevelsListBO orderLevelsListBO) {
data.add(orderLevelsListBO);
}
添加项目时调用此方法。
reorderAdapter.addData(orderLevelsListBO);
我有搜索字段,当我打开 activity 我正在获取一些数据时,我将这些数据填充到列表视图自定义行中。在自定义行中,我有文本视图、编辑文本、textViewCancelImage。当我从服务器收到响应时,解析这些响应并提取到自定义 rows.After 中,我在搜索字段中输入一些数据,我会得到一些 suggestions.If 我 select 任何建议然后我填充相同自定义行到列表视图。当我单击任何建议时,我得到 selected 值并填充相同的自定义行。但是所有以前的自定义行编辑文本的值也更改为 0。而且当我单击取消图像时我无法删除行。 请检查 Activity
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);
}
}
}
}
我的适配器class:
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;
}
}
为什么不使用 recyclerview 而不是 listview?
无论如何,如果您打算使用列表视图,您要做的就是在您的自定义适配器中创建一个方法。
像这样:
public void addData(OrderLevelsListBO orderLevelsListBO) {
data.add(orderLevelsListBO);
}
添加项目时调用此方法。
reorderAdapter.addData(orderLevelsListBO);