我有复选框,并和的EditText在ListView一个TextView。 它从一个列表的文本视图获取价值。 复选框,将进行动态检查。 以同样的方式还的EditText可以动态输入。 现在,我的问题是,当我滚动列表视图进入编辑文本的文本后(上下),我无法得到的类型值。 我选中该复选框也是这样的。 但是,使用位置,我将它设置正确。 我无法知道如何的EditText值正确设置到列表中。 请帮我。 这里是我的代码:
main.xml中:(发射升空主XML)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/my_list"
android:layout_width="fill_parent"
android:layout_height="250px" />
<Button
android:text="Save"
android:id="@+id/btnSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
row.xml:(ListView的行)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@+id/label"
android:textSize="30sp"/>
<CheckBox
android:id="@+id/check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:text=""
android:id="@+id/txtAddress"
android:layout_width="150px"
android:layout_height="wrap_content"/>
</LinearLayout>
Model.Java:(这是POJO类)
package com.checkboxlistview;
public class Model {
private String name;
private boolean selected;
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public void setName(String name) {
this.name = name;
}
public Model(String name) {
this.name = name;
}
public String getName() {
return name;
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
}
MyAdapter.Java:(这是用于使用所述转换器和保持器保持在列表视图中的视图)
package com.checkboxlistview;
import java.util.List;
import android.app.Activity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
public class MyAdapter extends ArrayAdapter<Model> implements TextWatcher {
private final List<Model> list;
private final Activity context;
int listPosititon;
public MyAdapter(Activity context, List<Model> list) {
super(context, R.layout.row, list);
this.context = context;
this.list = list;
}
static class ViewHolder {
protected TextView text;
protected CheckBox checkbox;
protected EditText address;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
listPosititon = position;
ViewHolder viewHolder = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
convertView = inflator.inflate(R.layout.row, null);
viewHolder = new ViewHolder();
viewHolder.text = (TextView) convertView.findViewById(R.id.label);
viewHolder.checkbox = (CheckBox) convertView
.findViewById(R.id.check);
viewHolder.address = (EditText) convertView
.findViewById(R.id.txtAddress);
viewHolder.checkbox
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
int getPosition = (Integer) buttonView.getTag();
//Here we get the position that we have set for the checkbox using setTag.
list.get(getPosition).setSelected(
buttonView.isChecked());
// Set the value of checkbox to maintain its state.
}
});
viewHolder.address.addTextChangedListener(this);
convertView.setTag(viewHolder);
convertView.setTag(R.id.label, viewHolder.text);
convertView.setTag(R.id.check, viewHolder.checkbox);
convertView.setTag(R.id.txtAddress, viewHolder.address);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.checkbox.setTag(position); // This line is important.
viewHolder.text.setText(list.get(position).getName());
viewHolder.checkbox.setChecked(list.get(position).isSelected());
if (list.get(position).getAddress() != null) {
viewHolder.address.setText(list.get(position).getAddress() + "");
} else {
viewHolder.address.setText("");
}
return convertView;
}
@Override
public void afterTextChanged(Editable s) {
list.get(listPosititon).setAddress(s.toString());
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
}
MainActivity.java(这是活动):
package com.checkboxlistview;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity {
ListView listView;
Button btnSave;
ArrayAdapter<Model> adapter;
List<Model> list = new ArrayList<Model>();
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
listView = (ListView) findViewById(R.id.my_list);
btnSave = (Button)findViewById(R.id.btnSave);
adapter = new MyAdapter(this,getModel());
listView.setAdapter(adapter);
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for (int i = 0; i < list.size(); i++) {
Toast.makeText(getBaseContext(), "Name : "+list.get(i).getName() +" Selected: "+list.get(i).isSelected(), Toast.LENGTH_SHORT).show();
}
}
});
}
private List<Model> getModel() {
list.add(new Model("Linux"));
list.add(new Model("Windows7"));
list.add(new Model("Suse"));
list.add(new Model("Eclipse"));
list.add(new Model("Ubuntu"));
list.add(new Model("Solaris"));
list.add(new Model("Android"));
list.add(new Model("iPhone"));
list.add(new Model("Java"));
list.add(new Model(".Net"));
list.add(new Model("PHP"));
return list;
}
}
有代码中没有错误。 它运行良好。 我可以保持在同一位置的复选框位置和显示连我向上和向下滚动。 但我不能得到妥善设置的EditText值。 请帮助我。 提前致谢。