package com.parse.starter;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Editable;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.parse.FindCallback;
import com.parse.GetCallback;
import com.parse.ParseAnalytics;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;

public class ParseStarterProjectActivity extends Activity {
	/** Called when the activity is first created. */
	private AsyncImageLoader imageLoader = new AsyncImageLoader();
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		TextView t = (TextView)findViewById(R.id.headText);
		t.setText("商品点评");		
		ParseAnalytics.trackAppOpened(getIntent());
		
		ParseObject gameScore = new ParseObject("GameScore");
		gameScore.put("score", 1337);
		gameScore.put("playerName", "Sean Plott");
		gameScore.put("cheatMode", false);
		gameScore.saveInBackground();
	}
	
    public void onSearchButtonClick(View view)
    {
    	Toast.makeText(this, "搜索Brand!", Toast.LENGTH_SHORT).show();

        /* 关闭当前的Activity */
        //MainActivity.this.finish();
    	ParseQuery<ParseObject> query = ParseQuery.getQuery("Product");
    	EditText e = (EditText)findViewById(R.id.editText1);    	
    	String input_brand = e.getText().toString();
    	try{
	    	query.whereContains("brandName", input_brand);
	    	query.findInBackground(new FindCallback<ParseObject>() {
	    	    public void done(List<ParseObject> productList, ParseException e) {
	    	    	TextView t = (TextView)findViewById(R.id.textView1);
	    	        if (e == null) {
	    	            Log.d("product", "Retrieved " + productList.size() + " products");
	    	            ParseObject p = productList.get(0);
	    	    		t.setText(p.getString("description"));	
	    	    		
	    	    		final ImageView imageView = (ImageView) findViewById(R.id.imageView1);
	    				imageLoader.loadDrawable(p.getString("brandLogoUrl"), new com.parse.starter.AsyncImageLoader.ImageCallback() {

	    					public void imageLoaded(Drawable imageDrawable, String imageUrl) {
	    						imageView.setImageDrawable(imageDrawable);
	    					}
	    				});
	    	    		
	    	    		
	    	        } else {
	    	            Log.d("product", "Error: " + e.getMessage());
	    	            t.setText("无此Brand");
	    	        }
	    	    }
	    	});
	    } catch (Exception e1) {
		
	    }

    }
}

@@ -1450,15 +1450,12 @@
 ry(%22
-Product
+data
 %22);%0A
@@ -1969,16 +1969,59 @@
         
+%0A%09    %09            try%7B%0A%09%09    %09            
 ParseObj
@@ -2048,16 +2048,17 @@
 get(0);%0A
+%09
 %09    %09  
@@ -2088,25 +2088,134 @@
 ng(%22
-description
+companyName%22) + '%5Cn' + p.getString(%22brandName%22)+ '%5Cn' + p.getString(%22lastCategory%22)+ '%5Cn' + p.getString(%22provinceCnName
 %22));%09%0A
+%09
 %09   
@@ -2215,32 +2215,33 @@
 %09%0A%09%09    %09    %09%09%0A
+%09
 %09    %09    %09%09fina
@@ -2311,42 +2311,34 @@
 1);%0A
+%09
 %09    %09
-%09%09%09imageLoader.loadDrawable(
+    %09%09String url = 
 p.ge
@@ -2342,25 +2342,28 @@
 .getString(%22
-b
+newB
 randLogoUrl%22
@@ -2363,16 +2363,88 @@
 ogoUrl%22)
+;%0A%09%09    %09    %09%09if (url != null)%7B%0A%09%09%09    %09%09%09%09imageLoader.loadDrawable(url
 , new co
@@ -2494,17 +2494,21 @@
 ack() %7B%0A
-%0A
+%09%09%0A%09%09
 %09    %09%09%09
@@ -2572,24 +2572,26 @@
 imageUrl) %7B%0A
+%09%09
 %09    %09%09%09%09%09%09i
@@ -2632,16 +2632,18 @@
 wable);%0A
+%09%09
 %09    %09%09%09
@@ -2646,16 +2646,18 @@
  %09%09%09%09%09%7D%0A
+%09%09
 %09    %09%09%09
@@ -2657,24 +2657,95 @@
     %09%09%09%09%7D);%0A
+%09%09    %09    %09%09%7D%0A%09    %09            %7D%0A%09%09    %09    %09catch (Exception e1) %7B%7D%0A
 %09    %09    %09%09

add more data;add try/catch when no url/data, no crash; new fields