0

please I'm trying to add understand how to add onItemClickListener to the followng code such that when "Smartphone Plans" is clicked, its activity starts and so on. I've seen other questions on StackOverflow relating to this question but do not understand how to go about them. I've already added an onItemClickListener but do not understand how to set it to specific list items. here is the code

package devchuks.com.rechargeit;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.app.ListActivity;

import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class EtisalatData extends AppCompatActivity {
    ListView listView;

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

        listView = (ListView) findViewById(R.id.list);


        String[] values = new String[] {
                "Smartphone Plans",
                "Internet Bundles",
                "Weekend Plans",

        };



        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, android.R.id.text1, values);



        listView.setAdapter(adapter);


        listView.setOnItemClickListener(new OnItemClickListener() {

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


            }

        });
    }

}
Fahim Al Mahmud Ashik
  • 1,083
  • 10
  • 26
devchuks
  • 11
  • 1
  • 7

5 Answers5

5

You can define your listView.setOnItemClickListener like this to go to different activity for clicking different element.

listView.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view,
                            int position, long id) {
         String item = listView.getItemAtPosition(position);
         Toast.makeText(this,"You selected : " + item,Toast.LENGTH_SHORT).show();
         if(position==0) {
            // Do your code for clicking "Smartphone Plans" example
            // startActivity(new Intent(getApplicationContext(),SmartphonePlans.class));
         }
        else if(position==1) { 
           // Do your code for clicking "Internet Bundles". example
          // startActivity(new Intent(getApplicationContext(),InternetBundles.class));
         }
        else if(position==2) { 
           // Do your code for clicking "Weekend Plans". example 
          //startActivity(new Intent(getApplicationContext(),WeekendPlans.class));*/
    }

});
Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
Fahim Al Mahmud Ashik
  • 1,083
  • 10
  • 26
2

onItemClick will be called whenever any of the list items are clicked. The position will be the position of the view in the Adapter.

Please refer - How to handle the click event in Listview in android?

Thanks Sriram

Community
  • 1
  • 1
Sri
  • 1,317
  • 9
  • 13
2

If this helps and is your concern `

listView.setOnItemClickListener(new OnItemClickListener() {
                @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                 String data = values[position];
                 switch(data){
                 case "Smartphone Plans":
                 // do somwthing
                 break;

                // similarly for other two values.
                 }
            }

        });`
Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
  • Yes but i want it to launch a new activity on a specific item click – devchuks Jan 08 '17 at 18:07
  • so based on the item that is clicked start the new activity by using **Intent** Intent i = new Intent(getApplicationContext(), NewActivity.class); startActivity(i); – Abhinav Tripathi Jan 08 '17 at 18:16
2

try this:

 listView.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
        String text = values[position];
        if(text.equals("Smartphone Plans")){ //your specific list item text
            Intent i = new Intent(MainActivity.this, AnotherActivity.class);
            i.putExtra("TEXT", text);
            startActivity(i);
        }
    }
}
rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62
2

Below method give you a position of a clicked row. Take advantage of that and value from your array.

 @Override
   public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
      // Move your values array to member array or make it final to use
      // in Anonymous class
      final String selectedValue = values[position];

     Intent intent = null
    // Now add case and start activity 
    if("Smartphone Plans".equals(selectedValue)) {
      intent = new Intent(EtisalatData.this, SmartPhonePlan.class);
    }
    else if("other Plans".equals(selectedValue)){
      // other action
    }
       //... more cases and at the end start your activity if its not null
     startActivity(intent);
}
Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
Rahul
  • 10,457
  • 4
  • 35
  • 55