Android从parse.com检索图像和文本并在适配器中使用它 [英] Android retrieving image and text from parse.com and using it in an adapter
问题描述
实际上,我想使用此库"Aphid-FlipView-Library"来制作翻转动画.我要翻转的图像和文本来自Parse.com.我已经可以使用下面的方法翻转图像了.
这是代码.活动的前半部分,我将从parse.com检索到的图像添加到名为notes的数组列表中,并在适配器中使用它.
Actually, I want to use this library"Aphid-FlipView-Library"to do an animation of flipping. The images and texts I want to flip are from Parse.com. I've already could flip the images by using the way below.
Here is the code. First half of the activity, I add the images which retrieved from parse.com into an arraylist called notes and use it in an adapter.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.rotate);
FlipViewController flipView = new FlipViewController(this, FlipViewController.HORIZONTAL);
final NoteViewAdapter mAdapter = new NoteViewAdapter(this);
flipView.setAdapter(mAdapter);
setContentView(flipView);
final ArrayList<Bitmap> notes = new ArrayList<Bitmap>();
@@@final ArrayList<String> texts = new ArrayList<String>();@@@
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Card");
query.getInBackground("wxYBHhRlhZ", new GetCallback<ParseObject>() {
@Override
public void done(ParseObject object, ParseException e) {
if(e == null){
ParseFile fileObject1 = (ParseFile) object.get("Photoin1");
fileObject1.getDataInBackground(new GetDataCallback(){
@Override
public void done(byte[] data, ParseException e) {
notes.add(BitmapFactory.decodeByteArray(data,0,data.length));
mAdapter.setNotes(notes);
}
});
@@@String playerName = object.getString("Textin1");@@@
@@@texts.add(playerName);@@@
@@@mAdapter.setTexts(texts);@@@
}else{
//nothing
}
}
});
}
然后,下面显示了作为适配器的活动的后半部分.
And then, second half of the activity that is a adapter is showed below.
public class NoteViewAdapter extends BaseAdapter {
private LayoutInflater inflater;
private ArrayList<Bitmap> notes;
@@@private ArrayList<String> texts;@@@
public NoteViewAdapter(Context currentContext) {
inflater = LayoutInflater.from(currentContext);
}
@Override
public int getCount() {
return notes == null ? 0 : notes.size()
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
public void setNotes(ArrayList notes) {
this.notes = notes;
notifyDataSetChanged();
}
@@@public void setTexts(ArrayList texts) {@@@
@@@ this.texts = texts; @@@
@@@ notifyDataSetChanged(); @@@
@@@} @@@
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View layout = convertView;
if (convertView == null) {
layout = inflater.inflate(R.layout.rotate, null);
}
Bitmap note = notes.get(position);
@@@String text = texts.get(position);@@@
ImageView tView = (ImageView) layout.findViewById(R.id.picture);
@@@TextView view = (TextView) layout.findViewById(R.id.text);@@@
tView.setImageBitmap(note);
@@@view.setText(text);@@@
return layout;
}
}
我已经可以显示图像了,我想使用相同的方法在图像附近显示文本,但是在添加@@@和@@@中保存的代码后出现了错误.请帮我弄清楚.
I could show the image already.I want to show the texts near by the image by using the same way, but I got the error after I add the code that held in @@@ and @@@. Please help me to make it clear.
这是日志,
08-22 04:11:55.348: E/AndroidRuntime(3756): FATAL EXCEPTION: main
08-22 04:11:55.348: E/AndroidRuntime(3756): junit.framework.AssertionFailedError: Invalid selection position
08-22 04:11:55.348: E/AndroidRuntime(3756): at junit.framework.Assert.fail(Assert.java:50)
08-22 04:11:55.348: E/AndroidRuntime(3756): at junit.framework.Assert.assertTrue(Assert.java:20)
08-22 04:11:55.348: E/AndroidRuntime(3756): at com.aphidmobile.flip.FlipViewController.setSelection(FlipViewController.java:312)
08-22 04:11:55.348: E/AndroidRuntime(3756): at com.aphidmobile.flip.FlipViewController.onDataChanged(FlipViewController.java:606)
08-22 04:11:55.348: E/AndroidRuntime(3756): at com.aphidmobile.flip.FlipViewController.access$2(FlipViewController.java:596)
08-22 04:11:55.348: E/AndroidRuntime(3756): at com.aphidmobile.flip.FlipViewController$MyDataSetObserver.onChanged(FlipViewController.java:613)
08-22 04:11:55.348: E/AndroidRuntime(3756): at android.database.DataSetObservable.notifyChanged(DataSetObservable.java:37)
08-22 04:11:55.348: E/AndroidRuntime(3756): at android.widget.BaseAdapter.notifyDataSetChanged(BaseAdapter.java:50)
08-22 04:11:55.348: E/AndroidRuntime(3756): at com.example.bookard.TestQAQActivity$NoteViewAdapter.setTexts(TestQAQActivity.java:100)
08-22 04:11:55.348: E/AndroidRuntime(3756): at com.example.bookard.TestQAQActivity$1.done(TestQAQActivity.java:58)
08-22 04:11:55.348: E/AndroidRuntime(3756): at com.example.bookard.TestQAQActivity$1.done(TestQAQActivity.java:1)
08-22 04:11:55.348: E/AndroidRuntime(3756): at com.parse.ParseTaskUtils$2$1.run(ParseTaskUtils.java:107)
08-22 04:11:55.348: E/AndroidRuntime(3756): at android.os.Handler.handleCallback(Handler.java:730)
08-22 04:11:55.348: E/AndroidRuntime(3756): at android.os.Handler.dispatchMessage(Handler.java:92)
08-22 04:11:55.348: E/AndroidRuntime(3756): at android.os.Looper.loop(Looper.java:137)
08-22 04:11:55.348: E/AndroidRuntime(3756): at android.app.ActivityThread.main(ActivityThread.java:5103)
08-22 04:11:55.348: E/AndroidRuntime(3756): at java.lang.reflect.Method.invokeNative(Native Method)
08-22 04:11:55.348: E/AndroidRuntime(3756): at java.lang.reflect.Method.invoke(Method.java:525)
08-22 04:11:55.348: E/AndroidRuntime(3756): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-22 04:11:55.348: E/AndroidRuntime(3756): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-22 04:11:55.348: E/AndroidRuntime(3756): at dalvik.system.NativeStart.main(Native Method)
Here is the log,
08-22 04:11:55.348: E/AndroidRuntime(3756): FATAL EXCEPTION: main
08-22 04:11:55.348: E/AndroidRuntime(3756): junit.framework.AssertionFailedError: Invalid selection position
08-22 04:11:55.348: E/AndroidRuntime(3756): at junit.framework.Assert.fail(Assert.java:50)
08-22 04:11:55.348: E/AndroidRuntime(3756): at junit.framework.Assert.assertTrue(Assert.java:20)
08-22 04:11:55.348: E/AndroidRuntime(3756): at com.aphidmobile.flip.FlipViewController.setSelection(FlipViewController.java:312)
08-22 04:11:55.348: E/AndroidRuntime(3756): at com.aphidmobile.flip.FlipViewController.onDataChanged(FlipViewController.java:606)
08-22 04:11:55.348: E/AndroidRuntime(3756): at com.aphidmobile.flip.FlipViewController.access$2(FlipViewController.java:596)
08-22 04:11:55.348: E/AndroidRuntime(3756): at com.aphidmobile.flip.FlipViewController$MyDataSetObserver.onChanged(FlipViewController.java:613)
08-22 04:11:55.348: E/AndroidRuntime(3756): at android.database.DataSetObservable.notifyChanged(DataSetObservable.java:37)
08-22 04:11:55.348: E/AndroidRuntime(3756): at android.widget.BaseAdapter.notifyDataSetChanged(BaseAdapter.java:50)
08-22 04:11:55.348: E/AndroidRuntime(3756): at com.example.bookard.TestQAQActivity$NoteViewAdapter.setTexts(TestQAQActivity.java:100)
08-22 04:11:55.348: E/AndroidRuntime(3756): at com.example.bookard.TestQAQActivity$1.done(TestQAQActivity.java:58)
08-22 04:11:55.348: E/AndroidRuntime(3756): at com.example.bookard.TestQAQActivity$1.done(TestQAQActivity.java:1)
08-22 04:11:55.348: E/AndroidRuntime(3756): at com.parse.ParseTaskUtils$2$1.run(ParseTaskUtils.java:107)
08-22 04:11:55.348: E/AndroidRuntime(3756): at android.os.Handler.handleCallback(Handler.java:730)
08-22 04:11:55.348: E/AndroidRuntime(3756): at android.os.Handler.dispatchMessage(Handler.java:92)
08-22 04:11:55.348: E/AndroidRuntime(3756): at android.os.Looper.loop(Looper.java:137)
08-22 04:11:55.348: E/AndroidRuntime(3756): at android.app.ActivityThread.main(ActivityThread.java:5103)
08-22 04:11:55.348: E/AndroidRuntime(3756): at java.lang.reflect.Method.invokeNative(Native Method)
08-22 04:11:55.348: E/AndroidRuntime(3756): at java.lang.reflect.Method.invoke(Method.java:525)
08-22 04:11:55.348: E/AndroidRuntime(3756): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-22 04:11:55.348: E/AndroidRuntime(3756): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-22 04:11:55.348: E/AndroidRuntime(3756): at dalvik.system.NativeStart.main(Native Method)
推荐答案
在适配器中,texts
应该是ArrayList<String>
,而不是ArrayList<Bitmap>
.我不知道这是否可以解决,但肯定是一个问题.
In the adapter, texts
should be an ArrayList<String>
, not an ArrayList<Bitmap>
. I don't know if this fixes but surely is an issue.
这篇关于Android从parse.com检索图像和文本并在适配器中使用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!