0

So I set up a list view which populates itself from an array of strings in the strings.xml file, and I can't seem to figure out how to code it so that tapping/clicking on one of the options in the list starts a new activity. I know that topics for this exist already, but it seems that I'm setting up my listview differently so the solutions presented aren't compatible. Here's my mainactivity.java code:

public class MainActivity extends AppCompatActivity implements OnClickListener {
Map<String, Integer> mapIndex;
ListView consoleList;
/**
 * ATTENTION: This was auto-generated to implement the App Indexing API.
 * See https://g.co/AppIndexing/AndroidStudio for more information.
 */
private GoogleApiClient client;

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

    String[] consoles = getResources().getStringArray(R.array.consoles_array);

    Arrays.asList(consoles);

    consoleList = (ListView) findViewById(R.id.list_consoles);
    consoleList.setAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, consoles));

    getIndexList(consoles);

    displayIndex();
    // ATTENTION: This was auto-generated to implement the App Indexing API.
    // See https://g.co/AppIndexing/AndroidStudio for more information.
    client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}

private void getIndexList(String[] consoles) {
    mapIndex = new LinkedHashMap<String, Integer>();
    for (int i = 0; i < consoles.length; i++) {
        String console = consoles[i];
        String index = console.substring(0, 1);

        if (mapIndex.get(index) == null)
            mapIndex.put(index, i);
    }
}

private void displayIndex() {
    LinearLayout indexLayout = (LinearLayout) findViewById(R.id.side_index);

    TextView textView;
    List<String> indexList = new ArrayList<String>(mapIndex.keySet());
    for (String index : indexList) {
        textView = (TextView) getLayoutInflater().inflate(
                R.layout.side_index_item, null);
        textView.setText(index);
        textView.setOnClickListener(this);
        indexLayout.addView(textView);
    }
}

public void onClick(View view) {
    TextView selectedIndex = (TextView) view;
    consoleList.setSelection(mapIndex.get(selectedIndex.getText()));
}


@Override
public void onStart() {
    super.onStart();

    // ATTENTION: This was auto-generated to implement the App Indexing API.
    // See https://g.co/AppIndexing/AndroidStudio for more information.
    client.connect();
    Action viewAction = Action.newAction(
            Action.TYPE_VIEW, // TODO: choose an action type.
            "Main Page", // TODO: Define a title for the content shown.
            // TODO: If you have web page content that matches this app activity's content,
            // make sure this auto-generated web page URL is correct.
            // Otherwise, set the URL to null.
            Uri.parse("http://host/path"),
            // TODO: Make sure this auto-generated app deep link URI is correct.
            Uri.parse("android-app://com.tylerfrisbee.gamerguide/http/host/path")
    );
    AppIndex.AppIndexApi.start(client, viewAction);
}

@Override
public void onStop() {
    super.onStop();

    // ATTENTION: This was auto-generated to implement the App Indexing API.
    // See https://g.co/AppIndexing/AndroidStudio for more information.
    Action viewAction = Action.newAction(
            Action.TYPE_VIEW, // TODO: choose an action type.
            "Main Page", // TODO: Define a title for the content shown.
            // TODO: If you have web page content that matches this app activity's content,
            // make sure this auto-generated web page URL is correct.
            // Otherwise, set the URL to null.
            Uri.parse("http://host/path"),
            // TODO: Make sure this auto-generated app deep link URI is correct.
            Uri.parse("android-app://com.tylerfrisbee.gamerguide/http/host/path")
    );
    AppIndex.AppIndexApi.end(client, viewAction);
    client.disconnect();
}

}

1 Answers1

1

You can call setOnItemClickListener on the ListView:

consoleList.setOnItemClickListener(new AdapterView.OnItemClickListener(){
  @Override
  public void onItemClick(AdapterView<?> parent, View view, int position, long id){
    //do whatever you want to do if you want to start a new Activity:
    Intent intent = new Intent(MainActivity.this, MyActivity.class);
    startActivity(intent);
  }
});

This should populate the onItemClick method as soon as you click on the item in the ListView and this should call your onClick method if this is what you want to do.

Nik-Sch
  • 102
  • 3
  • 6
  • I don't think `onClick(view)` should be called. That would throw a ClassCastException if the `ListView` has any other view than a `TextView` – OneCricketeer Jan 11 '16 at 21:13
  • Oh that's right. I will edit the answer corresponding. Thank you. – Nik-Sch Jan 11 '16 at 21:15
  • I might be wrong on this one, but `getContext()` should be `MainActivity.this` since it is within an anonymous class and `getContext()` is not a method within `AdapterView.OnItemClickListener` – OneCricketeer Jan 11 '16 at 21:18
  • getContext() was just meant to be replaced with the context you want to use in this case, however I will change it. – Nik-Sch Jan 11 '16 at 21:20
  • Is there any other setup code I need to implement first? I can't really describe why I can't get this working, I'm a noob here..getting "cannot resolve symbol" on setOnItemClickListener. Thanks for all the help btw! – Tyler Frisbee Jan 11 '16 at 21:29
  • there shouldn't. where were you adding the lines? you probably should implement this in `onResume`. – Nik-Sch Jan 11 '16 at 21:32
  • I just managed to get it working actually, that cleared everything up. Thanks! – Tyler Frisbee Jan 11 '16 at 21:47