2013-03-27 210 views
12

UPDATE:如何停止敬酒&alertDialog我过滤的EditText失去焦点

最新更新 - getChanges()方法已被添加。

第二次更新 - 我已经添加了整个我的ShoppingList.java类。

第一次更新 - 2人喜欢这个问题但没有答案后,我已经打开了这个问题的赏金。

问:

我有类似的问题,这个地方我不能给我一次开始新的意图重新筛选我的ListView,然后返回到原来的页面。这是通过使用onResume()方法并从其他过滤器方法中调用我的过滤器代码来解决的。

我最近的一个问题是我的应用程序页面上应该使用dialogBu​​ilder或toast消息,然后再次过滤文本被空白,即任何输入到我的过滤器EditText的文本都被我的过滤器忽略。

这里有一些截图突出问题:

的项目加载的ListView:

enter image description here

一个搜索词输入到过滤器的EditText和正确筛选:

enter image description here

第一项'A'被编辑为'AB'。敬酒消息确认操作:

enter image description here

这是问题,dialogbuilder(这是该项目是如何编辑)和烤面包的消息是完整的,新的过滤器术语订立的EditText和过滤不再过滤器:

enter image description here

这里是我的过滤器代码:

package com.example.flybaseapp; 



    public class ShoppingList extends ListActivity implements OnClickListener { 

Button AddItem; 
Button showShop; 
ListView showItems; 
SimpleCursorAdapter cursorAdapter; 
Long itemId; 
TextView totalPrice; 
String itemDescription; 
int itemAmount; 
int itemPrice; 
EditText itemNameEdit; 
DBHandlerShop getCons; 
Dialog e1; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.shoppinglistlayout); 

    AddItem = (Button) findViewById(R.id.btnAddItem); 
    showShop = (Button) findViewById(R.id.btnSearchShops); 

    showItems = (ListView) findViewById(android.R.id.list); 

    totalPrice = (TextView) findViewById(R.id.totalListPrice); 

    AddItem.setOnClickListener(this); 
    showShop.setOnClickListener(this); 

    setList(); 

    int setPrice = updateTotal(); 
    totalPrice.setText(Integer.toString(setPrice)); 

    itemNameEdit = (EditText) findViewById(R.id.inputItemName); 

    showItems.setTextFilterEnabled(true); 

    itemNameEdit.addTextChangedListener(new TextWatcher() { 

     @Override 
     public void afterTextChanged(Editable s) { 

     } 

     @Override 
     public void beforeTextChanged(CharSequence s, int start, int count, 
       int after) { 

     } 

     @Override 
     public void onTextChanged(CharSequence s, int start, int before, 
       int count) { 

      cursorAdapter.getFilter().filter(s.toString()); 
      showItems.refreshDrawableState(); 

     } 

    }); 

    getCons = new DBHandlerShop(this, null, null); 
    getCons.open(); 
    cursorAdapter.setFilterQueryProvider(new FilterQueryProvider() { 

     public Cursor runQuery(CharSequence constraint) { 

      return getCons.getChanges((constraint.toString())); 

     } 

    }); 

    showItems.setAdapter(cursorAdapter); 

} 

@Override 
public void onClick(View clickedAdd) { 

    switch (clickedAdd.getId()) { 

    case (R.id.btnAddItem): 

     show(); 

     break; 

    case (R.id.btnSearchShops): 

     Intent checkGPS = new Intent("com.example.flybaseapp.CheckGPS"); 
     startActivity(checkGPS); 

     break; 

    } 

} 

@Override 
protected void onListItemClick(ListView l, View v, int position, long idd) { 
    super.onListItemClick(l, v, position, idd); 

    itemId = idd; 

    final CharSequence[] items = { "Edit Item", "Delete Item" }; 

    Builder alertDialogBuilder = new AlertDialog.Builder(ShoppingList.this); 

    alertDialogBuilder.setTitle("Item Options:"); 

    alertDialogBuilder.setItems(items, 
      new DialogInterface.OnClickListener() { 

       public void onClick(DialogInterface dialog, int item) { 

        if (items[item].equals("Edit Item")) { 

         AlertDialog.Builder builder = new AlertDialog.Builder(
           ShoppingList.this); 

         builder.setTitle("Edit Item"); 

         DBHandlerShop setEdit = new DBHandlerShop(
           ShoppingList.this, null, null); 

         setEdit.open(); 
         String itemName = setEdit.getItem(itemId); 
         int itemAmount = setEdit.getItemQuan(itemId); 
         int itemPrice = setEdit.getItemCost(itemId); 
         setEdit.close(); 

         LinearLayout layout = new LinearLayout(
           ShoppingList.this); 
         layout.setOrientation(LinearLayout.VERTICAL); 

         final EditText titleBox = new EditText(
           ShoppingList.this); 
         titleBox.setText(itemName); 
         titleBox.setHint("Item Name:"); 
         layout.addView(titleBox); 

         final EditText quantityBox = new EditText(
           ShoppingList.this); 
         quantityBox.setText(Integer.toString(itemAmount)); 
         quantityBox.setHint("Item Quantity"); 
         layout.addView(quantityBox); 

         final EditText priceBox = new EditText(
           ShoppingList.this); 
         priceBox.setText(Integer.toString(itemPrice)); 
         priceBox.setHint("Item Price."); 
         layout.addView(priceBox); 

         builder.setView(layout); 

         builder.setPositiveButton("Ok", 
           new DialogInterface.OnClickListener() { 
            public void onClick(
              DialogInterface dialog, 
              int whichButton) { 

             Editable valueItem = titleBox 
               .getText(); 
             Editable valueAmount = quantityBox 
               .getText(); 
             Editable valuePrice = priceBox 
               .getText(); 

             String itemDescription = valueItem 
               .toString(); 
             String s = valueAmount.toString(); 
             int itemAmount = Integer 
               .parseInt(s); 
             String a = valuePrice.toString(); 
             int itemPrice = Integer.parseInt(a); 

             try { 
              DBHandlerShop update = new DBHandlerShop(
                ShoppingList.this, 
                null, null); 

              int totalCombined = itemAmount 
                * itemPrice; 

              update.open(); 
              update.updateItem(itemId, 
                itemDescription, 
                itemAmount, itemPrice); 
              update.close(); 

              int setPrice = updateTotal(); 
              totalPrice.setText(Integer 
                .toString(setPrice)); 

             } catch (Exception e) { 

              Toast.makeText(
                getApplicationContext(), 
                "Items not updated.", 
                Toast.LENGTH_SHORT) 
                .show(); 

             } finally { 

              Toast.makeText(
                getApplicationContext(), 
                "Items updated.", 
                Toast.LENGTH_SHORT) 
                .show(); 

              setList(); 

             } 

            } 

           }); 

         builder.setNegativeButton("Cancel", 
           new DialogInterface.OnClickListener() { 
            public void onClick(
              DialogInterface dialog, 
              int whichButton) { 

            } 
           }); 

         builder.show(); 

        } 

        else if (items[item].equals("Delete Item")) { 
         try { 

          DBHandlerShop delete = new DBHandlerShop(
            ShoppingList.this, null, null); 

          delete.open(); 
          delete.deleteItem(itemId); 
          delete.close(); 

          DBHandlerShop findPrice = new DBHandlerShop(
            ShoppingList.this, null, null); 

          findPrice.open(); 
          int returnedCost = findPrice 
            .getItemCost(itemId); 
          findPrice.close(); 

          int cost = updateTotal(); 

          int newTotal = cost - returnedCost; 
          totalPrice.setText(Integer.toString(newTotal)); 
         } 

         catch (Exception e) { 

          Toast.makeText(getApplicationContext(), 
            "Item failed to be deleted.", 
            Toast.LENGTH_SHORT).show(); 
         } 

         finally { 

          Toast.makeText(getApplicationContext(), 
            "Item deleted from the list.", 
            Toast.LENGTH_SHORT).show(); 

          setList(); 
         } 

        } 

       } 

      }); 

    alertDialogBuilder.show(); 

} 

@SuppressWarnings("deprecation") 
private void setList() { 

    DBHandlerShop DBShop = new DBHandlerShop(this, null, null); 

    DBHandlerShop searchItems = new DBHandlerShop(this, null, null); 

    searchItems.open(); 

    Cursor cursor = searchItems.getItems(); 

    startManagingCursor(cursor); 

    searchItems.close(); 

    String[] from = new String[] { DBShop.KEY_ITEMSHOP, DBShop.KEY_ITEMNUM, 
      DBShop.KEY_ITEMPRICE }; 
    int[] to = new int[] { R.id.txtSetItem, R.id.txtSetAmount, 
      R.id.txtSetPrice }; 

    cursorAdapter = new SimpleCursorAdapter(this, R.layout.setshoppinglist, 
      cursor, from, to); 
    showItems.setAdapter(cursorAdapter); 

} 

private int updateTotal() { 

    DBHandlerShop total = new DBHandlerShop(this, null, null); 

    int totalPrice = 0; 
    total.open(); 
    Cursor totalPrices = total.getTotals(); 
    total.close(); 

    if (totalPrices != null) { 

     startManagingCursor(totalPrices); 
     if (totalPrices.moveToFirst()) { 

      do { 
       int cost = totalPrices.getInt(3); 
       int amount = totalPrices.getInt(2); 

       int totalCost = cost * amount; 
       totalPrice += totalCost; 

      } while (totalPrices.moveToNext()); 

      return totalPrice; 
     } 

    } 

    else { 

     return totalPrice; 

    } 

    return 0; 

} 

private void show() { 

    AlertDialog.Builder builder = new AlertDialog.Builder(ShoppingList.this); 

    builder.setTitle("Enter Item Details:"); 

    LinearLayout layout = new LinearLayout(this); 
    layout.setOrientation(LinearLayout.VERTICAL); 

    final EditText titleBox = new EditText(this); 

    titleBox.setHint("Item Name:"); 
    layout.addView(titleBox); 

    final EditText quantityBox = new EditText(this); 

    quantityBox.setHint("Item Quantity"); 
    layout.addView(quantityBox); 

    final EditText priceBox = new EditText(this); 

    priceBox.setHint("Item Price."); 
    layout.addView(priceBox); 

    builder.setView(layout); 

    builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int whichButton) { 
      try { 

       Editable valueItem = titleBox.getText(); 
       Editable valueAmount = quantityBox.getText(); 
       Editable valuePrice = priceBox.getText(); 

       itemDescription = valueItem.toString(); 
       String s = valueAmount.toString(); 
       itemAmount = Integer.parseInt(s); 
       String a = valuePrice.toString(); 
       itemPrice = Integer.parseInt(a); 

       DBHandlerShop addItem = new DBHandlerShop(
         ShoppingList.this, null, null); 
       addItem.open(); 
       addItem.insertItems(itemDescription, itemAmount, itemPrice); 
       addItem.close(); 

      } catch (Exception e) { 

       Toast.makeText(getApplicationContext(), 
         "Item failed to be added", Toast.LENGTH_SHORT) 
         .show(); 

      } finally { 

       Toast.makeText(getApplicationContext(), 
         "Item added to your list", Toast.LENGTH_SHORT) 
         .show(); 

       int cost = updateTotal(); 
       totalPrice.setText(Integer.toString(cost)); 

       setList(); 

      } 

     } 

    }); 

    builder.setNegativeButton("Cancel", 
      new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int whichButton) { 

       } 
      }); 

    builder.show(); 

} 

@Override 
protected void onResume() { 
    super.onResume(); 

    setList(); 

    showItems.setTextFilterEnabled(true); 

    itemNameEdit.addTextChangedListener(new TextWatcher() { 

     @Override 
     public void afterTextChanged(Editable s) { 

     } 

     @Override 
     public void beforeTextChanged(CharSequence s, int start, int count, 
       int after) { 

     } 

     @Override 
     public void onTextChanged(CharSequence s, int start, int before, 
       int count) { 

      cursorAdapter.getFilter().filter(s.toString()); 
      showItems.refreshDrawableState(); 

     } 

    }); 

    getCons = new DBHandlerShop(this, null, null); 
    getCons.open(); 
    cursorAdapter.setFilterQueryProvider(new FilterQueryProvider() { 

     public Cursor runQuery(CharSequence constraint) { 

      return getCons.getChanges((constraint.toString())); 

     } 

    }); 

    showItems.setAdapter(cursorAdapter); 
} 

} 

getChanges()从数据库处理程序类:

public Cursor getChanges(String constraintPassed) { 

     String [] columns = new String[]{KEY_ROWSHOPID, KEY_ITEMSHOP, KEY_ITEMNUM, KEY_ITEMPRICE}; 
     Cursor c = null; 
     if(constraintPassed.equals("")) 
     { 
      c = ourDatabase.query(DATABASE_TABLESHOP, columns, null, null, null, null, null); 

     } 

     else 
     { 
      c = ourDatabase.query(DATABASE_TABLESHOP, columns, KEY_ITEMSHOP + " LIKE'" + constraintPassed + "%'", null, null, null, KEY_ITEMSHOP + " ASC", null); 
     } 

     if(c != null) 
      { 
       c.moveToFirst(); 

      } 
     return c; 
    } 

我需要一次编辑已经为实施生命周期方法?如果是这样的话,有人会把我推向正确的方向,因为我试过onResume()和onRestart()都无济于事。

+2

告诉我们更多的代码..... – 2013-03-29 13:02:54

+0

@DhavalSodhaParmar我已经添加了整个ShoppingList类的代码。 – user1352057 2013-03-29 13:58:20

+0

getChanges()方法的外观如何? – Luksprog 2013-03-29 14:12:23

回答

1

据我所知,你多次运行getCons.open();而没有关闭它或在两者之间。我不知道open()方法是忽略多次呼叫还是多次呼叫都会导致错误,但是您可能需要尝试将getCons.open();直接移至getCons = new DBHandlerShop(this, null, null);以下,以解决该问题。

+0

感谢您的回复。我试图在我的DBHandler对象下移动我的数据库.open()方法,但遗憾的是它仍然不会在alertdialog&toast消息后过滤 – user1352057 2013-03-29 13:50:05

2

更新过滤器后,尝试在适配器上调用notifyDataSetChanged()。这应该通知ListView它也需要刷新它的数据。

+0

感谢您的回答。我相信notifyDataSetChanged();现已折旧。在发布这个问题之前,我尝试了这个,可悲的是它没有成功。 – user1352057 2013-03-29 18:23:14

+0

嗯,这很奇怪。但它不被弃用。至少它不会在文档中这么说,并且不会在Eclipse中显示弃用的警告。 – Thrakbad 2013-03-30 09:11:04

1

一个简单的解决这个问题的方法我发现,只需在完成编辑后启动一个新的intent对象来调用该类。这仍然使整个过程的整体操作流畅而快速,当然也允许我在编辑后进行过滤。所以暂时来说,这就是我如何去配合它。

1

我认为你应该使用

android:hapticFeedbackEnabled="true" 
android:imeOptions="actionNext" 
android:nextFocusUp="@id/editeText1" 
android:nextFocusLeft="@id/edittextText" 
android:includeFontPadding="true" 
1

我看到什么是你的情况发生。用户每次成功编辑一个项目时,都会为您的列表设置一个新的适配器!而在你的情况下,也TextWatcher添加到您的的EditText的onCreate方法是使用第一个适配器(在onTextChanged法)在的onCreate方法还创建了第一次的活动已创建!

有几种方法可以解决这个问题。一种方法是改变你实施活动的整个方式(我个人不会这样做)。

另一种方式是简单地改变SETLIST方法,这就是所谓的的onCreate方法的第一次,每一次用户成功地进行的项目。这是我将详细解释的解决方案,因为它既快速又简单,且不耗时间:

  • 如果列表中没有适配器,则创建一个。
  • 如果列表已经有适配器,那么只需更改适配器的光标并刷新列表。

所以你SETLIST方法应该是这样的:

@SuppressWarnings("deprecation") 
private void setList() { 

    DBHandlerShop DBShop = new DBHandlerShop(this, null, null); 

    DBHandlerShop searchItems = new DBHandlerShop(this, null, null); 

    searchItems.open(); 

    Cursor cursor = searchItems.getItems(); 

    startManagingCursor(cursor); 

    searchItems.close(); 

    String[] from = new String [] { DBShop.KEY_ITEMSHOP , DBShop.KEY_ITEMNUM, DBShop.KEY_ITEMPRICE }; 
    int[] to = new int[] { R.id.txtSetItem, R.id.txtSetAmount, R.id.txtSetPrice }; 

    // Check the cursor adapter is previously created 
    if (cursorAdapter == null) { 
     // There is no previous cursors, create a new one 
     cursorAdapter = new SimpleCursorAdapter(this, R.layout.setshoppinglist, cursor, from, to); 
     showItems.setAdapter(cursorAdapter); 
    } 
    else { 
     // There is a previous adapter 
     // Stop managing its cursor 
     stopManagingCursor (cursorAdapter.getCursor()); 
     // Assign the new cursor to the current adapter 
     // The old cursor will be closed 
     cursorAdapter.changeCursor (cursor); 
     // No need to refresh the list, it will be automatically refreshed after the adapter's cursor is changed 
    } 

} 

以这种方式,列表的适配器是,TextWatcher使用过滤列表中的一个相同。它应该工作,试一试,让我知道会发生什么。

1

你可以试试下面的变化..

显示你的面包项目更新/删除的消息称此

cursorAdapter.notifyDataSetChanged(); 

在你SETLIST()方法中添加以下后..

cursorAdapter.registerDataSetObserver(new DataSetObserver() { 
@Override 
public void onChanged() { 
    super.onChanged(); 
    setList(); 
}}); 

并将代码移动到setList()

itemNameEdit.addTextChangedListener(new TextWatcher() { 

     @Override 
     public void afterTextChanged(Editable s) { 

     } 

     @Override 
     public void beforeTextChanged(CharSequence s, int start, int count, 
             int after) { 

     } 

     @Override 
     public void onTextChanged(CharSequence s, int start, int before, 
            int count) { 

      cursorAdapter.getFilter().filter(s.toString()); 
      showItems.refreshDrawableState(); 

     } 

    }); 

    getCons = new DBHandlerShop(this, null, null); 
    getCons.open(); 
    cursorAdapter.setFilterQueryProvider(new FilterQueryProvider() { 

     public Cursor runQuery(CharSequence constraint) { 

      return getCons.getChanges((constraint.toString())); 

     } 

    });