2015-12-03 64 views
0

你好,每一個我有一個自定義列表視圖與搜索和動畫所有工程很酷,但在搜索我有一個小問題。例如,我想在一個句子中搜索一個單詞。 但我用來輸入字母的方式必須按順序排列。 這裏是我的代碼:如何使自定義列表視圖的輸入搜索

public class MainActivity extends Activity { 
ListView list; 
ListViewAdapter adapter; 
EditText editsearch; 
String[] country; 
Typeface tf; 
ArrayList<WorldPopulation> arraylist = new ArrayList<WorldPopulation>(); 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.listview_main); 
    editsearch = (EditText) findViewById(R.id.search); 
    list = (ListView) findViewById(R.id.listview); 
    tf = Typeface.createFromAsset(getAssets(), "fonts/BKOODB.TTF"); 

    country = new String[54]; 
    for (int x = 1; x < 54 + 1; x = x + 1) { 
     String this_subject = "mo_" + String.valueOf(x); 
     int resID = getResources().getIdentifier(this_subject, "string", getPackageName()); 
     country[x - 1] = getResources().getString(resID); 
    } 
    for (int i = 0; i < 54; i++) { 
     WorldPopulation wp = new WorldPopulation(country[i]); 
     // Binds all strings into an array 
     arraylist.add(wp); 
    } 
    adapter = new ListViewAdapter(this, arraylist); 
    list.setAdapter(adapter); 
    editsearch.addTextChangedListener(new TextWatcher() { 

     @Override 
     public void afterTextChanged(Editable arg0) { 
      // TODO Auto-generated method stub 
      String text = editsearch.getText().toString().toLowerCase(Locale.getDefault()); 
      adapter.filter(text); 
     } 

     @Override 
     public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { 
      // TODO Auto-generated method stub 
     } 

     @Override 
     public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { 
      // TODO Auto-generated method stub 
     } 
    }); 
    list.setOnScrollListener(new OnScrollListener() { 

     @Override 
     public void onScrollStateChanged(AbsListView view, int scrollState) { 
      hideKeyboard(); 

     } 

     @Override 
     public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { 

     } 
    }); 
    new CountDownTimer(500, 500) { 
     @Override 
     public void onTick(long millisUntilFinished) { 
     } 

     @Override 
     public void onFinish() { 
      hideKeyboard(); 
     } 
    }.start(); 
} 

public class WorldPopulation { 

    private String country; 

    public WorldPopulation(String country) { 

     this.country = country; 
    } 

    public String getCountry() { 
     return this.country; 
    } 

} 

public class ListViewAdapter extends BaseAdapter { 

    // Declare Variables 
    Context mContext; 
    LayoutInflater inflater; 
    private List<WorldPopulation> worldpopulationlist = null; 
    private ArrayList<WorldPopulation> arraylist; 
    Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_in_right); 

    public ListViewAdapter(Context context, List<WorldPopulation> worldpopulationlist) { 
     mContext = context; 
     this.worldpopulationlist = worldpopulationlist; 
     inflater = LayoutInflater.from(mContext); 
     this.arraylist = new ArrayList<WorldPopulation>(); 
     this.arraylist.addAll(worldpopulationlist); 
    } 

    public class ViewHolder { 

     TextView country; 

    } 

    @Override 
    public int getCount() { 
     return worldpopulationlist.size(); 
    } 

    @Override 
    public WorldPopulation getItem(int position) { 
     return worldpopulationlist.get(position); 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 

    public View getView(final int position, View view, ViewGroup parent) { 
     LayoutInflater inflater = getLayoutInflater(); 
     View row; 

     row = inflater.inflate(R.layout.listview_item, parent, false); 
     TextView textview = (TextView) row.findViewById(R.id.country); 
     ImageView im = (ImageView) row.findViewById(R.id.imageitem); 
     im.setImageResource(R.drawable.hair); 
     textview.setText(worldpopulationlist.get(position).getCountry()); 
     textview.setTypeface(tf); 
     Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_in_right); 
     row.startAnimation(animation); 

     row.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       // ********************************************************* 
       // in here it does not send right number to secend activity* 
       // ********************************************************* 
       int orgPos = 0; 
       if (country.length != worldpopulationlist.size()) { 
        notifyDataSetChanged(); 
        // The list on which we clicked is sorted! 
        String clickedText = worldpopulationlist.get(position).toString(); 
        int i1 = 0; 
        boolean found = false; 

        while (i1 < country.length && found == false) { 
         if (clickedText == country[i1]) { 
          orgPos = i1; 
          found = true; 
         } else { 
          i1++; 
         } 
        } 

        Intent i2 = new Intent(mContext, SingleItemView.class); 
        String Subject_number = String.valueOf(orgPos + 1); 
        i2.putExtra("subject_number", Subject_number); 
        startActivity(i2); 

       } else { 
        Intent i = new Intent(mContext, SingleItemView.class); 
        String Subject_number = String.valueOf(position + 1); 
        i.putExtra("subject_number", Subject_number); 
        startActivity(i); 

       } 
      } 
     }); 

     return row; 
    } 

    // Filter Class 
    public void filter(String charText) { 
     charText = charText.toLowerCase(Locale.getDefault()); 
     worldpopulationlist.clear(); 
     if (charText.length() == 0) { 
      worldpopulationlist.addAll(arraylist); 
     } else { 
      for (final WorldPopulation wp : arraylist) { 
       if (wp.getCountry().toLowerCase(Locale.getDefault()).contains(charText)) { 
        worldpopulationlist.add(wp); 
       } 
      } 
     } 
     notifyDataSetChanged(); 
    } 

} 

private void hideKeyboard() { 
    View view = this.getCurrentFocus(); 
    if (view != null) { 
     InputMethodManager inputManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE); 
     inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); 
    } 
} 

}

回答

1

嘗試afterTextChanged方法做這件事,因爲這可以讓你輸入的所有文本。並按照this鏈接

public void afterTextChanged(Editable s) { 

    } 
+0

我用你的給定的項目,但我現在有另一個問題,我的項目的位置,如果我做搜索它發送錯誤的位置,下一個活動。 – saeed

0

嘗試使用自定義適配器。

這會幫助你。 Link

+0

我用你的給定項目,但我現在有另一個問題,我的項目的位置,如果我做搜索它發送到下一個活動的錯誤位置。 – saeed