下午好,各位:)
我有一个添加目标的列表视图(不重要:p)。listview的子程序有一些文本视图和编辑文本框,用户可以在这些文本框中填写其信息。我有一个Textwatcher,它检查listview的一个子程序中的文本是否已更改,因此数据可以从listview+全局数组(用于在活动之间传递数据,但这也不重要)写入数组中的类中。
现在我的问题是,当我将一个项目添加到列表中时,编辑文本“描述”中所有以前存在的项目的文本都会更改新添加的项的文本。例如,当列表视图中有一项显示“测试”时,我在列表中添加了一个新的项,上面写着“这是一个新的测试”,>>都会更改为“这是一个新的测试”。我调试了很多程序,并注意到,在最后(添加了新的项目),该程序将通过与‘’=‘这是一个新的测试’和位置= '0‘的文本处理程序。这可能会导致第一篇文章的文字发生变化。但我不明白为什么会这样。
有人能帮我吗?
这是我的列表:
class listViewAdapter extends BaseAdapter {
private Context context;
private ArrayList<Goals> list;
public listViewAdapter(final Context context, ArrayList<Goals> list) {
this.context = context;
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public void synchronise(){
geldclass.setGoals_list(this.list);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
final LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.goal_item, parent,
false);
}
Goals goal = (Goals) getItem(position);
final EditText txtdiscription = (EditText) convertView.findViewById(R.id.editDiscription);
txtdiscription.setText(goal.getDiscription());
txtdiscription.addTextChangedListener(new textWatcher(txtdiscription, position));
final EditText txtgoal = (EditText) convertView
.findViewById(R.id.editGoal);
txtgoal.addTextChangedListener(new textWatcher(txtgoal, position));
final EditText txtfrom = (EditText) convertView
.findViewById(R.id.editFrom);
txtfrom.addTextChangedListener(new textWatcher(txtfrom, position));
final EditText txtto = (EditText) convertView
.findViewById(R.id.editto);
txtto.addTextChangedListener(new textWatcher(txtto, position));
final CheckBox check_date = (CheckBox) convertView
.findViewById(R.id.check_enable_date);
check_date
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
}
});
return convertView;
}
private class textWatcher implements TextWatcher {
private View view;
private int position;
private textWatcher(View view, int position) {
this.view = view;
this.position = position;
}
@Override
public void afterTextChanged(Editable s) {
switch (view.getId()) {
case R.id.editDiscription:
list.get(position).setDiscription(s.toString());
synchronise();
break;
case R.id.editGoal:
list.get(position).setGoalvalue(Integer.parseInt(s.toString()));
synchronise();
break;
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
}
}
}这是我在xml中的目标子项:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textDiscription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:text="@string/name"
android:textSize="15dp"
android:width="80dp" />
<EditText
android:id="@+id/editDiscription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textNoSuggestions"
android:width="120dp" >
</EditText>
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textGoal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:text="@string/goal"
android:textSize="15dp" />
<EditText
android:id="@+id/editGoal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textNoSuggestions"
android:width="200dp" >
</EditText>
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<CheckBox
android:id="@+id/check_enable_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/from"
android:textSize="15dp" />
<EditText
android:id="@+id/editFrom"
android:enabled="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="date"
android:ems="10" >
</EditText>
</TableRow>
<TableRow
android:id="@+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textfrom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:text="@string/to"
android:textSize="15dp" />
<EditText
android:id="@+id/editto"
android:enabled="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="date"
android:ems="10" >
</EditText>
</TableRow>
</TableLayout>这里是我添加项目的地方(不要注意“明胶类”)。它是存储全局数组的全局类):
public void onClick(View v) {
Goals goal = new Goals("this is a new test");
geldclass.addGoals(goal);
goal_adapter = new listViewAdapter(GoalManager.this, geldclass.getGoals_list());
goal_list.setAdapter(goal_adapter);
}发布于 2013-08-16 02:19:07
public void onClick(View v) {
Goals goal = new Goals("this is a new test");
geldclass.addGoals(goal);
goal_adapter = new listViewAdapter(GoalManager.this, geldclass.getGoals_list());
goal_list.setAdapter(goal_adapter);
}应改为
public void onClick(View v) {
Goals goal = new Goals("this is a new test");
geldclass.addGoals(goal);
goal_adapter.setNotifyDatasetChanges();
}调用setNotifyDatasetChanges() make Adapter以使用其列表中新更改的数据刷新listView,我假设geldclass.getGoals_list()与您之前初始化Adapter的列表相同。
https://stackoverflow.com/questions/11543900
复制相似问题