这不是一个重复的问题请完整阅读我已经搜索了所有以前的问题,但是没有找到任何合适的答案。
我正在尝试在Listview中保留Edittext值,这是使用baseadapter类填充的。
public class ServicesListAdapter extends BaseAdapter {
LayoutInflater vi;
Context context;
ArrayList<ServiceListItems> contactList;
int qty,prc;
public int totalpricestr;
ServiceListItems contactListItems;
private static SharedPreferences prefs;
String ab;
private static final String LIST_STATE = "listState";
private Parcelable mListState = null;
public static final String MY_ORDER_LIST = "orderlist_retrive";
public ServicesListAdapter(Context context, ArrayList<ServiceListItems> list) {
vi = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.context = context;
contactList = list;
}
@Override
public int getCount() {
return contactList.size();
}
@Override
public Object getItem(int position) {
return contactList.get(position);
}
@Override
public long getItemId(int position) {
return 500;
}
@Override
public View getView(final int position, View convertView, ViewGroup arg2) {
contactListItems = contactList.get(position);
final ViewHolder holder;
if (convertView == null) {
convertView = vi.inflate(R.layout.services_row, null);
holder = new ViewHolder();
holder.idtxt = (TextView) convertView.findViewById(R.id.idtxt);
holder.service = (TextView) convertView.findViewById(R.id.producttxt);
holder.price = (TextView) convertView.findViewById(R.id.pricetxt);
holder.editquantity = (TextView) convertView.findViewById(R.id.editquantity);
holder.plus = (Button) convertView.findViewById(R.id.plusbtn);
holder.minus = (Button) convertView.findViewById(R.id.minusbtn);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
String a = String.valueOf(contactListItems.getDbname()!=null);
holder.idtxt.setText(String.valueOf(contactListItems.getDbid()));
holder.service.setText(String.valueOf(contactListItems.getDbname()));
holder.price.setText(contactListItems.getDbprice());
holder.editquantity.setText(String.valueOf(contactListItems.getDbquantity()));
holder.plus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
qty = Integer.parseInt(holder.editquantity.getText().toString());
prc = Integer.parseInt(holder.price.getText().toString());
/* if (qty == 0) {
} else {*/
qty = qty + 1;
holder.editquantity.setText(String.valueOf(qty));
int id = Integer.parseInt(holder.idtxt.getText().toString());
SqlHandler db = new SqlHandler(context);
String query = "UPDATE SERVICES_LIST SET quantity = "+String.valueOf(qty)+" WHERE id= "+id+"" ;
Toast.makeText(context," "+id,Toast.LENGTH_LONG).show();
db.executeQuery(query);
Services i = new Services();
if (i.totalprice.getText().toString().length()>0){
prefs = context.getApplicationContext().getSharedPreferences(MY_ORDER_LIST, context.MODE_PRIVATE);
int u = prefs.getInt("totalsp", 0);
totalpricestr = prc+u;
}else{
totalpricestr = totalpricestr+Integer.parseInt(holder.price.getText().toString());
}
i.totalprice.setText(String.valueOf(totalpricestr));
prefs = context.getApplicationContext().getSharedPreferences(MY_ORDER_LIST, context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putInt("totalsp", totalpricestr);
editor.commit();
}
});
holder.minus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
qty = Integer.parseInt(holder.editquantity.getText().toString());
prc = Integer.parseInt(holder.price.getText().toString());
if (qty == 0) {
} else {
qty = qty - 1;
holder.editquantity.setText(String.valueOf(qty));
int id = Integer.parseInt(holder.idtxt.getText().toString());
SqlHandler db = new SqlHandler(context);
String query = "UPDATE SERVICES_LIST SET quantity = "+String.valueOf(qty)+" WHERE id= "+String.valueOf(id)+"" ;
//String query = "DELETE FROM SERVICES_LIST";
Toast.makeText(context," "+query,Toast.LENGTH_LONG).show();
db.executeQuery(query);
Services i = new Services();
if(Integer.valueOf(i.totalprice.getText().toString())!=0){
if (i.totalprice.getText().toString().length()>0){
prefs = context.getApplicationContext().getSharedPreferences(MY_ORDER_LIST, context.MODE_PRIVATE);
int u = prefs.getInt("totalsp", 0);
totalpricestr = u-prc;
}else{
totalpricestr = totalpricestr-Integer.parseInt(holder.price.getText().toString());
}
i.totalprice.setText(String.valueOf(totalpricestr));
prefs = context.getApplicationContext().getSharedPreferences(MY_ORDER_LIST, context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putInt("totalsp", totalpricestr);
editor.commit();
}else{}
}
}
});
return convertView;
}
private static class ViewHolder {
public TextView idtxt;
public TextView service;
public TextView price;
public TextView editquantity;
public Button plus;
public Button minus;
}
现在我的问题是,当我点击holder.plus按钮时,我的edittext值增加1并且holder.minus按钮会递减,但是当我将SCROLL值恢复到之前的值时,它们将被回收。
我不希望我的行被回收或者如果回收如何保留值。 我已将我的列表视图声明为FRAGMENT
并编写了一个方法,用于将数据从sqlite DB显示到listview
public void showList() {
ArrayList<ServiceListItems> contactList = new ArrayList<ServiceListItems>();
contactList.clear();
String query = null;
query = "SELECT * FROM SERVICES_LIST WHERE category = 'QA'";
Cursor c1 = sqlHandler.selectQuery(query);
if (c1 != null && c1.getCount() != 0) ;
{
if (c1.moveToFirst()) {
do {
ServiceListItems contactListItems = new ServiceListItems();
contactListItems.setDbid(c1.getInt(c1
.getColumnIndex("id")));
contactListItems.setDbname(c1.getString(c1
.getColumnIndex("name")));
contactListItems.setDbprice(c1.getString(c1
.getColumnIndex("price")));
contactListItems.setDbquantity(c1.getInt(c1
.getColumnIndex("quantity")));
contactList.add(contactListItems);
} while (c1.moveToNext());
}
}
c1.close();
ServicesListAdapter contactListAdapter = new ServicesListAdapter(
getContext(), contactList);
list.setAdapter(contactListAdapter);
}
由于我已经编写了这个Fragment类,我无法从我已经显示的baseadapter中运行此方法。
当我使用这种方法时
Fragment o = new Fragment();
o.showlist(); // show list is not being detected
是否有任何方法可以从baseadapter中运行片段中的方法。
我的servicerow的xml文件(在baseadapter中声明)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp"
android:weightSum="10"
android:gravity="center"
android:background="#E6E6E6">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp"
android:weightSum="10">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="4"
android:orientation="vertical">
<TextView
android:id="@+id/idtxt"
android:layout_width="0dp"
android:layout_height="0dp" />
<TextView
android:id="@+id/producttxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Product"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/pricetxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Price"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="7"
android:orientation="horizontal">
<Button
android:id="@+id/plusbtn"
android:layout_width="12dp"
android:layout_height="30dp"
android:layout_weight="1"
android:background="@drawable/circle"
android:text="+"
android:textColor="#ffffff"
android:textSize="15dp" />
<TextView
android:id="@+id/editquantity"
android:layout_width="1dp"
android:layout_height="30dp"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:maxLines="1"
android:gravity="center"
android:text="0" />
<Button
android:id="@+id/minusbtn"
android:layout_width="12dp"
android:layout_height="30dp"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:background="@drawable/circle"
android:gravity="center"
android:text="-"
android:textColor="#ffffff" />
</LinearLayout>
</LinearLayout>
感谢每一个人的建议我尝试了不同的方法并解决了问题。
答案 0 :(得分:0)
好吧,我认为这会对你有所帮助。创建一个整数(键)和&amp;的HashMap。设置适配器之前的字符串(值)。类似的东西:
HashMap<Integer, String> itemsMap = new HashMap<Integer, String>();
现在,无论何时单击holder.plus按钮或holder.minus按钮,都要在地图中输入位置,如下所示:
itemsMap.put(position, qty);
改变了课程数量。最后,在getView()方法中,检查映射是否为null,并且该位置的值不为null。如果该位置存在值,则只需在该位置设置文本。
if (itemsMap != null && itemsMap.get(position) != null) {
holder.editquantity.setText.setText(itemsMap.get(rowId));
}
试试这个,我希望这会有所帮助。快乐的编码...