2012-06-26 2 views
0

"root, top and down"을 포함하는 세 개의 목록이 있습니다. 하지만 "top"으로 목록을 필터링 한 다음 목록에서 클릭하면 "root"가 나타납니다. 필터를 적용 할 때 클릭 가능한 목록으로 만들려면 어떻게해야합니까?searchbox로 사용자 정의 listview를 얻는 방법을 클릭하셨습니까?

list.java

public class root extends Activity { 

EditText edittext; 
ListView listview; 

private String[] text = { "ROOT", "TOP", "DOWN" }; 

private int[] image = { R.drawable.root, R.drawable.top, R.drawable.down }; 

int textlength = 0; 

ArrayList<String> text_sort = new ArrayList<String>(); 
ArrayList<Integer> image_sort = new ArrayList<Integer>(); 

public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.mainlistview); 

    this.getWindow().setSoftInputMode(
      WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 

    edittext = (EditText) findViewById(R.id.EditText01); 
    listview = (ListView) findViewById(R.id.ListView01); 
    listview.setAdapter(new MyCustomAdapter(text, image)); 

    listview.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View view, 
       int position, long id) { 

      if ("ROOT".equals(text[position])) { 
       Intent myIntent = new Intent(view.getContext(), 
         root.class); 
       startActivityForResult(myIntent, 0); 

      } 
      if ("TOP".equals(text[position])) { 
       Intent myIntent = new Intent(view.getContext(), 
         top.class); 
       startActivityForResult(myIntent, 0); 
      } 
      if ("DOWN".equals(text[position])) { 
       Intent myIntent = new Intent(view.getContext(), 
         down.class); 
       startActivityForResult(myIntent, 0); 

     } 

    }); 

    edittext.addTextChangedListener(new TextWatcher() { 

     public void afterTextChanged(Editable s) { 

     } 

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

     } 

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

      textlength = edittext.getText().length(); 
      text_sort.clear(); 
      image_sort.clear(); 

      for (int i = 0; i < text.length; i++) { 
       if (textlength <= text[i].length()) { 
        if (edittext 
          .getText() 
          .toString() 
          .equalsIgnoreCase(
            (String) text[i].subSequence(0, 
              textlength))) { 
         text_sort.add(text[i]); 
         image_sort.add(image[i]); 
        } 
       } 
      } 

      listview.setAdapter(new MyCustomAdapter(text_sort, image_sort)); 

     } 
    }); 
} 

class MyCustomAdapter extends BaseAdapter { 

    String[] data_text; 
    int[] data_image; 

    MyCustomAdapter() { 

    } 

    MyCustomAdapter(String[] text, int[] image) { 
     data_text = text; 
     data_image = image; 
    } 

    MyCustomAdapter(ArrayList<String> text, ArrayList<Integer> image) { 

     data_text = new String[text.size()]; 
     data_image = new int[image.size()]; 

     for (int i = 0; i < text.size(); i++) { 
      data_text[i] = text.get(i); 
      data_image[i] = image.get(i); 
     } 

    } 

    public int getCount() { 
     return data_text.length; 
    } 

    public String getItem(int position) { 
     return null; 
    } 

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

    public View getView(int position, View convertView, ViewGroup parent) { 

     LayoutInflater inflater = getLayoutInflater(); 
     View row; 

     row = inflater.inflate(R.layout.listview, parent, false); 

     TextView textview = (TextView) row.findViewById(R.id.TextView01); 
     ImageView imageview = (ImageView) row 
       .findViewById(R.id.ImageView01); 

     textview.setText(data_text[position]); 
     imageview.setImageResource(data_image[position]); 

     return (row); 

    } 
} 
}

main.xml에

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<EditText 
    android:id="@+id/EditText01" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:hint="Search" > 
</EditText> 

<ListView 
    android:id="@+id/ListView01" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" > 
</ListView> 

listview.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:background="#fff200" 
android:gravity="left|center" 
android:paddingBottom="5px" 
android:paddingLeft="5px" 
android:paddingTop="5px" > 

<ImageView 
    android:id="@+id/ImageView01" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" > 
</ImageView> 

<TextView 
    android:id="@+id/TextView01" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="10px" 
    android:textColor="#0099CC" 
    android:textSize="20px" 
    android:textStyle="bold" > 
</TextView> 
,617,

답변

0

문제는 ListView 당신의 onItemClickListener 내에있는 : 당신이 당신의 ListView을 필터링 한 후

listview.setOnItemClickListener(new OnItemClickListener() { 
    public void onItemClick(AdapterView<?> parent, View view, 
      int position, long id) { 

     if ("ROOT".equals(text[position])) { 
      Intent myIntent = new Intent(view.getContext(),class); 
      startActivityForResult(myIntent, 0); 

, 당신의 ListView 만 1 항목을 표시합니다. 이 항목을 클릭하면 onItemClick이 실행됩니다. 클릭 한 항목이 새로 필터링 된 첫 번째 위치에 있으므로 position 변수는 0입니다. Array text[]의 첫 번째 항목은 "ROOT"이므로 RootActivity가 시작됩니다.

View selectedView = yourAdapter.getItemAtPosition(position) 
TextView myText = (TextView) selectedView.findViewById(R.id.TextView01).getText() 

if(myText.equals("ROOT")){ 
    // start Activity.. 
+0

내가 어떤 생각을 가지고 있겠지, 그래서 난 그냥 넣어 : onItemClicked(..)이 내부

을 :

솔루션

하면 클릭하고 문자열로 그 비교 한보기 어댑터를 물어 이 "보기 selectedView = MyCustomAdapter.getItemAtPosition (위치) TextView myText = (TextView) selectedView.findViewById (Ri d "TextView01) .getText()"- my : "공개 무효 onItemClick (어댑터보기 부모,보기보기, int 위치, 긴 id) {" – user1482732

+0

나는 http://www.mediafire.com 파일을 업로드 /? binttnilkoh5iuc 당신이 고칠 수 있도록 도와주세요. 고마워요. – user1482732

+0

글자 그대로 붙여 넣으면 내 제공 코드가 작동하지 않을 수도 있습니다. 가능한 해결책에 대한 아이디어를 줄 것입니다. – DroidBender

관련 문제