我正在尝试用 Parse.com 上的数据库中的对象填充 ListView,但我遇到了麻烦,因为 ListView 不直接接受对象。我尝试通过遍历我的对象并将它们转换为字符串来制作字符串数组,但失败了。我也尝试在对象上调用 .toString ,但也没有运气。
我的计划是遍历所有解析对象,然后将它们添加到 ListView。然后,该 ListView 将用于允许用户搜索 Parse.com 中的所有对象。Parse.com 中的对象应替换 listview_array[] 数据......例如替换“BudWeiser”、“Dubra”等......
我为检索对象名称、价格、大小而创建的循环确实有效,但我无法将它放入 listView 中,因为我相信它是一个对象。
这是我的代码
package com.alpha.dealtap;
import java.lang.reflect.Array;
import java.util.List;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import com.parse.FindCallback;
import com.parse.Parse;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;
public class Search_Page extends Activity {
private ListView lv;
private EditText et;
public String listview_array[] = { "BudWeiser", "Dubra", "Corona",
"Jack Daniels", "Smirnoff", "Keystone Light", "Natural Ice" };
private ArrayList<String> array_sort = new ArrayList<String>();
public ArrayList<Object> _arrayList = new ArrayList<Object>();
int textlength = 0;
ParseObject dealsObject;
int n = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search_page);
Parse.initialize(this, "vUz23Z6zdIL1jbxbVWeLpsSdu1ClTu3YiG30zTWY",
"4BTyoq1QQKows8qVJV7lvU3ZokSRrLFyOCPzffwJ");
// Using this Parsequery...I can grab/locate the objects from the Parse
// list...Here I grabbed the objects that have Burnettes and the price
// of it
// Eventually I may need to set a limit on how many results I will get
// from the for loop....So far I think it is limited to 100 by default
ParseQuery query = new ParseQuery("Deals");
// query.whereEqualTo("Brand", "Burnettes");
query.findInBackground(new FindCallback() {
@Override
public void done(List<ParseObject> objects, ParseException e) {
if (e == null) {
Log.d("Brand", "Retrieved " + objects.size() + " Brands");
for (ParseObject dealsObject : objects) {
// use dealsObject.get('columnName') to access the
// properties of the Deals object
Object brands = dealsObject.get("Brand").toString();
_arrayList.add(brands);
listview_array = _arrayList;
//Having trouble putting strings into an array list....Have to cast as ArrayList and then it causes error!
Log.d("Size", (String) dealsObject.get("Size"));
Log.d("Price", (String) dealsObject.get("Price"));
Log.d("Brand", (String) dealsObject.get("Brand"));
n++;
}
} else {
Log.d("Brand", "Error: " + e.getMessage());
}
}
});
Button store = (Button) findViewById(R.id.b1);
Button deal = (Button) findViewById(R.id.b2);
lv = (ListView) findViewById(R.id.ListView01);
et = (EditText) findViewById(R.id.search_box);
lv.setAdapter(new ArrayAdapter<Object>(this,
android.R.layout.simple_list_item_1, listview_array));
et.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
// Abstract Method of TextWatcher Interface.
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// Abstract Method of TextWatcher Interface.
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
textlength = et.getText().length();
array_sort.clear();
for (int i = 0; i < listview_array.length; i++) {
if (textlength <= listview_array[i].length()) {
if (et.getText()
.toString()
.equalsIgnoreCase(
(String) listview_array[i].subSequence(
0, textlength))) {
array_sort.add(listview_array[i]);
}
}
}
lv.setAdapter(new ArrayAdapter<String>(Search_Page.this,
android.R.layout.simple_list_item_1, array_sort));
}
});
store.setOnClickListener(new View.OnClickListener() {
// When you use OnClickListener...you need the onClick method inside
// of it
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
startActivity(new Intent("com.alpha.dealtap.STOREPAGE"));
}
});
deal.setOnClickListener(new View.OnClickListener() {
// When you use OnClickListener...you need the onClick method inside
// of it
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
startActivity(new Intent("com.alpha.dealtap.DEALPAGE"));
}
});
}
protected void onListItemClick(ListView l, View v, int position, long id) {
// Cheese equals the position for which item that is clicked...so it
// depends on which item that is clicked
String cheese = "TAPDEAL";
try {
Class ourClass = Class.forName("com.alpha.dealtap." + cheese);
Intent ourIntent = new Intent(Search_Page.this, ourClass);
startActivity(ourIntent);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
protected void onPause() {
super.onPause();
}