我试图在ListView中保存Edittext的值并且它可以工作,但是当ListView太大时,当我编辑1个edittext时,如果我滚动列表视图,则另一个EditText同时编辑...我不知道该怎么办,我浪费了6个小时,什么也没得到。如果有人可以帮助我,请。
这是自定义列表视图的getView的代码
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final int position2 = position;
View v = convertView;
final ViewHolder holder;
if(v == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.lista_item_prepedido, null);
holder = new ViewHolder();
holder.codi = (TextView) v.findViewById(R.id.NomProducte);
holder.nom = (EditText) v.findViewById(R.id.Preu);
holder.eliminar = (ImageButton) v.findViewById(R.id.delete);
View eliminarPrepedido = (ImageButton) v.findViewById(R.id.delete);
if(blocClients.equals("0"))
{
eliminarPrepedido.setVisibility(View.GONE);
holder.nom.setFocusable(false);
}
v.setTag(holder);
holder.nom.setText(items.get(position).getNom());
holder.nom.addTextChangedListener(new TextWatcher(){
@Override
public void afterTextChanged(Editable s) {
}
@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) {
items.get(position2).Nom = s.toString();
String preu = items.get(position2).Nom;
String nomproducte = items.get(position2).Codic;
int position = position2;
//mostrarMensaje("Valor: "+preu+" Nom Producte: "+nomproducte+" CodiClient: " +CodicClient+ " posicio: "+position);
baseDatos = openOrCreateDatabase(nombreBD, MODE_WORLD_WRITEABLE, null);
String sqlStr = "UPDATE estadistiques SET ultimpreu = '" +preu+"' WHERE codic_client='"+CodicClient+"' AND desc_article = '"+nomproducte+"' ";
baseDatos.execSQL(sqlStr);
try {
fnGlobal.CreaSincroFitxer(sqlStr);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
baseDatos.close();
}
});
} else {
holder = (ViewHolder) v.getTag();
}
holder.codi.setText(items.get(position).getCodic());
holder.eliminar.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
final int position = position2;
String CodicClient = null;
Bundle extras = getIntent().getExtras();
CodicClient = extras.getString("codiClientTab");
String nomproducte = items.get(position).Codic;
int Opcio = 1;
baseDatos = openOrCreateDatabase(nombreBD, MODE_WORLD_WRITEABLE, null);
String sqlStr = "DELETE FROM estadistiques WHERE codic_client ='"+CodicClient+"' AND desc_article ='"+nomproducte+"'" ;
baseDatos.execSQL(sqlStr);
try {
fnGlobal.CreaSincroFitxer(sqlStr);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
baseDatos.close();
notifyDataSetChanged();
onResume();
}
});
return v;
}
}
listview的自定义XML项目
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/item"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:background="@drawable/rounded_rectangle"
android:orientation="horizontal"
android:padding="6dip" >
<ImageButton
android:id="@+id/delete"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginRight="6dp"
android:layout_marginTop="15dp"
android:background="@drawable/delete" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<EditText
android:id="@+id/Preu"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/NomProducte"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="5dp"
android:background="@color/white"
android:ellipsize="end"
android:gravity="right"
android:inputType="numberDecimal"
android:singleLine="true"
android:text="nom"
android:textSize="18sp" />
<TextView
android:id="@+id/NomProducte"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/Preu"
android:layout_alignParentLeft="true"
android:layout_marginLeft="5dp"
android:layout_weight="0.16"
android:clickable="false"
android:ellipsize="end"
android:focusable="false"
android:focusableInTouchMode="false"
android:singleLine="true"
android:text="codi"
android:textColor="#000000"
android:textSize="18sp" />
</RelativeLayout>
</LinearLayout>
非常感谢! :)
答案 0 :(得分:0)
如果我已经理解了你的问题,那么很难让它总是以一种容易理解的方式表现出来。我建议你改变你实现它的方式,在ListView中有两个TextViews显示两条信息。当点击可编辑的对话框时,调出一个对话框来编辑信息,然后用户必须在移动列表视图之前接受或取消该信息,以明确发生了什么。总之,我建议: