0

I am working in an Android app where I need to create onItemClick method,but I fail to do so. Below is the code where the onItemClick method does not work. Does anyone have idea why it does not work?

public class sample extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.live_chat_screen);

        mListview = (ListView) findViewById(R.id.mLiveview);
        mListview.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapters, View childView,int position, long arg3) {
                // TODO Auto-generated method stub
                Toast.makeText(sample.this,"Item number : " + position + " clicked",Toast.LENGTH_LONG).show();
            }
        });

here is the xml:

<ListView
     android:id="@+id/mLiveview"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"    
     android:fastScrollEnabled="true"           

     android:scrollbars="vertical" >
</ListView>

Actually I have used two views in my app first is when the app is open and the another view is open while the listview is getting load.

Could somebody help me out.

Renjith
  • 5,783
  • 9
  • 31
  • 42
  • Why it is not working? Code is perfect. Add some logs inside `onItemClick` so that you know that item is clicked. – Chintan Rathod Aug 13 '13 at 06:56
  • what kind of error you are getting? – T_V Aug 13 '13 at 06:56
  • Do you have items inside list? write `Toast.makeText(YourActivity.this,"Item number : " + position + " clicked",Toast.LENGTH_LONG).show();`. – Chintan Rathod Aug 13 '13 at 07:00
  • @AndroidKiller I tried the above code but i am getting response,actually i have used two view in my app –  Aug 13 '13 at 07:01
  • @priya NOT WORKING incredibly UNHELPFUL, post some log if any, place the code of List Adapter how u r adding `listview` items... – Avadhani Y Aug 13 '13 at 07:03
  • where you specify listadapter? paste the getView(..) method of list adapter if you have implemented it. – Suji Aug 13 '13 at 07:03
  • I think you must try @Android Killer's answer. – Chintan Rathod Aug 13 '13 at 07:04
  • I suggest you to take any simple blog from internet for custom listview with adapter and follow it carefully. – MKJParekh Aug 13 '13 at 07:07
  • @MKJParekh actually for simple case its working fine but here i am replacing the views,i dnt know why the issue is coming –  Aug 13 '13 at 07:08
  • I am not getting you. What does mean by 2 views, can you please post your full code with layout? – Chintan Rathod Aug 13 '13 at 07:08
  • how u r putting the data in the listview ??post that code also – Developer Aug 13 '13 at 07:10
  • @ChintanRathod actually when the app starts one custom layout is visible for eg:layout A , after that when the listview loads comletely the layout B is visible but the setContentView(R.layout.layout B); it will be always there.the Layout A is replacing the Layout B while the listview loads –  Aug 13 '13 at 07:12
  • But i don't think so that will affect your code. But I will suggest you for creating a new activity called `SplashActivity` which will be there for 2 secs and then your main activity called in which your ListView resides. You should do like this. Otherwise, if you want to wait until ListView loads, probably you should go for ProgressDialog. Which will not allow user to interact with screen until it disappear. :) – Chintan Rathod Aug 13 '13 at 07:15
  • @ChintanRathod actually its right but i don't have any idea why its happening ,,i can't create Splash screen for that cause it is in the middle of the app..so plz help me out –  Aug 13 '13 at 07:18
  • Do you have any idea bout `AsyncTask`? You can use that for loading data. Look at question in http://stackoverflow.com/questions/15678472/progress-dialog-not-shown-at-the-time-of-loading-of-data – Chintan Rathod Aug 13 '13 at 07:28
  • @ChintanRathod actually its kind of chat application so each time i can't load the message by async task right –  Aug 13 '13 at 07:30
  • Totally confused !!!! I have also developed a chat application in which i used AsyncTask, but holds user to access chat until it fetch all chat from server. And one more thing, in AsyncTask, after completion, I just update list. Which will not require any type of freezing.. Another way to do is, fetch your chat first time when user login, update that chat response (not screen) within time duration. When you update your chat response, just fire some listener so that activity will get that and will update that list :) – Chintan Rathod Aug 13 '13 at 07:35
  • @ChintanRathod ok let me check –  Aug 13 '13 at 07:43

4 Answers4

1

You are not setting your layout in your activity. First set your xml using setContentView() like this:

setContentView(R.layout.your_xml);

Then continue

mListview = (ListView) findViewById(R.id.mLiveview);
Cody Guldner
  • 2,888
  • 1
  • 25
  • 36
Android Killer
  • 18,174
  • 13
  • 67
  • 90
0

Add this code to your layout then try again;

android:clickable="true"
android:focusable="true"
Twinsens
  • 417
  • 6
  • 23
0

Try this in your ListView

 <ListView
                android:id="@+id/list"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:focusable="true"
                android:descendantFocusability="blocksDescendants" 
                android:orientation="horizontal"
                android:scrollbars="vertical"
                android:smoothScrollbar="true" />

If you are using custom ListView, such as using a check box, use

   android:focusable="false"
   android:focusableInTouchMode="false" 


 <CheckBox
    android:id="@+id/chk_delete"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:focusable="false"
    android:focusableInTouchMode="false" />
Cody Guldner
  • 2,888
  • 1
  • 25
  • 36
Deepanker Chaudhary
  • 1,694
  • 3
  • 15
  • 35
-1

The main thing you are forgetting is

setContentView(R.layout.activity_menus);
Cody Guldner
  • 2,888
  • 1
  • 25
  • 36
Developer
  • 6,292
  • 19
  • 55
  • 115
  • 1
    Hi gaurav, Don't be in hurry to put your answer. Provide what she want only. In your code, who knows, what is product, why you are opening activity? So, just edit your answer with some logs, so questioner simply understand. :) – Chintan Rathod Aug 13 '13 at 07:01
  • @TarunDroid the problem with priya's code is she forgot to set contentview – Developer Aug 13 '13 at 07:04
  • 2
    @TarunDroid yes earlier in her code setContentView was not thr. – Android Killer Aug 13 '13 at 07:08