2016-04-29 17 views
1

我試圖創建一個活動,搜索拋出listview項目。我創建了一個switchcaseCategory其中Category是在main<string-array>一個項目,但是當我點擊不執行任何操作的項目和logcat的顯示這條消息 - > 04-29 20:20:00.380:d/ViewRootImpl(30869):ViewPostImeInputStage ACTION_DOWN當點擊列表視圖項目沒有執行任何操作和LogCat顯示ViewPostImeInputStage ACTION_DOWN

任何人都可以告訴我解決方案嗎?

Search.java

public class Search extends Activity implements TextWatcher/*,AdapterView.OnItemClickListener*/{ 

private String[] main,category,manufacturer,Chanel,Gucci,DandG,DKNY,MK; 
private ListView listView; 

private ArrayList<ListEntry> data_normal; 
private ArrayList<ListEntry> data_search; 

private EditText main_listview_edittext; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_search); 

    //get data 
    main=getResources().getStringArray(R.array.main); 
    manufacturer=getResources().getStringArray(R.array.Manufacturer); 
    category=getResources().getStringArray(R.array.Category); 
    Chanel=getResources().getStringArray(R.array.Chanel); 
    Gucci=getResources().getStringArray(R.array.Gucci); 
    DandG=getResources().getStringArray(R.array.DandG); 
    DKNY=getResources().getStringArray(R.array.DKNY); 
    MK=getResources().getStringArray(R.array.MK); 



    //loop data 
    data_normal = new ArrayList<ListEntry>(); 
    for(int i=0; i<main.length-1; i++){ 
     ListEntry listEntry1=new ListEntry(); 
     listEntry1.setTitle(main[i]); 
     data_normal.add(listEntry1); 
    } 

    for(int i=0; i<category.length; i++){ 
     ListEntry listEntry1=new ListEntry(); 
     listEntry1.setTitle(category[i]); 
     data_normal.add(listEntry1); 
    } 

    for(int i=0; i<manufacturer.length; i++){ 
     ListEntry listEntry=new ListEntry(); 
     listEntry.setTitle(manufacturer[i]); 
     data_normal.add(listEntry); 
    } 

    for(int i=0; i<Chanel.length; i++){ 
     ListEntry listEntry1=new ListEntry(); 
     listEntry1.setTitle(Chanel[i]); 
     data_normal.add(listEntry1); 
    } 

    for(int i=0; i<Gucci.length; i++){ 
     ListEntry listEntry1=new ListEntry(); 
     listEntry1.setTitle(Gucci[i]); 
     data_normal.add(listEntry1); 
    } 

    for(int i=0; i<DandG.length; i++){ 
     ListEntry listEntry1=new ListEntry(); 
     listEntry1.setTitle(DandG[i]); 
     data_normal.add(listEntry1); 
    } 

    for(int i=0; i<DKNY.length; i++){ 
     ListEntry listEntry1=new ListEntry(); 
     listEntry1.setTitle(DKNY[i]); 
     data_normal.add(listEntry1); 
    } 

    for(int i=0; i<MK.length; i++){ 
     ListEntry listEntry1=new ListEntry(); 
     listEntry1.setTitle(MK[i]); 
     data_normal.add(listEntry1); 
    } 

    //view matching.. 
    main_listview_edittext=(EditText)findViewById(R.id.main_listview_edittext); 
    listView=(ListView)findViewById(R.id.main_listView); 
    doListNormal(); 


    //event EditText 
    main_listview_edittext.addTextChangedListener(this); //TextWatcher 

} 


private void doListSearch() { 
    listView.setAdapter(new CustomAdapter(data_search)); 
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, 
       long id) { 

      switch(listView.getItemAtPosition(position).toString()){ 

      case "Category": 
       Intent i=new Intent(Search.this,MainActivity.class); 
       startActivity(i); 
       //get.lv.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,getResources().getStringArray(R.array.Category))); 
       break;} 
      //whatever you want to happen when an item is clicked 
     } 
    }); 
} 

private void doListNormal() { 
    listView.setAdapter(new CustomAdapter(data_normal)); 
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, 
       long id) { 

      switch(listView.getItemAtPosition(position).toString()){ 

      case "Category": 
       Intent i=new Intent(Search.this,MainActivity.class); 
       startActivity(i); 
       //get.lv.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,getResources().getStringArray(R.array.Category))); 
       break;} 
      //whatever you want to happen when an item is clicked 
     } 
    }); 
} 


/*@Override 
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

    //Intent i=new Intent(Search.this,SwipeActivity.class); 

    switch(listView.getItemAtPosition(position).toString()){ 

    case "Category": 
     Intent i=new Intent(Search.this,MainActivity.class); 
     startActivity(i); 
     //get.lv.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,getResources().getStringArray(R.array.Category))); 
     break; 

    } 
}*/ 


//TextWatchet implement method 1 
@Override 
public void beforeTextChanged(CharSequence s, int start, int count, int after) { 

} 

//TextWatchet implement method 2 
@Override 
public void onTextChanged(CharSequence s, int start, int before, int count) { 

    if(main_listview_edittext.length() != 0){ 

     data_search = new ArrayList<ListEntry>(); 

     for(int i=0; i<data_normal.size(); i++){ 

      //Check if the Text is Match the Contents 
      if(data_normal.get(i).getTitle().contains(s)||data_normal.get(i).getTitle().toLowerCase().contains(s)||data_normal.get(i).getTitle().toUpperCase().contains(s)){ 

       ListEntry listEntry=new ListEntry(); 
       listEntry.setTitle(data_normal.get(i).getTitle()); 
       data_search.add(listEntry); 

      } 
     } 

     doListSearch(); 

    }else { 
     doListNormal(); 
    } 
} 

//TextWatchet implement method 3 
@Override 
public void afterTextChanged(Editable s) { 

} 


public class CustomAdapter extends BaseAdapter{ 

    private ArrayList<ListEntry> mData; 

    public Holder holder=new Holder(); 

    public CustomAdapter(ArrayList<ListEntry> data){ 
     this.mData=data; 
    } 

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

    @Override 
    public Object getItem(int position) { 
     return data_normal.get(position); 
    } 

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

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

     convertView=View.inflate(Search.this,R.layout.listview_item_layout,null); 

     if(convertView!=null){ 

      //view matching child 
      holder.title = (TextView) convertView.findViewById(R.id.listview_item_title); 

      holder.title.setText(mData.get(position).getTitle()); 

      convertView.setTag(holder); 

     }else{ 
      convertView=(View) convertView.getTag(); 
     } 
     return convertView; 
    } 

    public class Holder{ 
     public TextView title; 
    } 
} 

} 

ListEntry.java

public class ListEntry { 

private String title; 
private String address; 

public String getTitle(){ 
    return title; 
} 

public void setTitle(String title){ 
    this.title=title; 
} 

@Override 
public String toString() { 
    return super.toString(); 
} 

}

logcat的

04-30 22:11:22.610: D/SecWifiDisplayUtil(18274): Metadata value : none 
04-30 22:11:22.645: D/PhoneWindow(18274): *FMB* installDecor mIsFloating : false 
04-30 22:11:22.645: D/PhoneWindow(18274): *FMB* installDecor flags : 8454400 
04-30 22:11:22.650: D/AbsListView(18274): Get MotionRecognitionManager 
04-30 22:11:22.650: E/MotionRecognitionManager(18274): mSContextService = [email protected] 
04-30 22:11:22.650: E/MotionRecognitionManager(18274): motionService = [email protected]3b6ee200 
04-30 22:11:22.650: E/MotionRecognitionManager(18274): motionService = [email protected]3b6ee200 
04-30 22:11:22.660: D/OpenGLRenderer(18274): Use EGL_SWAP_BEHAVIOR_PRESERVED: true 
04-30 22:11:22.670: D/PhoneWindow(18274): *FMB* isFloatingMenuEnabled mFloatingMenuBtn : null 
04-30 22:11:22.670: D/PhoneWindow(18274): *FMB* isFloatingMenuEnabled return false 
04-30 22:11:22.675: D/SRIB_DCS(18274): log_dcs ThreadedRenderer::initialize entered! 
04-30 22:11:22.680: I/OpenGLRenderer(18274): Initialized EGL, version 1.4 
04-30 22:11:22.685: I/OpenGLRenderer(18274): HWUI protection enabled for context , &this =0xb4627190 ,&mEglDisplay = 1 , &mEglConfig = -1267322608 
04-30 22:11:22.685: D/OpenGLRenderer(18274): Get maximum texture size. GL_MAX_TEXTURE_SIZE is 8192 
04-30 22:11:22.685: D/OpenGLRenderer(18274): Enabling debug mode 0 
04-30 22:11:22.685: D/mali_winsys(18274): new_window_surface returns 0x3000, [1440x2560]-format:1 
04-30 22:11:22.700: D/ListView(18274): change accessibility focus position = 0 
04-30 22:11:22.795: I/Timeline(18274): Timeline: Activity_idle id: [email protected] time:66657037 
04-30 22:11:26.315: D/ViewRootImpl(18274): ViewPostImeInputStage ACTION_DOWN 
04-30 22:11:26.425: I/Timeline(18274): Timeline: Activity_launch_request id:com.example.shaymatest time:66660669 
04-30 22:11:26.455: D/PhoneWindow(18274): *FMB* installDecor mIsFloating : false 
04-30 22:11:26.455: D/PhoneWindow(18274): *FMB* installDecor flags : 8454400 
04-30 22:11:26.460: D/AbsListView(18274): Get MotionRecognitionManager 
04-30 22:11:26.460: E/MotionRecognitionManager(18274): mSContextService = [email protected] 
04-30 22:11:26.460: E/MotionRecognitionManager(18274): motionService = [email protected]1795fa8e 
04-30 22:11:26.460: E/MotionRecognitionManager(18274): motionService = [email protected]1795fa8e 
04-30 22:11:26.465: D/SecWifiDisplayUtil(18274): Metadata value : none 
04-30 22:11:26.470: D/PhoneWindow(18274): *FMB* isFloatingMenuEnabled mFloatingMenuBtn : null 
04-30 22:11:26.470: D/PhoneWindow(18274): *FMB* isFloatingMenuEnabled return false 
04-30 22:11:26.485: D/SRIB_DCS(18274): log_dcs ThreadedRenderer::initialize entered! 
04-30 22:11:26.485: D/mali_winsys(18274): new_window_surface returns 0x3000, [1440x2560]-format:1 
04-30 22:11:26.540: D/ListView(18274): change accessibility focus position = 0 
04-30 22:11:26.565: E/SpannableStringBuilder(18274): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length 
04-30 22:11:26.565: E/SpannableStringBuilder(18274): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length 
04-30 22:11:26.565: I/Timeline(18274): Timeline: Activity_idle id: [email protected] time:66660805 
04-30 22:11:26.565: E/SpannableStringBuilder(18274): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length 
04-30 22:11:26.565: E/SpannableStringBuilder(18274): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length 
04-30 22:11:26.840: V/ActivityThread(18274): updateVisibility : ActivityRecord{27683843 [email protected] {com.example.shaymatest/com.example.shaymatest.MainActivity}} show : false 
04-30 22:11:27.425: D/ViewRootImpl(18274): ViewPostImeInputStage ACTION_DOWN 
04-30 22:11:27.555: D/AndroidRuntime(18274): Shutting down VM 
04-30 22:11:27.555: E/AndroidRuntime(18274): FATAL EXCEPTION: main 
04-30 22:11:27.555: E/AndroidRuntime(18274): Process: com.example.shaymatest, PID: 18274 
04-30 22:11:27.555: E/AndroidRuntime(18274): java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView 
04-30 22:11:27.555: E/AndroidRuntime(18274): at com.example.shaymatest.Search.onItemClick(Search.java:126) 
04-30 22:11:27.555: E/AndroidRuntime(18274): at android.widget.AdapterView.performItemClick(AdapterView.java:339) 
04-30 22:11:27.555: E/AndroidRuntime(18274): at android.widget.AbsListView.performItemClick(AbsListView.java:1548) 
04-30 22:11:27.555: E/AndroidRuntime(18274): at android.widget.AbsListView$PerformClick.run(AbsListView.java:3737) 
04-30 22:11:27.555: E/AndroidRuntime(18274): at android.widget.AbsListView$3.run(AbsListView.java:5676) 
04-30 22:11:27.555: E/AndroidRuntime(18274): at android.os.Handler.handleCallback(Handler.java:739) 
04-30 22:11:27.555: E/AndroidRuntime(18274): at android.os.Handler.dispatchMessage(Handler.java:95) 
04-30 22:11:27.555: E/AndroidRuntime(18274): at android.os.Looper.loop(Looper.java:145) 
04-30 22:11:27.555: E/AndroidRuntime(18274): at android.app.ActivityThread.main(ActivityThread.java:6872) 
04-30 22:11:27.555: E/AndroidRuntime(18274): at java.lang.reflect.Method.invoke(Native Method) 
04-30 22:11:27.555: E/AndroidRuntime(18274): at java.lang.reflect.Method.invoke(Method.java:372) 
04-30 22:11:27.555: E/AndroidRuntime(18274): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404) 
04-30 22:11:27.555: E/AndroidRuntime(18274): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199) 

listview_item_layout.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" android:layout_height="match_parent" 
    android:padding="20dp"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="TESTING" 
     android:textSize="20sp" 
     android:textStyle="bold" 
     android:id="@+id/listview_item_title"/> 

</LinearLayout> 
+0

您必須在ListView上設置itemClickListener。 http://stackoverflow.com/questions/17851687/how-to-handle-the-click-event-in-listview-in-android –

+0

我在代碼中有這種方法,但它不響應,也看到LogCat錯誤@Sammy T – Doglas

回答

1

那麼它看起來像你的switch-case沒有被調用然後。嘗試將您的switch語句替換爲if語句。使用.equals()是比較字符串更可靠的方法。

TextView thisTextView = (TextView)view.findViewById(R.id.listview_item_title); 
String text = (String)thisTextView.getText(); 

if(text.equals("Category")){ 
    Intent i=new Intent(Search.this, MainActivity.class); 
    startActivity(i); 
}else{ 
    Toast.makeText(Search.this,"What's my text: " + text, Toast.LENGTH_LONG).show(); 
} 
+0

仍然不工作! :/在LogCat中的相同msg - >'04-30 21:16:03.920:D/ViewRootImpl(17890):ViewPostImeInputStage ACTION_DOWN' – Doglas

+0

嘗試使用此新編輯來代替之前的if語句。通過這種方式,它應該告訴你即使由於某種原因失敗了,也會返回哪些文本。我是否認爲你有TextViews填充你的ListView? –

+0

getActivity()方法是什麼(未定義)?我嘗試設置'getApplicationContext()'而不是'getActivity()'並運行該應用程序,當點擊該項目時,程序停止'不幸已停止' – Doglas

0

您有onItemClick但您在ListView上設置Click Listener時沒有設置它。把它放在你的setOnItemClickListener

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, 
       long id) { 
      //whatever you want to happen when an item is clicked 
     } 
    }); 
+0

我把它稱爲這個>> listView.setOnItemClickListener(this);'在'doListSearch()'和'doListNormal()'方法 – Doglas

+0

是的,我看到了,但我認爲這可能是你的問題。按照我的建議來嘗試。 –

+0

我已經試過了,仍然沒有工作! – Doglas

相關問題