0

here's my main activity:

    package com.example.uilistview;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;


public class MainActivity extends ListActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setListAdapter(new ArrayAdapter<String>(this, R.layout.activity_main,
                getResources().getStringArray(R.array.listnames)));

        ListView lv = getListView();
    }


    }


}

My second activity:

package com.example.uilistview;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;


public class SecondActivity extends ActionBarActivity {

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


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_second, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

My string array for the list view:

<string-array name="listnames">
    <item onClick="onItemClick" id="iOS">iOS</item>
    <item>Android</item>
    <item>Windows</item>
    <item>Mac</item>
    <item>Linux</item>
    <item>Ubunto</item>
</string-array>

Once I click on iOS, I want it to load SecondActivity. I've tried lots of things. I've looked all over the place. I know I have to put an onClick Listener and something like that. iated. Edit: now you're all saying duplicate and all that crap, but I've searched everywhere and couldn't find a correct explanation.

Ali Bdeir
  • 4,151
  • 10
  • 57
  • 117

4 Answers4

1

Your MainActivity should look like this.

    package com.example.uilistview;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;


public class MainActivity extends ListActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setListAdapter(new ArrayAdapter<String>(this, R.layout.activity_main,
                getResources().getStringArray(R.array.listnames)));

        ListView lv = getListView();
        lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> arg0, View view, int arg2,
                        long arg3) {
        Intent intent = new Intent(MainActivity.this, SecondActivity.class);
        startActivity(intent);
    }


    } );  }
Prabhuraj
  • 928
  • 2
  • 8
  • 14
  • How can I ever thank you enough? THANKS SO MUCH!!! I'VE WENT OVER THE WHOLE DEVELOPER.ANDROID.COM SITE AND THIS FORUM AND I NEVER FOUND SOMETHING COMPATIBLE! – Ali Bdeir Jun 16 '15 at 18:03
0

Attach setOnitemClickListener to your ListView and inside the onItemClick method, use an Intent to open your SecondActivity.

 ListView lv = getListView();
 lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> arg0, View view, int arg2,
                        long arg3) {
                     Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                     startActivity(intent);
                }
            });
Bidhan
  • 10,607
  • 3
  • 39
  • 50
0

Override onListItemClick in your ListActivity and start your second activity from there:

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    Intent intent = new Intent(this, SecondActivity.class);
    startActivity(intent);
}
korrekorre
  • 1,025
  • 11
  • 24
0

Try this(Considering IOS position is 1st in Your array)

lv.setOnItemClickListener(new OnItemClickListener() {
  public void onItemClick(AdapterView<?> parent, View view,
      int position, long id) {
switch(position)
{
case 0:
Intent intent = new Intent();
intent.setClass(this, Other_Activity.class);
startActivity(intent);
  }
});
Roon13
  • 387
  • 2
  • 23