我正在尝试用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();
}
答案 0 :(得分:3)
您可以使用BaseAdapter类进行自定义列表视图。这是代码。
List<ParseObject> parse = new ArrayList<ParseObject>();
public class CustomChannelListAdapter extends BaseAdapter {
private Context context;
public CustomChannelListAdapter(Context context) {
super();
this.context = context;
}
@Override
public int getCount() {
if (parse != null) {
return parse.size();
}
return 0;
}
@Override
public Channel getItem(int position) {
// TODO Auto-generated method stub
return parse.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
TextView tittle = null;
TextView desc = null;
TextView nowPlaying = null;
if (view == null) {
LayoutInflater inflater = ((Activity) context)
.getLayoutInflater();
view = inflater.inflate(R.layout.video_listrow, parent, false);
} else {
view = convertView;
}
tittle = (TextView) view.findViewById(R.id.title);
desc = (TextView) view.findViewById(R.id.Descp);
nowPlaying = (TextView) view.findViewById(R.id.NowplayingId);
if (parse!= null) {
if (parse.get(position).getName() != null) {
tittle.setText(parse.get(position).getName()
.toString());
} else {
tittle.setText("----------------------");
}
if (parse.get(position).getDesc() != null) {
desc.setText(parse.get(position).getDesc()
.toString());
} else {
desc.setText("------------------------");
}
}
return view;
}
}
<强> video_listrow.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="400dp"
android:layout_height="75dp"
android:background="@drawable/videolist_selector"
android:orientation="horizontal"
android:paddingLeft="5dp"
android:paddingRight="5dp" >
<!-- Title Of Song -->
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="5dp"
android:textColor="@color/orange"
android:textSize="18dip"
android:textStyle="bold"
android:typeface="sans"
android:ellipsize="marquee"
android:freezesText="true"
android:marqueeRepeatLimit="marquee_forever"
android:paddingLeft="5dip"
android:scrollHorizontally="true"
android:singleLine="true"
android:text="Tittle Not Found " />
<!-- Artist Name -->
<TextView
android:id="@+id/Descp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/title"
android:layout_marginTop="1dip"
android:layout_toRightOf="@+id/thumbnail"
android:paddingTop="5dp"
android:textColor="@color/white"
android:textSize="12dip"
android:textStyle="bold"
android:typeface="sans"
android:ellipsize="marquee"
android:freezesText="true"
android:marqueeRepeatLimit="marquee_forever"
android:paddingLeft="5dip"
android:scrollHorizontally="true"
android:singleLine="true"
android:text="Description Not Found"
/>
<!-- Rightend Duration -->
<!--
<TextView
android:id="@+id/duration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@id/title"
android:gravity="right"
android:text="5:45"
android:layout_marginRight="5dip"
android:textSize="10dip"
android:textColor="#10bcc9"
android:textStyle="bold"/>
-->
<!-- Rightend Arrow -->
<!--
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/arrow"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"/>
-->
<TextView
android:id="@+id/NowplayingId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="5dp"
android:layout_marginRight="4dp"
android:text="Now playing..."
android:textColor="@color/red"
android:textSize="12dp" />
</RelativeLayout>
在您的活动代码中
ListView videoList = (ListView) findviewByid(_);
CustomChannelListAdapter channelAdapter = new CustomChannelListAdapter(PlasmaView.this);
videoList.setAdapter(channelAdapter);