我在從列表視圖適配器中的EditText獲取文本時遇到問題。例如,當我點擊「9」時,它不會給我9.相反,我總是得到3.我無法找出背後的原因。請告訴我我哪裏錯了。如何使用ListView和適配器在EditText中獲取文本
這是我的代碼,供您細讀。先謝謝你!
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;
}
}
共享代碼和適配器。 –
請通過編輯您的問題更新您的代碼,不要將它添加到答案 –
我有這個錯誤:「看起來您的文章主要是代碼,請添加一些更多詳細信息」。爲什麼? –