2016-05-01 16 views
0

多くの国の情報(名前、資本、人口、旗の画像)を含むデータベースからデータを取得するアクティビティがあります。メインレイアウトには、入力としての大文字の名前とListViewを取るEditTextがあります。アクティビティを起動すると、すべての国が取得され、ListViewに簡単に表示されます。次に、EditTextを入力すると、リストビューに、一致する大文字の名前を持つ国が表示されます。しかし、私が見るものは、空のリストビューです。また、EditTextが空の場合は、リストビューに最初に表示されたすべての国を表示する必要があります。アンドロイド - EditText値の変更時にリストビューを更新できません

メインレイアウトファイルは以下の通りである:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/parentLayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#FFFFFF" 
    android:orientation="vertical" > 

    <com.google.android.gms.ads.AdView 
     xmlns:ads="http://schemas.android.com/apk/res-auto" 
     android:id="@+id/adView" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     ads:adSize="SMART_BANNER" 
     ads:adUnitId="@string/banner_ad_id" 
     android:visibility="gone" /> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_above="@id/adView" 
     android:layout_marginBottom="10dp" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="10dp" 
     android:layout_marginTop="5dp" > 



     <EditText 
      android:id="@+id/search_input_capital" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:inputType="text" 
      android:layout_alignParentTop="true" 
      android:visibility="visible" > 
     <requestFocus /> 

     </EditText> 
     <ListView 
      android:id="@+id/listView" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:cacheColorHint="#00000000" 
      android:divider="@android:color/transparent" 
      android:drawSelectorOnTop="false" 
      android:layout_below="@id/search_input_capital"> 
     </ListView> 
    </RelativeLayout> 

</RelativeLayout> 

および活性(CapitalActivity.java)のコードである:

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_capital); 

     private ArrayList<Country> items; 
     private ArrayList<Country> items_all; 

     private ArrayList<Country> items_new; 

     private EfficientAdapter adapter; 
     private ListView listView; 


     EditText searchInput=(EditText)findViewById(R.id.search_input_capital); 

     items = new ArrayList<Country>(); 
     items_new = new ArrayList<Country>(); 
     listView = (ListView) findViewById(R.id.listView); 
     listView.setDividerHeight(10); 

     adapter = new EfficientAdapter(CapitalActivity.this); 
     listView.setAdapter(adapter); 

     setListItems(""); 

     searchInput.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) { 


        String searchCapital = new String(s.toString()).trim(); 

       items_new.clear(); 

       for(Country myCountry : items_all) { 

       if(myCountry.getCapital() != null && 
        myCountry.getCapital().trim().toLowerCase().contains(searchCapital)) { 

       items_new.add(myCountry); 
       } 
       } 



      items.clear(); 

      items = (ArrayList<Country>) items_new.clone(); 

      if(searchCapital.trim().isEmpty()){ 

      items = items_all; 
      } 

      adapter.notifyDataSetChanged(); 


       } 
       }); 





private void setListItems(String searchKey) { 

    items_all= DatabaseAccessor.getCountryList(CapitalActivity.this, type, typeValue, searchKey); 
    items=items_all; 

    adapter.notifyDataSetChanged(); 
} 

のみ必要なコードセグメントは、上記に示されています。目標を達成するために私は何をすべきですか?

EDIT1:ほとんどの場合、上に貼り付けたコードのEfficientAdapter実装部分は省略されました。しかし、私はあまりにも、この時間はそのコードを貼り付けています:

public class EfficientAdapter extends BaseAdapter { 
     private LayoutInflater mInflater; 
     private Context context; 

     public EfficientAdapter(Context context) { 
      mInflater = LayoutInflater.from(context); 
      this.context = context; 
     } 

     public View getView(final int position, View convertView, ViewGroup parent) { 
      ViewHolder holder; 
      if (convertView == null) { 
       convertView = mInflater.inflate(R.layout.country_container, null); 
       holder = new ViewHolder(); 

       holder.nameView = (TextView) convertView.findViewById(R.id.nameView); 
       holder.continentView = (TextView) convertView.findViewById(R.id.continentView); 
       holder.imageView = (ImageView) convertView.findViewById(R.id.imageView); 
       holder.detailsView = (TextView) convertView.findViewById(R.id.detailsView); 
       convertView.setTag(holder); 
      } else { 
       holder = (ViewHolder) convertView.getTag(); 
      } 

      OnClickListener ocl = new OnClickListener() { 

       @Override 
       public void onClick(View v) { 
        Intent intent = new Intent(CapitalActivity.this, CountryLearningActivity.class); 
        intent.putExtra(Constants.KEY_COUNTRY, items); 
        intent.putExtra(Constants.KEY_INDEX, position); 
        startActivity(intent); 
       } 
      }; 

      Country item = items.get(position); 
      holder.nameView.setText(item.getCountry()); 
      if (type.equalsIgnoreCase(filters[0]) || type.equalsIgnoreCase(filters[1]) 
        || type.equalsIgnoreCase(filters[2])) { 
       holder.continentView.setText(item.getContinent()); 
      } else if (type.equalsIgnoreCase(filters[3])) { 
       holder.continentView.setText(Html 
         .fromHtml("Area: " + formatter.format(Double.parseDouble(item.getArea())) + " km<sup>2</sup>")); 
      } else if (type.equalsIgnoreCase(filters[4])) { 
       holder.continentView.setText("Population : " + item.getPopulation()); 
      } 
      holder.imageView.setImageResource(Utils.getResID(CapitalActivity.this, item.getFlag())); 
      holder.detailsView.setOnClickListener(ocl); 
      convertView.setOnClickListener(ocl); 

      return convertView; 
     } 

     class ViewHolder { 
      TextView nameView; 
      ImageView imageView; 
      TextView continentView; 
      TextView detailsView; 
     } 

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

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

     @Override 
     public Object getItem(int position) { 
      return items.get(position); 
     } 
    } 
+0

CapitalActivityのフルコードを投稿するかどうかを理解することが重要です。 – Masum

+0

@マッサム、「EDIT1」が追加されました –

答えて

0

はちょうど

enter cod protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_capital); 

    private ArrayList<Country> items; 
    private ArrayList<Country> items_all; 

    private ArrayList<Country> items_new; 

    private EfficientAdapter adapter; 
    private ListView listView; 


    EditText searchInput=(EditText)findViewById(R.id.search_input_capital); 

    items = new ArrayList<Country>(); 
    items_new = new ArrayList<Country>(); 
    listView = (ListView) findViewById(R.id.listView); 
    listView.setDividerHeight(10); 

    adapter = new EfficientAdapter(CapitalActivity.this); 
    listView.setAdapter(adapter); 

    setListItems(""); 

    searchInput.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) { 


       String searchCapital = new String(s.toString()).trim(); 

      items_new.clear(); 

      for(Country myCountry : items_all) { 

      if(myCountry.getCapital() != null && 
       myCountry.getCapital().trim().toLowerCase().contains(searchCapital)) { 

      items_new.add(myCountry); 
      } 
      } 



     items.clear(); 

     items = (ArrayList<Country>) items_new.clone(); 

     if(searchCapital.trim().isEmpty()){ 

     items = items_all; 
     } 

     adapter.notifyDataSetChanged(); 


      } 
      }); 

ます。private void setListItems(文字列SEARCHKEY){

items_all= DatabaseAccessor.getCountryList(CapitalActivity.this, type, typeValue, searchKey); 
items=items_all; 

adapter.notifyDataSetChanged(); 
以下のコードのためにあなたの現在のページからTEH同じページを開きます

eここをクリック

+0

何を変更しましたか? –

関連する問題