Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Can't call setOnItemClickListener from a ListView

I've been following some Android development tutorials on Udemy, which have been very helpful, but now I've run into an issue with the tutors code. Not sure if this is because of an updated SDK or what, but the code does not error out in his video.

package com.example.app;

import android.app.ListActivity;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.widget.AdapterView;
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);
        String[]countries=getResources().getStringArray(R.array.countries);
        this.setListAdapter(new ArrayAdapter<String>(
                this,
                R.layout.list_element,
                R.id.countryName,
                countries));
    }

    ListView listview = getListView();

    listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?>  parent, View clickView, int position, long id) {
            String country = countries[position];
            Toast.makeText(MainActivity.this, String.format("%s was chosen.", country),
                    Toast.LENGTH_SHORT).show();
        }
    });


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, 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();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        }
    }

}

The errors start here, with a "Cannot resolve symbol" error.

listview.setOnItemClickListener

and further errors continue throughout the containing loop.

like image 825
user71607 Avatar asked Jan 04 '14 14:01

user71607


2 Answers

write that code in onCreate()... you are writing outside of methods which gives a syntax error...

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    String[] countries = getResources().getStringArray(R.array.countries);
    this.setListAdapter(new ArrayAdapter<String>(this,
            R.layout.list_element, R.id.countryName, countries));

    ListView listview = getListView();

    listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View clickView,
                int position, long id) {
            String country = countries[position];
            Toast.makeText(MainActivity.this,
                    String.format("%s was chosen.", country),
                    Toast.LENGTH_SHORT).show();
        }
    });
}
like image 164
Gopal Gopi Avatar answered Oct 22 '22 20:10

Gopal Gopi


this line of code

listview.setOnItemClickListener

cannot be placed directly in a class, it must be placed in the scope some function like onCreate()

like image 3
zealoct Avatar answered Oct 22 '22 22:10

zealoct