2013-03-27 57 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())); 

     } 

    });