3

I'm trying to create an activity that search throw the listview items. I create a switch with case Category where Category is an item in the main <string-array>, but when I'm clicking on the Item no action is performed and the LogCat show this msg --> 04-29 20:20:00.380: D/ViewRootImpl(30869): ViewPostImeInputStage ACTION_DOWN

can any one tell me the solution?

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 = android.hardware.scontext.ISContextService$Stub$Proxy@aacd083
04-30 22:11:22.650: E/MotionRecognitionManager(18274): motionService = com.samsung.android.motion.IMotionRecognitionService$Stub$Proxy@3b6ee200
04-30 22:11:22.650: E/MotionRecognitionManager(18274): motionService = com.samsung.android.motion.IMotionRecognitionService$Stub$Proxy@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: android.os.BinderProxy@23126a39 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 = android.hardware.scontext.ISContextService$Stub$Proxy@c41da89
04-30 22:11:26.460: E/MotionRecognitionManager(18274): motionService = com.samsung.android.motion.IMotionRecognitionService$Stub$Proxy@1795fa8e
04-30 22:11:26.460: E/MotionRecognitionManager(18274): motionService = com.samsung.android.motion.IMotionRecognitionService$Stub$Proxy@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: android.os.BinderProxy@150722af 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 token=android.os.BinderProxy@23126a39 {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>
Doglas
  • 47
  • 1
  • 8
  • You have to set the itemClickListener on the ListView. http://stackoverflow.com/questions/17851687/how-to-handle-the-click-event-in-listview-in-android – Sammy T Apr 29 '16 at 22:43
  • I have this method in the code but it doesn't respond, also see the LogCat error @Sammy T – Doglas Apr 30 '16 at 13:27

2 Answers2

1

Well it looks like your switch-case isn't being called then. Try replacing your switch statement to an if statement. Using .equals() is a more reliable way to compare strings.

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();
}
Sammy T
  • 1,924
  • 1
  • 13
  • 20
  • still not working!! :/ the same msg in LogCat --> `04-30 21:16:03.920: D/ViewRootImpl(17890): ViewPostImeInputStage ACTION_DOWN` – Doglas Apr 30 '16 at 18:16
  • Try this new edit in place of the if statement from before. This way it should tell you what text you're getting back even if it fails for some reason. Am I right in assuming that you have TextViews filling your ListView? – Sammy T Apr 30 '16 at 18:33
  • what's the `getActivity()` method (is undefined)? I tried to set `getApplicationContext()` instead of `getActivity()` and run the app, and when clicking on the Item the program stop `unfortunately has stopped` – Doglas Apr 30 '16 at 18:54
  • Sorry replace `getActivity()` with `Search.this` depending on the situation getActivity doesn't always work. – Sammy T Apr 30 '16 at 18:57
  • `unfortunately has stopped` again. You don't think the problem it occur with the other place within the code?, I think maybe be in the returned value of `getItem(int position)` method in `CustomAdapter` class? – Doglas Apr 30 '16 at 19:04
  • I change the LogCat in the post to the new one. – Doglas Apr 30 '16 at 19:14
  • Alright, I think I see the problem. Can you add your layout xml for what you're using to fill your ListView? I can tell there's a LinearLayout but it would just be easier to see the xml. – Sammy T Apr 30 '16 at 19:19
  • see the post I add the layout. – Doglas Apr 30 '16 at 19:25
  • Alright, so if you run this edit and see a Toast, then the text you're getting isn't what you expected to get. – Sammy T Apr 30 '16 at 19:33
  • Ohh, how I forget to change that code! OMG .... It's work, I don't know what to say Mr. Sammy I don't know how to say how much I'm happy now.. It's for 5 days I work within this error. Thanks Thanks Thanks a lot thank youuuuuuuu .... – Doglas Apr 30 '16 at 19:42
  • You're welcome. Just take note of how we debugged your code for the next time you run into a problem. Either look through the log for an `E:/something: some error` or, if there aren't any, add Toasts(it doesn't have to be a toast but they're my fav) to narrow down where your code is failing. – Sammy T Apr 30 '16 at 19:50
  • Thank you for this advice. Actually I have a problem with the LogCat error reading, but also I'm new in android programming. I wish I'll getting better with it.. Is there any references that help ? – Doglas Apr 30 '16 at 19:58
  • Take the logcat still attached to this post for example. There are several lines indicating an error with `E/` If you start looking from the top, you find the type of error `LinearLayout cannot be cast to`... and where it is `Search.onItemClick(Search.java:126)` – Sammy T Apr 30 '16 at 20:37
  • Thank you. I understand it :) (y) – Doglas May 01 '16 at 09:51
0

You have onItemClick but you're not setting it when you set the Click Listener on the ListView. Put it inside of your 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
        }
    });
Sammy T
  • 1,924
  • 1
  • 13
  • 20
  • I call it like this >>`listView.setOnItemClickListener(this);` in `doListSearch()` and `doListNormal()`methods – Doglas Apr 30 '16 at 17:18
  • Yes, I saw that but I think that may be your problem. Try it the way I'm suggesting. – Sammy T Apr 30 '16 at 17:21
  • Ok, good. Now, try adding `Toast.makeText(getActivity(),"button clicked", Toast.LENGTH_SHORT).show();` just before your `switch(listView.getItemAtPosition(position).toString())` and tell me if you see anything. – Sammy T Apr 30 '16 at 17:51
  • without `switch` every thing is ok. I tried `Toast` and it's work – Doglas Apr 30 '16 at 17:54