我在listview适配器中从EditText获取文本时遇到问题。例如,当我点击" 9"时,它并没有给我9.相反,我总是得到3.我无法弄清楚这背后的原因。请告诉我我哪里出错了。
这是我的代码供你细读。提前谢谢!
class Planet {
String name;
int distance;
String quantità;
boolean selected = false;
public Planet(String name, int distance, String quantità) {
super();
this.name = name;
this.distance = distance;
this.quantità = quantità;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getDistance() {
return distance;
}
public void setDistance(int distance) {
this.distance = distance;
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
public String getQuantità() {
return quantità;
}
public void setQuantità(String quantità) {
this.quantità = quantità;
}
}
public class PlanetAdapter extends ArrayAdapter<Planet>{
private List<Planet> planetList;
private Context context;
ArrayList<Planet> objects;
public PlanetAdapter(List<Planet> planetList, Context context) {
super(context, R.layout.single_listview_item, planetList);
this.planetList = planetList;
this.context = context;
}
public static class PlanetHolder {
public TextView planetName;
public TextView distView;
public CheckBox chkBox;
public EditText edit;
public String quantità="3";
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
final PlanetHolder holder = new PlanetHolder();
if(convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.single_listview_item, null);
holder.planetName = (TextView) v.findViewById(R.id.name);
holder.distView = (TextView) v.findViewById(R.id.dist);
holder.chkBox = (CheckBox) v.findViewById(R.id.chk_box);
holder.edit = (EditText) v.findViewById(R.id.editText);
holder.edit.setVisibility(View.GONE);
holder.edit.setEnabled(false);
holder.chkBox.setOnCheckedChangeListener((MainActivity) context);
//
holder.chkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (holder.chkBox.isChecked()) {
holder.edit.setVisibility(View.VISIBLE);
holder.edit.setEnabled(true);
holder.edit.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) {
holder.quantità = holder.edit.getText().toString();
System.out.println("leggo questo dalla edittext:"+ holder.edit.getText().toString());
}
@Override
public void afterTextChanged(Editable s) {
}
});
} else {
holder.edit.setVisibility(View.GONE);
holder.edit.setEnabled(false);
holder.edit.setText(null);
}
}
});
} else {
}
Planet p = planetList.get(position);
p.setQuantità(holder.quantità);
holder.planetName.setText(p.getName());
holder.distView.setText("" + p.getDistance());
holder.chkBox.setChecked(p.isSelected());
holder.chkBox.setTag(p);
holder.edit.setEnabled(false);
return v;
}
ArrayList<Planet> getBox() {
ArrayList<Planet> box = new ArrayList<Planet>();
for (Planet p : planetList) {
if (p.selected)
box.add(p);
}
return box;
}
}
答案 0 :(得分:0)
您可以通过这种方式获取列表视图的文本,在活动或片段中使用此文本。这里的项目是一个arraylist。
for (int i = 0; i < items.size(); i++) {
View wantedView = recyclerView.getChildAt(i);
EditText editqty = (EditText) wantedView.findViewById(R.id.edtTxtQty);
if ((editqty.getText().toString().length() > 0)) {
//store edit text value in any arraylist or array by editqty.getText().toString()
}
}
答案 1 :(得分:0)
您拥有Excel
课程的固定价值,因此您始终可以获得价值&#39; 3&#39;
PlanetHolder
删除 public static class PlanetHolder {
public TextView planetName;
public TextView distView;
public CheckBox chkBox;
public EditText edit;
public String quantità="3";
}
修正值。
,第二个是您已声明public String quantità
,但如果有条件,您可以查看View v = convertView;
,请将if(convertView == null) {
替换为convertView
答案 2 :(得分:0)
用这个替换你的getView()方法:
level