使用android和parse.com进行慢速数据检索

时间:2014-01-05 20:36:14

标签: android android-listview parse-platform android-adapter android-internet

我正在使用parse.com检索我的应用上使用的数据。问题是下载需要大约一分钟,而且只有4张小图片(380x380)和一个名字。这是我的代码:

package com.favega.groups;

import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;

import com.parse.*;

public class MainActivity extends ActionBarActivity implements CategoryFragment.OnFragmentInteractionListener, EventFragment.OnFragmentInteractionListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Parse.initialize(this, "xxxxxxxxxxx", "xxxxx");
        ParseAnalytics.trackAppOpened(getIntent());
        ParseObject.registerSubclass(Category.class);
        ParseObject.registerSubclass(Event.class);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new CategoryFragment())
                    .commit();
        }
    }


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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onFragmentInteraction(Uri uri) {

    }

    @Override
    public void onFragmentInteraction(String id) {

    }
}

我的适配器:

package com.favega.groups;

import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.text.method.CharacterPickerDialog;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.parse.FindCallback;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.ParseQueryAdapter;
import com.squareup.picasso.Picasso;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class CategoryAdapter extends ParseQueryAdapter<Category>{

    MainActivity mContext;

    public CategoryAdapter(Context context) {
        super(context, "Category");
        this.mContext = (MainActivity) context;
    }

    @Override
    public final View getItemView(final Category object, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = ((Activity) mContext).getLayoutInflater().inflate(R.layout.category, parent, false);
        }
        TextView categoryName = (TextView) convertView.findViewById(R.id.categoryName);
        categoryName.setText(object.getName());
        categoryName.setTypeface(Typeface.createFromAsset(mContext.getAssets(), "fonts/Roboto-Light.ttf"));
        final TextView eventNum = (TextView) convertView.findViewById(R.id.eventNum);
        eventNum.setTypeface(Typeface.createFromAsset(mContext.getAssets(), "fonts/Roboto-Light.ttf"));
        ParseQuery<Event> query = ParseQuery.getQuery("Event");
        query.whereEqualTo("parent", object);
        query.findInBackground(new FindCallback<Event>() {
            @Override
            public void done(List<Event> events, ParseException e) {
                eventNum.setText(events.size() + " " + mContext.getString(R.string.events));
                notifyDataSetChanged();
            }
        });

        SquareImageView imageView = (SquareImageView) convertView.findViewById(R.id.image);
        Picasso.with(mContext).load(object.getImage().getUrl()).placeholder(new ColorDrawable(object.getColor())).into(imageView);
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FragmentTransaction fragmentTransaction = mContext.getSupportFragmentManager().beginTransaction();
                Bundle args = new Bundle();
                args.putString("ARGS_CATEGORY", object.getName());
                EventFragment eventFragment = new EventFragment();
                eventFragment.setArguments(args);
                fragmentTransaction.replace(R.id.container, eventFragment);
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();
            }
        });
        return convertView;
    }
}

1 个答案:

答案 0 :(得分:0)

在解析中检索图像的一种更简单的方法如下:

for(ParseObject obj : objects)
{
    ParseFile file = obj.getParseFile("Column_Name");
    try
    {
         Bitmap bit = BitmapFactory.decodeByteArray(file.getData(),0,file.getData().length);
    }
    catch(ParseException e1)
    {}
}

这有助于我更快地下载图像。我建议把它放在Asynctask