2013-12-16 79 views
2

我有一個listview與自定義的行填充adapter.when我點擊任何行,它打開一個新的活動,我想要的。一切工作正常,但當我將搜索過濾器功能添加到此列表中,並且我在edittext中搜索listitem並獲取了正確的過濾器,但是當我單擊過濾器項目時,它不打開與其關聯的活動。它打開與其他listitems相關的其他活動,但不是我點擊one.it是非常奇怪的我。我不粘貼完整的代碼在這裏只附加所需的代碼。從列表視圖中的edittext中搜索項顯示錯誤的結果

這裏是我的代碼,我啓用了搜索功能在listview上編輯文本。

edtSearch.addTextChangedListener(new TextWatcher() { 

      public void onTextChanged(CharSequence s, int start, int before, 
        int count) { 
       adapter.getFilter().filter(s); 
       adapter.notifyDataSetChanged(); 

      } 

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

      } 

      public void afterTextChanged(Editable s) { 
      } 
     }); 
    } 

如果有關於相同的任何查詢,請給我評論,任何幫助將不勝感激。

這裏是我的完整代碼:

package com.tomar.sixxsigma; 

import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.text.Editable; 
import android.text.TextWatcher; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.EditText; 
import android.widget.ListView; 
import android.widget.SimpleAdapter; 
import android.widget.TextView; 
import android.widget.AdapterView.OnItemClickListener; 

public class Tabtwo extends Activity implements OnItemClickListener { 

    ListView listView; 
    TextView txt; 

    ArrayAdapter<String> adapter; 

    // Search EditText 
    EditText edtSearch; 

    // Array of strings storing country names 
    String[] countries = new String[] { "Admin Cost", "Affinity Diagram", 
      "Analyse", "Apprasal Costs", "Assessment of Stakeholders", 
       }; 

    // Array of integers points to images stored in /res/drawable-ldpi/ 
    int[] flags = new int[] { R.drawable.admin, R.drawable.affinity, 
      R.drawable.analysis, R.drawable.appraisal, R.drawable.assessment, 
       }; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.tabtwo); 

     // Each row in the list stores country name, currency and flag 
     List<HashMap<String, String>> aList = new ArrayList<HashMap<String, String>>(); 

     for (int i = 0; i < 4; i++) { 
      HashMap<String, String> hm = new HashMap<String, String>(); 
      hm.put("txt", countries[i]); 

      hm.put("flag", Integer.toString(flags[i])); 
      aList.add(hm); 
     } 

     // Keys used in Hashmap 
     String[] from = { "flag", "txt" }; 

     // Ids of views in listview_layout 
     int[] to = { R.id.flag, R.id.txt }; 

     // Instantiating an adapter to store each items 
     // R.layout.listview_layout defines the layout of each item 
     final SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), 
       aList, R.layout.listview_layout, from, to); 

     // Getting a reference to listview of main.xml layout file 
     ListView listView = (ListView) findViewById(R.id.listview); 
     edtSearch = (EditText) findViewById(R.id.Search_box); 
     txt = (TextView) findViewById(R.id.txt); 
     listView.setOnItemClickListener(this); 
     // Setting the adapter to the listView 
     listView.setAdapter(adapter); 

     listView.setTextFilterEnabled(true); 
     listView.setOnItemClickListener(this); 
     edtSearch.addTextChangedListener(new TextWatcher() { 

      public void onTextChanged(CharSequence s, int start, int before, 
        int count) { 
       adapter.getFilter().filter(s); 
       adapter.notifyDataSetChanged(); 

      } 

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

      } 

      public void afterTextChanged(Editable s) { 
      } 
     }); 
    } 

    @Override 
    public void onItemClick(AdapterView<?> arg0, View arg1, int position, 
      long arg3) { 
     // TODO Auto-generated method stub 
     if (position == 0) { 
      Intent int0 = new Intent(getApplicationContext(), Admincost.class); 
      startActivity(int0); 
     } 

     if (position == 1) { 
      Intent int1 = new Intent(getApplicationContext(), Affinity.class); 
      startActivity(int1); 
     } 
     if (position == 2) { 
      Intent int2 = new Intent(getApplicationContext(), Analyse.class); 
      startActivity(int2); 
     } 

     if (position == 3) { 
      Intent int3 = new Intent(getApplicationContext(), 
        ApprasalCosts.class); 
      startActivity(int3); 
     } 
     if (position == 4) { 
      Intent int1 = new Intent(getApplicationContext(), Assessment.class); 
      startActivity(int1); 
     } } 
    } 
} 
+0

您必須清除包含數據的數組列表,並將其填充到您過濾列表視圖的位置。 –

+0

看到我的答案在下面,希望它會幫助你。 –

+0

告訴我們關於商品的點擊單擊監聽器 –

回答

0

請參閱下面的代碼: -

@Override 
       public void onTextChanged(CharSequence s, int start, int before, int count) { 
        // TODO Auto-generated method stub 

        ArrayList<String> STOREID_SUB = new ArrayList<String>(); 
        ArrayList<String> STORE_NAME_SUB = new ArrayList<String>(); 
        ArrayList<String> PHOTO_SUB = new ArrayList<String>(); 
        ArrayList<String> PLACE_STATUS_SUB = new ArrayList<String>(); 
        final ArrayList<String> STORE_DESC_SUB = new ArrayList<String>(); 

        textlength = searchText.getText().length(); 

        for (int i = 0; i < STORE_NAME.size(); i++) 
        { 
         if (textlength <= STORE_NAME.get(i).length()) 
         { 
          if (searchText.getText().toString(). 
            equalsIgnoreCase((String) STORE_NAME.get(i).subSequence(0, textlength))) 
          { 
           STORE_NAME_SUB.add(STORE_NAME.get(i)); 
           STOREID_SUB.add(STOREID.get(i)); 
           PHOTO_SUB.add(PHOTO.get(i)); 
           STORE_DESC_SUB.add(STORE_DESC.get(i)); 
           PLACE_STATUS_SUB.add(PLACE_STATUS.get(i)); 


          } 
         } 
        } 

        adapter=new StoreSearchListAdapter(context, R.drawable.ic_launcher, R.drawable.ic_launcher, STORE_NAME_SUB, PHOTO_SUB,STORE_DESC_SUB,PLACE_STATUS_SUB); 
        singlePlaceChooserList.setAdapter(adapter); 



     } 

我在這裏創造onTextCahnged方法中一個新的數組列表,然後利用這些值來設置我的適配器。

+0

thnks for answer,現在我解決了我的問題。看到我的答案中的代碼。 –

0

您onItemClick更改爲:

@Override 
    public void onItemClick(AdapterView<?> arg0, View arg1, int position, 
      long arg3) { 
     // TODO Auto-generated method 

     if (((String)adapter.getItem(position)).equals("Admin Cost")) { 
      Intent int0 = new Intent(getApplicationContext(), Admincost.class); 
      startActivity(int0); 
     } 
      ......................... 

    } 

當您過濾列表,項目將重新排列和thier位置變化,使導航到下一個活動基於這一立場,而不是位置的值。

+0

最新的問題? –

+0

嘗試我的adited ans –

+0

我猜這個字符串返回選擇的國家名稱。如果這是真的,這將起作用。如果不嘗試打印該值並找出它返回的內容。 –

相關問題