Search code examples
javaandroidlistviewandroid-custom-view

ListView with customView and onClickItemListener


i have problem with listview... i'm trying to add OnClickListener but in still doesn't work. I want to display another activity after click. Can somebody help me? I know that there are many of example, but it's doesn't work for my appl or i don't know how to use it in my example...

This is my LocationAdapter class:

public class LocationAdapter extends ArrayAdapter<LocationModel> {

int resource;
String response;
Context context;
private LayoutInflater mInflater;

public LocationAdapter(Context context, int resource, List<LocationModel> objects) {
    super(context, resource, objects);
    this.resource = resource;
    mInflater = LayoutInflater.from(context);
}

static class ViewHolder {
    TextView titleGameName;
    TextView distanceGame;
        }


public View getView(int position, View convertView, ViewGroup parent)
{
    ViewHolder holder;
    //Get the current location object
    LocationModel lm = (LocationModel) getItem(position);

    //Inflate the view
    if(convertView==null)
    {
        convertView = mInflater.inflate(R.layout.item, null);
        holder = new ViewHolder();
        holder.titleGameName = (TextView) convertView
                .findViewById(R.id.it_location_title);
        holder.distanceGame = (TextView) convertView
                .findViewById(R.id.it_location_distance);

        convertView.setTag(holder);   


     } else  {

         holder = (ViewHolder) convertView.getTag();

    }

    holder.titleGameName.setText(lm.getGameName());
    holder.distanceGame.setText(lm.getGameDistance()+" km");

    return convertView;
}

}

This is my mainListView class:

public class SelectGameActivity extends Activity {
LocationManager lm;
GeoPoint userLocation;

ArrayList<LocationModel> locationArray = null;
LocationAdapter locationAdapter;
LocationList list;

ListView lv;
TextView loadingText;
TextView sprawdz;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.selectgame);

    lv = (ListView) findViewById(R.id.list_nearme);

    locationArray = new ArrayList<LocationModel>();
    locationAdapter = new LocationAdapter(SelectGameActivity.this, R.layout.item, locationArray);


    lv.setTextFilterEnabled(true);
    lv.setAdapter(locationAdapter);
    lv.setItemsCanFocus(true);





    String serverName = getResources().getString(R.string.serverAdress);
    ApplicationController AC = (ApplicationController)getApplicationContext();
    String idPlayer = AC.getIdPlayer();
    int latitude = AC.getCurrentPositionLat();
    int longitude = AC.getCurrentPositionLon();
    int maxDistance = 99999999;


    try {
        new LocationSync().execute("myserverName");
    } catch(Exception e) {}







}


//this is connection with json
private class LocationSync extends AsyncTask<String, Integer, LocationList> {

    protected LocationList doInBackground(String... urls) {
        LocationList list = null;
        int count = urls.length;

        for (int i = 0; i < count; i++) {
            try {           
                // ntar diganti service
                RestClient client = new RestClient(urls[i]);

                try {
                    client.Execute(RequestMethod.GET);
                } catch (Exception e) {
                    e.printStackTrace();
                }

                String json = client.getResponse();

                list = new Gson().fromJson(json, LocationList.class);

                //
            } catch(Exception e) {}
        }
        return list;
    }

    protected void onProgressUpdate(Integer... progress) {

    }

    protected void onPostExecute(LocationList loclist) {

        for(LocationModel lm : loclist.getLocations())
        {
            locationArray.add(lm);
        }
        locationAdapter.notifyDataSetChanged();
    }

}

EDIT:: I have second problem... i want to get id from item (items are downloading from json url) This is my list:

enter image description here

I want to get for example: ID:159 for first item and send it to nextActivity.

I have also the controllerClass.java where i'm setting and getting selectedIdGame:

    public String getIdGameSelected() {

    return idGame;
}

public void setIdGameSelected(String idGame) {

    this.idGame = idGame;
}

Is it good idea? Thanks for help.

Ok, it's done. i used:

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


            String idGame = (String) ((TextView) v.findViewById(R.id.idGameSelected)).getText();

Thanks, Michal.


Solution

  • You could define an onItemClick on your adapter instance (i.e. in mainListView.java, just after lv.setAdapter):

    lv.setOnItemClickListener(new OnItemClickListener() {
                    public void onItemClick(AdapterView<?> a, View
                                            v, int position, long id) {
                        Intent i = new Intent(v.getContext(), NextActivity.class);
                        startActivity(i);
                    }
                });