2015-01-11 5 views
0

안녕하세요 여러분, listView를 사용하여 차량 정보를 표시하는 내 활동에 검색 기능을 추가하려고합니다. 나는 많은 해결책을 시도했지만 나에게는 아무런 효과가 없었다.안드로이드에서 LayoutInflater를 사용하는 ListView를 검색하십시오.

사실 저는 VehicleActivity에서 textView를 추가했습니다 & 사용자가 textView에 무언가를 입력 할 때 listView의 내용을 필터링하고 싶습니다.

이 차량의 활동

//Passing Actual Data To PlaceHolder Of ListView 
    private class EfficientAdapter extends BaseAdapter 
    { 
     private LayoutInflater mInflater; 

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

     public int getCount() 
     { 
      return VehicleList.Vehicle_ID.length; 
      //return GetVehicleList.ID.length; 
     } 

     public Object getItem(int position) 
     { 
      return position; 
     } 

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

     public View getView(final int position, View convertView, ViewGroup parent) 
     { 
      ViewHolder holder; 
      try 
      { 
       if (convertView == null) 
       { 
        mInflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
        convertView = mInflater.inflate(R.layout.three_col_row, null); 

         holder = new ViewHolder(); 

         holder.text1 = (TextView) convertView.findViewById(R.id.imei); 
         holder.text2 = (TextView) convertView.findViewById(R.id.status); 
         holder.text3 = (TextView) convertView.findViewById(R.id.location); 
         holder.text4 = (TextView) convertView.findViewById(R.id.speed); 
         holder.text5 = (TextView) convertView.findViewById(R.id.date); 
         holder.img_option = (ImageView) convertView.findViewById(R.id.img_arrow); 

         convertView.setTag(holder); 

        holder.img_option.setOnClickListener(new OnClickListener() 
        { 
         @Override 
         public void onClick(View v) 
         { 
          VehicleActivity.this.registerForContextMenu(listView);           
          VehicleActivity.this.openContextMenu(listView); 


          VehicleActivity.this.context_menu_index = VehicleActivity.this.listView.getPositionForView(v); 

         } 
        }); 

       } 
       else 
       { 
        holder = (ViewHolder) convertView.getTag(); 
       } 

       holder.img_option.setFocusable(true); 
       holder.img_option.setClickable(true); 
       holder.img_option.setTag(holder); 

       //Setting The Font Style 
       holder.text1.setTypeface(null, Typeface.BOLD); 
       holder.text2.setTypeface(null, Typeface.ITALIC); 

       //Passing The Actual Values To TextViews 
       holder. text1.setText("ID: "  +VehicleList.IMEI[position]); 
       holder.text2.setText("Status: " +VehicleList.Status[position]); 
       holder.text3.setText("Location: " +VehicleList.Location[position]); 
       holder.text4.setText("Speed: " +VehicleList.Speed[position]); 
       holder.text5.setText("Date: " +VehicleList.Date[position]); 

       convertView.setBackgroundColor(position % 2 == 0 ? Color.parseColor("#eaeaea") : Color.parseColor("#d3e3f3")); 
      } 
      catch(Exception e) 
      { 
       PstExp = new PostException("VA.EA.GETVEW Ln:830",                 //Block 
          (e.getMessage() == null)? "null": "Android App VA.EA.GETVEW() \n"+e.getMessage().toString(),    //Message 
         (e.getStackTrace() == null)? "null": "Android App VA.EA.GETVEW() \n"+e.getStackTrace().toString(),   //Stack Trace 
          (e.getCause() == null)? "null": "Android App VA.EA.GETVEW() \n"+e.getCause().toString()  );  //Inner Exception 
      } 
      catch(Error e2) 
      { 
       PstExp = new PostException("VA.EA.GETVEW Ln:837",                 //Block 
         (e2.getMessage() == null)? "null": "Android App VA.EA.GETVEW() \n"+e2.getMessage().toString(),    //Message 
        (e2.getStackTrace() == null)? "null": "Android App VA.EA.GETVEW() \n"+e2.getStackTrace().toString(),   //Stack Trace 
         (e2.getCause() == null)? "null": "Android App VA.EA.GETVEW() \n"+e2.getCause().toString()  );   //Inner Exception 
      } 

      return convertView; 
     } 

     public class ViewHolder 
     { 
      TextView text1; 
      TextView text2; 
      TextView text3; 
      TextView text4; 
      TextView text5; 
      ImageView img_option; 
     } 


    } 

이 VehicleList의 코드

public class VehicleList 
{ 
public static int[] Vehicle_ID; 

public static String[] IMEI; 
public static String[] Status; 
public static String[] Location; 

public static String[] Speed; 
public static String[] Date; 

public static Double[] Latitude; 
public static Double[] Longitude; 

String error="", msg=""; 

}의 일부입니다 VehicleActivity

public class VehicleActivity extends Activity 
{ 
     //The PlaceHolder For The View's 
    ListView listView; 
    private ListAdapter mAdapter; 


    //Static Variable For Passing The VehicleActivity Context To SessionExpired() 
    public static Context mContext; 

    //public ImageView img_arrow; 
    public int context_menu_index; 

    //Alert DialogBox 
    public AlertDialog AltDialog; 
    public boolean CDialog_value = false;  //Used when command of immoblize & De-Imoblize use fired 

    //Exception Handling 
    static PostException PstExp; 

    //Text View 
    EditText editsearch; 

    /** Called when the activity is first created. */ 
    public void onCreate(Bundle savedInstanceState) 
    { 
     try 
     { 
      Log.d("VA OnC", "Before Super"); 
      super.onCreate(savedInstanceState); 
      mContext = getBaseContext(); 
      setTitleBar();    //Setting The Title Bar 

      setContentView(R.layout.activity_vehicle);  //Passing The XML Layout 

      //Getting Infromation From Parent Activity 
      Intent intent_veh  = getIntent(); 
      User_ID     = intent_veh.getIntExtra("UID", 0);        //UserId 
      session_key    = intent_veh.getStringExtra("Key");        //Session Key 
      permissions    = new int [(intent_veh.getIntArrayExtra("Rights")).length];  //Permissions Length 
      permissions    = intent_veh.getIntArrayExtra("Rights");      //Permissions Assignment 


      //To Overwrite The Last Value Of The Variables 
      EmptyActivity = false; 
      //Initializing GetVehicleList Object 
      obj_GVL = new GetVehicleList(VehicleActivity.this, User_ID, session_key); 

      //Calling The Method That Will Fetch Information From Server 
      obj_GVL.getVehicleInfo(); 

      Log.d("VA ", "Before EA Check"); 
      if(EmptyActivity == false) 
      { 
       //Passing XML layout To ListView 
       Log.d("VA ", "Before listView = (ListView) findViewById(R.id.list_view)"); 
       listView = (ListView) findViewById(R.id.list_view); 
       Log.d("VA ", "Before listView.setAdapter(new EfficientAdapter(this))"); 

       mAdapter = new EfficientAdapter(this); 
       listView.setAdapter(mAdapter); 
       //enables filtering for the contents of the given ListView 
       listView.setTextFilterEnabled(true); 
       //listView.setAdapter(new EfficientAdapter(this)); 

       //Search Box Code Start 
       editsearch = (EditText) findViewById(R.id.search); 

       //Search Box Code End 

       Log.d("VA ", "Before listView.setOnItemClickListener()"); 
       Log.d("VA ", "Before VehicleList.Location[0] ="); 
      } 
     } 
     catch(Exception e) 
     { 
      //Some Code 
     } 
     catch(Error e2) 
     { 
      //Some Code 
     } 
    } 

    //Some code 

    } 

이 내 어댑터의 코드의 코드

답변

0

귀하의 질문에서 내가 이해 한 것은 에 텍스트를 입력 할 때 검색 기능을 구현해야합니다. 검색 편집 상자. 따라서 위의 코드는 완벽하게 작동하고 있습니다 (최소한 처음에는 활동이 처음로드 될 때). 귀하의 경우에 텍스트 상자편집 텍스트 상자을 다음과 같이 추가해야합니다.

editsearch.addTextChangedListener(this); 

@Override 
public void afterTextChanged(Editable s) { 
    // TODO Auto-generated method stub 
} 
@Override 
public void beforeTextChanged(CharSequence s, int start, int count, 
     int after) { 
    // TODO Auto-generated method stub 


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


} 

이동을하는 방법에 이러한 선,

mAdapter = new EfficientAdapter(this); 
listView.setAdapter(mAdapter); 
listView.setTextFilterEnabled(true); 

이 도움이

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

희망 내부의 메소드를 호출합니다.

+0

감사합니다. & 지금 필자는 Filterable 인터페이스를 구현하는 데 혼란을 겪고 있습니다. –

관련 문제