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) {


            }

        });
    }

}
4
  • you mean values[position] , do whatever you want next Commented Jan 8, 2017 at 18:00
  • how do i make it select specific items Commented Jan 8, 2017 at 18:05
  • use equals function with the value of this values[position] Commented Jan 8, 2017 at 18:11
  • You should accept an answer so that people knows which answer resolved your problem Commented Jan 8, 2017 at 22:06

5 Answers 5

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));*/
    }

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

Comments

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

Comments

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);
        }
    }
}

Comments

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.
                 }
            }

        });`

2 Comments

Yes but i want it to launch a new activity on a specific item click
so based on the item that is clicked start the new activity by using Intent Intent i = new Intent(getApplicationContext(), NewActivity.class); startActivity(i);
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);
}

Comments

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.