Skip to content Skip to sidebar Skip to footer

Android List With Onclicklistener Using SampleAdapter

How to let users click on the selected item in the list menu and after select it will intent to another page. I know this question have been asked a lot in stackoverflow but i have

Solution 1:

You can get this by the setOnItemClickListener of the ListView as follows:

list.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
            // TODO Auto-generated method stub
            switch(arg2){
            case 0:{  

                    startActivity(new Intent(getApplicationContext(),ActivityA.class));                     
                }
            break;  
            case 1:{

                    startActivity(new Intent(getApplicationContext(),ActivityB.class));
                                    }   
            break;
            case 2:{  
                ExampleQuestion.topicNo=3;                          
                startActivity(new Intent(getApplicationContext(),ActivityC.class));                     
            }
            break;              
}

You can think of it as the listview contains A, B and C. When user selects A, it is the Case 0 and for B Case 2 and for C case 3.


Solution 2:

// try this way
public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        SampleAdapter adapter = new SampleAdapter(getActivity());
        adapter.add(new SampleItem("a"));
        adapter.add(new SampleItem("b"));
        adapter.add(new SampleItem("c"));
        adapter.add(new SampleItem("d"));
        adapter.add(new SampleItem("e"));
        adapter.add(new SampleItem("f"));
        setListAdapter(adapter);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                // write your start another activity code here
            }
        });
        TextView text1 = (TextView) getView().findViewById(R.id.loginName);

        text1.setText("TEST");
}

Solution 3:

Try to give onClickListner in your getView method

  title.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                    Intent i = new Intent(context,anotherActivity.class);
                                      context.startActivity(i);

                        }
                    });

Solution 4:

getListView().setOnItemClickListener(new AdapterView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id){
        /* your code */
    };
})

Solution 5:

RightListFragment rightListFragment = new RightListFragment();
rightListFragment.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int index, long l)         
            {
                swtich(index)
                {
                   // your implementaion
                }
            }
        });

Post a Comment for "Android List With Onclicklistener Using SampleAdapter"