Question

i'm having some trouble getting the onclicklistener to work with a customadapter from parse.com's mealspotting android app. When I try clicking on an item in the list nothing happens in the logcat (except for the standard "can't load audio" or whatever), i'm pretty sure i need to attach something to the adapter but i haven't found a way to implement that with parse's APIs. When a list item is clicked I want it to show the DetailViews class activity by the way.

This is where i'm trying to implement the onclicklistener

package com.parse.mealspotting;

import java.util.Arrays;

import android.content.Context;

import android.util.Log;

import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import android.widget.TextView;
import android.widget.Toast;

import com.parse.GetDataCallback;
import com.parse.ParseException;
import com.parse.ParseFile;
import com.parse.ParseImageView;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.ParseQueryAdapter;




public class MealSpottingViewYay extends ParseQueryAdapter<Meal> implements OnItemClickListener {


  private ParseQueryAdapter<Meal> posts;
  private static final String TAG = "MealSpottingViewYay";   



  public MealSpottingViewYay(Context context) {
      super(context, new ParseQueryAdapter.QueryFactory<Meal>() {
          public ParseQuery<Meal> create() {
              // Here we can configure a ParseQuery to display
              // only top-rated meals.
              ParseQuery query = new ParseQuery("Meal");
              return query;

          }
      });
  }

  @Override
  public View getItemView(Meal meal, View v, ViewGroup parent) {


      if (v == null) {
          v = View.inflate(getContext(), R.layout.main_list_view, null);
      }

      super.getItemView(meal, v, parent);

      ParseImageView mealImage = (ParseImageView) v.findViewById(R.id.icon);
      ParseFile photoFile = meal.getParseFile("photo");
      if (photoFile != null) {
          mealImage.setParseFile(photoFile);
          mealImage.loadInBackground(new GetDataCallback() {
              @Override
              public void done(byte[] data, ParseException e) {
                  // nothing to do



              }});

      }

      TextView titleTextView = (TextView) v.findViewById(R.id.text1);
      titleTextView.setText(meal.getTitle());
      TextView ratingTextView = (TextView) v
              .findViewById(R.id.favorite_meal_rating);
      ratingTextView.setText(meal.getRating());
      return v;       
  }


  public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {
            Log.d(TAG, "onItemClick");

        ParseObject parseObject = posts.getItem(position);

        // Use the parseobject here
        Context context = getContext();
        Toast toast = Toast.makeText(context, "Clicked!", Toast.LENGTH_SHORT);
        toast.show();


        } 
    }

and this is my mainactivity class

package com.parse.mealspotting;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;


import com.parse.ParseObject;
import com.parse.ParseQueryAdapter;

public class MealListActivity extends ListActivity {

  private MealSpottingViewYay mainAdapter;
  private FavoriteMealAdapter22 favoritesAdapter;


  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      getListView().setClickable(false);

      mainAdapter = new MealSpottingViewYay(this);


      // There's probably a better way to do this
      if (mainAdapter != null){
          updateMealList();
      }

      // Subclass of ParseQueryAdapter
      favoritesAdapter = new FavoriteMealAdapter22(this);



  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      getMenuInflater().inflate(R.menu.activity_meal_list, menu);
      return true;
  }

  /*
   * Posting meals and refreshing the list will be controlled from the Action
   * Bar.
   */

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
      switch (item.getItemId()) {

      case R.id.action_refresh: {
          updateMealList();
          break;
      }

      case R.id.action_favorites: {
          showFavorites();
          break;
      }

      case R.id.action_new: {
          newMeal();
          break;
      }
      }
      return super.onOptionsItemSelected(item);
  }

  private void updateMealList() {
      mainAdapter.loadObjects();
      setListAdapter(mainAdapter);
  }

  private void showFavorites() {
      favoritesAdapter.loadObjects();
      setListAdapter(favoritesAdapter);
  }

  private void newMeal() {
      Intent i = new Intent(this, NewMealActivity.class);
      startActivityForResult(i, 0);
  }

  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent data) {
      if (resultCode == Activity.RESULT_OK) {
          // If a new post has been added, update
          // the list of posts
          updateMealList();
      }
  }


}

Thanks!

Was it helpful?

Solution

You haven't set a click listener to your listview. Try this getListView().setOnItemClickListener(mainAdapter) as your adapter has implemented an onItemClickListenerinterface.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top