-1

How can i make clicking a item on a listview open a another activity. For example, if i click France it should display the toast and open my france.java activity. This is just for testing and experience as im still learning how to properly layout or? format code.

public class weapons extends Activity {
  ListView list;
  String[] web = {
      "Brazil",
      "France",
      "Japan",
      "USA",
      "Russia"
  } ;
  Integer[] imageId = {
      R.drawable.brazil,
      R.drawable.france,
      R.drawable.japan,
      R.drawable.usa,
      R.drawable.russia,
  };
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.weapons);
    CustomList adapter = new
        CustomList(weapons.this, web, imageId);
    list=(ListView)findViewById(R.id.list22);
        list.setAdapter(adapter);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                                        int position, long id) {
                    Toast.makeText(weapons.this, "You Clicked at " +web[+ position], Toast.LENGTH_SHORT).show();
                    }
            });
  }
}

Thanks.

3

6 Answers 6

0
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                                        int position, long id) {
String clicked=(String) parent.getItemAtPosition(position);
switch(clicked){
case "Brazil":
Toast.makeText(weapons.this, "You Clicked at "+clicked, Toast.LENGTH_SHORT).show();
 Intent intent = new Intent(this, Brazil.class);
startActivity(intent);
break;
case "France":
Toast.makeText(weapons.this, "You Clicked at "+clicked, Toast.LENGTH_SHORT).show();
 Intent intent = new Intent(this, France.class);
startActivity(intent);
default:
break;}
                    }
            });
0

Use Intent to start Activity Use following code in onItemClick

Intent intent = new Intent(this, YourNextActivity.class);
startActivity(intent);
0

You can start the new activity in onItemClick

@Override
public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                Toast.makeText(weapons.this, "You Clicked at " +web[+ position], Toast.LENGTH_SHORT).show();
Intent intent = new Intent(weapons.this, NewActivity.class);
startActivity(intent);
                }
0

Take a look at this.

   mlistView.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                  // When clicked, show a toast with the TextView text Game, Help, Home
                  Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
                      Toast.LENGTH_SHORT).show();
                 Intent intent = new Intent(this, Activity.class);
startActivity(intent);

              });
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
0

Use below code :

 @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position,
            long arg3) {

        TextView tv = (TextView) arg1.findViewById(R.id.txt);
        String str = tv.getText().toString().trim();

        if (str.equals(web[0])) {
            Intent int0 = new Intent(weapons.this, Brazil.class);
            startActivity(int0);
        }else if(str.equals(web[1])) {
            Intent int1 = new Intent(weapons.this, France.class);
            startActivity(int1);
        }else if(str.equals(web[2])) {
            Intent int2 = new Intent(weapons.this, Japan.class);
            startActivity(int2);
        }else if(str.equals(web[3])) {
            Intent int3 = new Intent(weapons.this, USA.class);
            startActivity(int3);
        }else if(str.equals(web[4])) {
            Intent int1 = new Intent(weapons.this, Russia.class);
            startActivity(int1);
        } 
}
0

User this code

Toast.makeText(this, "your message", Toast.LENGTH_LONG).show();  

(new Handler())
    .postDelayed(
    new Runnable() {
    public void run() {
         Intent intent = new Intent(this, Activity.class);
          startActivity(intent);
    }
    }, timeInMillisecondTheToastIsShowingFor);

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.