Parse.com MealSpotting教程onitemclicklistener无效

时间:2014-03-03 20:02:29

标签: java android parse-platform onclicklistener

我在让onclicklistener与parse.com的mealpotting Android应用程序中的customadapter一起工作时遇到了一些麻烦。当我尝试单击列表中的项目时,logcat中没有任何事情发生(除了标准的“无法加载音频”或其他),我很确定我需要将某些东西附加到适配器但我还没有找到用parse的API实现它的方法。单击列表项时,我希望它按顺序显示DetailViews类活动。

这是我正在尝试实现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();


        } 
    }

这是我的主要活动课

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();
      }
  }


}

谢谢!

1 个答案:

答案 0 :(得分:0)

您尚未为列表视图设置点击监听器。请尝试此getListView().setOnItemClickListener(mainAdapter),因为您的适配器已实现onItemClickListener接口。