Mitch Thornton
Mitch Thornton

Reputation: 31

Populating a ListView with Objects from Parse.com Android

I'm trying to fill a ListView with objects from my database at Parse.com, but I'm am having trouble because the ListView does not accept objects directly. I've tried making an array of strings by looping through my objects and casting them as strings but that failed. I have also tried to call a .toString on the objects but no luck either.

My plan is to loop through all of the parse objects and then add them to the ListView. That ListView will then be used to allow the user to search through all of the objects from Parse.com. The objects from Parse.com should replace the listview_array[] data....ex replace "BudWeiser", "Dubra" etc....

The loop I did create to retrieve the Objects names, prices, size did work, but I could not place it into the listView since I believe it was an object.

Here's my code

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

Upvotes: 0

Views: 2617

Answers (1)

Bebin T.N
Bebin T.N

Reputation: 2649

you can use BaseAdapter class for custom listview. here is the code for that.

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>

In your activity code

     ListView videoList = (ListView) findviewByid(_);
   CustomChannelListAdapter channelAdapter = new CustomChannelListAdapter(PlasmaView.this);
                videoList.setAdapter(channelAdapter);

Upvotes: 3

Related Questions