1

i m creating webservice application. it has ArrayAdapter class that is present the web data to Listview. and i want to get index of selected Listview Item and send the index value to next activity to do other task. but i m not able to get Index from ListView. ListView id is default Android API id

I used almost everything like setOnItemSelectedListener and setOnClickListener OnItemSelectedListener but did not work

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);        
        setContentView(R.layout.activity_main);     

        initView();   
    }

    private void initView() {
        // show progress dialog
        dialog = ProgressDialog.show(this, "", "Loading...");

        String url = "xxxxxxxx";
        // json class doing background data processing  
        FetchDataTask task = new FetchDataTask(this);
        task.execute(url);
    }

    @Override
    public void onFetchComplete(List<Application> data) {
        // dismiss the progress dialog
        if(dialog != null)  dialog.dismiss();
        // create new adapter
     ApplicationAdapter adapter = new ApplicationAdapter(this, data);
        // set the adapter to list
        lv.setAdapter(adapter);        

         }

THIS IS ArrayAdapter class

public class ApplicationAdapter extends ArrayAdapter<Application>  implements OnItemClickListener{
    private List<Application> items;
    ListView listview;
    public ApplicationAdapter(Context context, List<Application> items) {
        super(context, R.layout.app_custom_list, items);
        this.items = items;

        MainActivity.lv.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() 
        {
            public void onItemSelected(AdapterView<?> parentView, View childView, int position, long id) 
            {
                String text = ((TextView)childView.getTag()).toString();
             Log.d("index",text);
            }
            public void onNothingSelected(AdapterView<?> parentView) 
            {
            }
        });
    }

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



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

        if(v == null) {
            LayoutInflater li = LayoutInflater.from(getContext());
            v = li.inflate(R.layout.app_custom_list, null);            
        }

        Application app = items.get(position);

        if(app != null) {
            ImageView icon = (ImageView)v.findViewById(R.id.appIcon);



                for(int i=1; i<=5; i++) {
                    ImageView iv = new ImageView(getContext());

                    if(i <= app.getRating()) {
                        iv.setImageDrawable(getContext().getResources().getDrawable(R.drawable.start_checked));
                    }
                    else {                
                        iv.setImageDrawable(getContext().getResources().getDrawable(R.drawable.start_unchecked));
                    }

                    ratingCntr.addView(iv);
                }
            }
        }

        return v;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        String[] lv_arr = null;
        // TODO Auto-generated method stub
        String itemname = lv_arr[position];
        Log.d("string from ListView",itemname);


    }
}

Please help me to get Index of Selected Item of ListView. if anyone can suggest me what should i use , i will be grateful to them

thanks in Advance

1
  • What exactly do you not work with onItemSelected, onItemClick? But in general, you oddly create listener. As the bud using static variables. Look at that, if this guide... Commented Sep 16, 2015 at 2:25

1 Answer 1

2

at the bottom of protected void onCreate(Bundle savedInstanceState) { add

    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

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

      //get position index of item here. 

       String indexid = String.valueOf(position);  

       //and do whatever afterwards.                              


                        });

                    }

and remove public void onItemClick(AdapterView<?> parent, View view, int position, ....... in your adapter

Sign up to request clarification or add additional context in comments.

3 Comments

lv is declare in MainActivity as ListView and i edit lv to MainActivity.lvand it gives nullPointerExpection on onItemClick could please identity @Tasos
Cool dude, it is working now @Tasos you made my day
I have a TextView inside the array adapter and I would like to fire off the onclick for the item element, not the element itself. How can I do that? stackoverflow.com/questions/37071016/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.