2

My app is a list of firms, each firm is a row in my listview that shows: picture, name and phone (by adapter). Everything is working! But when I click in a item of my listview, it should start another activity that shows page with firm details. I'm having trouble with onitemclick listener (it doesn't work):

empresa = firm (in portuguese) lv = my listview
  lv.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
      Intent i = new Intent (getApplicationContext(), detalheEmpresas.class );
      Empresas empresa = (Empresas) parent.getItemAtPosition(position);
      i.putExtra("Nome", empresa.getTitle().toString());
      startActivity(i); }});

my detalheEmpresas activity

public class detalheEmpresas extends Activity {

@Override
public void onCreate (Bundle savedInstanceState){

super.onCreate(savedInstanceState);
this.setContentView(R.layout.detalhes_empresa);
Intent i = getIntent();
String nome = i.getStringExtra("Nome");
TextView NOME = (TextView) findViewById(R.id.Nome);
NOME.setText(nome);
}}
Rob Mensching
  • 33,834
  • 5
  • 90
  • 130

2 Answers2

0

could very well be related to the getApplicationContext... try to use activity context:

lv.setOnItemClickListener(new OnItemClickListener() {
  public void onItemClick(AdapterView<?> parent, View view,int position, long id) {

  Intent i = new Intent (mSavedContext.getActivity(), detalheEmpresas.class ); <-------------

  Empresas empresa = (Empresas) parent.getItemAtPosition(position);
  i.putExtra("Nome", empresa.getTitle().toString());
  startActivity(i); 
}});

---------- new edit -----------------

ok how about you just add a click handler to the parent activity and you should be good to go:

public class ParentActivity extends Activity implements OnItemClickListener {
   your activity code...

   public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
         public void onItemClick(AdapterView<?> parent, View view,int position, long id) {

         Intent i = new Intent (getActivity(), detalheEmpresas.class ); 
         Empresas empresa = (Empresas) parent.getItemAtPosition(position);
         i.putExtra("Nome", empresa.getTitle().toString());
         startActivity(i);
   } 

in your activity code replace

 lv.setOnItemClickListener(new OnItemClickListener() {

with

 lv.setOnItemClickListener(this); 
Dory Zidon
  • 10,497
  • 2
  • 25
  • 39
  • getactivity() is undefined for the type new adapterview.OnItemClickListener =/ – user2395406 May 17 '13 at 21:30
  • unless you pass it in the contstructor and then you can use it inside:) – Dory Zidon May 17 '13 at 21:35
  • sorry, im very noob on this.. my "lv.setOnItemClickListener( new.... " in in my main activity (not in the adapter). my adapter: public class customListViewAdapter extends BaseAdapter implements SectionIndexer { i should put "this.mSavedContext=context;" here? – user2395406 May 17 '13 at 22:01
  • ok I just made an edit..suggesting you setup the click listener as parent of the parent activity rather than an annoymous handler..see if that works for you ? – Dory Zidon May 18 '13 at 00:16
0

I had a very similar problem. Solved by setting on click listener to view itself in the list adapter.

public class EmpresasListAdapter extends ArrayAdapter<Empresas> {

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        //recycling view and setting view fields.

        /* Set listener to open firm detail activity on click */
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Your code to open firm detail activity
                Intent i = new Intent (getApplicationContext(), detalheEmpresas.class );
                Empresas empresa = (Empresas) parent.getItemAtPosition(position);
                i.putExtra("Nome", empresa.getTitle().toString());
                startActivity(i);
            }
        });
        return view;
    } 
}
Tautvydas
  • 2,027
  • 3
  • 25
  • 38