-1

I have a list view that is inflated from my adapter. When I click on one of the Buttons I get the wrong item. It used to work o.k until I added the whatsApp button.

This is my adapter:

public class shawarmaddapter  extends ArrayAdapter<shawarma> {
String nname;
double lat,lon;

public shawarmaddapter(Context context, List<shawarma> resource) {
    super(context,0, resource);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {

   ....

    if (convertView==null)
    {
        convertView= LayoutInflater.from(getContext()).inflate(R.layout.shawarmaddapter,parent,false);
    }
   ....
    waze= (ImageButton)convertView.findViewById(R.id.adapterWazeBTN);
    whats = (ImageButton)convertView.findViewById(R.id.whats);
    locName=name.getText().toString();


....
    nname = sh1.getName();
    name.setText(sh1.getName());
....

    if (sh1.getParking()==1)
        park.setChecked(true);
    else
        park.setChecked(false);

    rate.setRating((float) sh1.getRank());
    rate.setIsIndicator(true);
    listener lis = new listener();
    waze.setOnClickListener(lis);
    whats.setOnClickListener(lis);
    lat=   sh1.getLat();
    lon= sh1.getLon();

    return convertView;
}

there is a onClickListener added in the code thats supposed to check the location if waze button is pressed or the name if whatsapp button is pressed. This is the code

class listener implements View.OnClickListener{

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.adapterWazeBTN:
                Log.i("a7", "button clicked");

              //  Log.i("a7", lat+" lat,  "+lon+"  lon"+ "distance: "+ sh1.getDistance());

                try
                {
                    String url = "waze://?ll="+lat+","+lon+"&navigate=yes";
                    Intent intent = new Intent( Intent.ACTION_VIEW, Uri.parse( url ) );
                    shawarmaList.con. startActivity( intent );
                }
                catch ( ActivityNotFoundException ex  )
                {
                    Intent intent =
                            new Intent( Intent.ACTION_VIEW, Uri.parse( "market://details?id=com.waze" ) );
                    shawarmaList.con.startActivity(intent);
                }
                catch (Exception ex)
                {
                    Log.i("a7",ex.toString());
                }


                break;
            case R.id.whats:
                Intent sendIntent = new Intent();
                sendIntent.setAction(Intent.ACTION_SEND);
                sendIntent.putExtra(Intent.EXTRA_TEXT, "do you want to Join me to "+nname+"?");
                sendIntent.setType("text/plain");

                try {
                    Toast.makeText(shawarmaList.con.getApplicationContext(),nname,Toast.LENGTH_SHORT).show();
                    shawarmaList.con.startActivity(sendIntent);
                } catch (Exception e) {
                    Log.i("a7",e.getMessage());
                }

                break;


        }
    }
}

How can I change this to get the right item?

Ittai Oren
  • 542
  • 5
  • 10
  • You are not sending the index of your item to your listener. How can listener know which index did you clicked? Also you need to use ViewHolder Pattern on your adapter for performance and other wrong item issues. – Oğuzhan Döngül Nov 13 '16 at 08:31
  • how can i send the index? how come it used to work before i added the whatsapp button? can you help with the viewholder? – Ittai Oren Nov 13 '16 at 08:36
  • You need to learn how to research. Check these links: https://developer.android.com/training/improving-layouts/smooth-scrolling.html and http://stackoverflow.com/questions/20541821/get-listview-item-position-on-button-click – Oğuzhan Döngül Nov 13 '16 at 08:53

3 Answers3

0

You can try this way

package com.example.listview;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.content.ClipData.Item;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity {
    ListView list;
    ArrayList<String> data;
    ArrayAdapter<String> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        list=(ListView)findViewById(R.id.listView1);
        data=new ArrayList<String>();
        data.add("List item 1");
        data.add("List item 2");
        data.add("List item 3");
        data.add("List item 4");
        adapter=new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1,data);
        list.setAdapter(adapter);

       list.setOnItemClickListener(new OnItemClickListener() {

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

            String s= Integer.toString(arg2) ; //here arg2 selects the list item
            Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();
            // TODO Auto-generated method stub

        }
    });
    }




    @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;
    }

}
Tazwar Utshas
  • 921
  • 2
  • 17
  • 30
0

Try this:

class listener implements View.OnClickListener{

    shawarma sh;

    listener(shawarma sh1) {
        sh = sh1;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.adapterWazeBTN:
                Log.i("a7", "button clicked");

              //  Log.i("a7", sh.getLat()+" lat,  "+sh.getLon()+"  lon"+ "distance: "+ sh.getDistance());

                try
                {
                    String url = "waze://?ll="+sh.getLat()+","+sh.getLon()+"&navigate=yes";
                    Intent intent = new Intent( Intent.ACTION_VIEW, Uri.parse( url ) );
                    shawarmaList.con. startActivity( intent );
                }
                catch ( ActivityNotFoundException ex  )
                {
                    Intent intent =
                            new Intent( Intent.ACTION_VIEW, Uri.parse( "market://details?id=com.waze" ) );
                    shawarmaList.con.startActivity(intent);
                }
                catch (Exception ex)
                {
                    Log.i("a7",ex.toString());
                }


                break;
            case R.id.whats:
                Intent sendIntent = new Intent();
                sendIntent.setAction(Intent.ACTION_SEND);
                sendIntent.putExtra(Intent.EXTRA_TEXT, "do you want to Join me to "+sh.getName()+"?");
                sendIntent.setType("text/plain");

                try {
                    Toast.makeText(shawarmaList.con.getApplicationContext(),sh.getName(),Toast.LENGTH_SHORT).show();
                    shawarmaList.con.startActivity(sendIntent);
                } catch (Exception e) {
                    Log.i("a7",e.getMessage());
                }

                break;


        }
    }
}

Then when you create the listener just pass the shwarma object like this:

listener lis = new listener(sh1);
MidasLefko
  • 4,499
  • 1
  • 31
  • 44
0

Instead of,

waze.setOnClickListener(lis);
whats.setOnClickListener(lis);

try this,

waze.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        try {
            String url = "waze://?ll="+sh1.getLat()+","+sh1.getLon()+"&navigate=yes";
            Intent intent = new Intent( Intent.ACTION_VIEW, Uri.parse( url ) );
            shawarmaList.con. startActivity( intent );
        }  catch ( ActivityNotFoundException ex  ) {
            Intent intent = new Intent( Intent.ACTION_VIEW, Uri.parse( "market://details?id=com.waze" ) );
            shawarmaList.con.startActivity(intent);
        } catch (Exception ex) {
            Log.i("a7",ex.toString());
        }
    }
});

whats.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        try {
            Intent sendIntent = new Intent();
            sendIntent.setAction(Intent.ACTION_SEND);
            sendIntent.putExtra(Intent.EXTRA_TEXT, "do you want to Join me to "+sh1.getName()+"?");
            sendIntent.setType("text/plain");

            Toast.makeText(shawarmaList.con.getApplicationContext(),sh1.getName(),Toast.LENGTH_SHORT).show();
            shawarmaList.con.startActivity(sendIntent);
        } catch (Exception e) {
            Log.i("a7",e.getMessage());
        }
    }
});

You will want to make sh1 as final,

final shawarma sh1 = getItem(position);
K Neeraj Lal
  • 6,768
  • 3
  • 24
  • 33