ParseUser搜索

时间:2014-08-06 23:51:16

标签: java android xml parse-platform

我正在尝试为我的应用创建一个简单的用户搜索页面。我尝试创建一个ParseQuery并在主活动页面的Listview中显示结果。但是,当我运行程序时,我无法获得搜索结果,程序什么也没显示。如果我得到答案,我会很高兴。

这是我的MainActivity.java;

package com.example.searchtest;

import java.util.ArrayList;
import java.util.List;

import com.parse.*;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class MainActivity extends ActionBarActivity {

    private Button search;
    private EditText searchArea;
    private ListView userList;
    private String uName;
    private ArrayAdapter<ParseUser> mainAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Parse.initialize(this, "WHz2GmkGFIUxBir6ZEDt2FIG4WUcE1kfKZmPayxy", "IAs1RCaRt5LM1E1ZW7LFXy5uDjtZEQ5CDa3LKJt2");

        setContentView(R.layout.activity_main);


        search = (Button) findViewById(R.id.searchButton);
        searchArea = (EditText) findViewById(R.id.searchField);
        userList = (ListView) findViewById(R.id.results);

        mainAdapter = new ArrayAdapter<ParseUser>(this, R.layout.activity_main, R.layout.search_list );
        //mainAdapter.setTextKey("username");

        userList.setAdapter(mainAdapter);
        //mainAdapter.loadObjects();

        uName = searchArea.getText().toString();

        search.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                ParseQuery<ParseUser> query = ParseUser.getQuery();
                query.whereEqualTo("username", uName);

                query.findInBackground(new FindCallback<ParseUser>() {
                  public void done(List<ParseUser> objects, ParseException e) {
                    if (e == null) {
                        for(int i=0; i<objects.size();i++)
                        {
                            mainAdapter.add((ParseUser)objects.get(i));
                        }

                    } else {
                        // Something went wrong.
                        e.printStackTrace();
                    }
                  }
                });

                userList.setAdapter(mainAdapter);
            }

        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

我的主要活动xml文件;

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.searchtest.MainActivity" >

    <EditText
        android:id="@+id/searchField"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="29dp"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/searchButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/editText1"
        android:layout_alignParentRight="true"
        android:text="Search" />

    <ListView
        android:id="@+id/results"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="19dp" >
    </ListView>

</RelativeLayout>

2 个答案:

答案 0 :(得分:1)

我解决了这个问题。这是代码的更新版本;

package com.example.searchtest;

import java.util.ArrayList;
import java.util.List;

import com.parse.*;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {

    private Button search;
    private EditText searchArea;
    private ListView userList;
    //private String uName;
    private ArrayAdapter<ParseUser> mainAdapter;
    private ArrayList<ParseUser> resultList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Parse.initialize(this, "WHz2GmkGFIUxBir6ZEDt2FIG4WUcE1kfKZmPayxy", "IAs1RCaRt5LM1E1ZW7LFXy5uDjtZEQ5CDa3LKJt2");

        setContentView(R.layout.activity_main);


        search = (Button) findViewById(R.id.searchButton);
        searchArea = (EditText) findViewById(R.id.searchField);
        userList = (ListView) findViewById(R.id.userResultList);
        resultList = new ArrayList<ParseUser>();
        mainAdapter = new ArrayAdapter<ParseUser>(this, R.layout.list_item, R.id.user_results,resultList);
        //mainAdapter.setTextKey("username");

        //userList.setAdapter(mainAdapter);
        //mainAdapter.loadObjects();



        search.setOnClickListener(new OnClickListener() {



            @Override
            public void onClick(View v) {

                final String uName = searchArea.getText().toString();

                final ParseQuery<ParseUser> query = ParseUser.getQuery();
                query.whereEqualTo("username", uName);

                query.findInBackground(new FindCallback<ParseUser>() {
                  public void done(List<ParseUser> objects, ParseException e) {
                    if (e == null) {

                           for(int i=0; i<objects.size();i++)
                            {
                                resultList.add((ParseUser)objects.get(i));
                            }
                           userList.setAdapter(mainAdapter);

                        Toast.makeText(getApplicationContext(), "User is Found",
                                Toast.LENGTH_LONG).show();

                    } else {
                        // Something went wrong.
                        e.printStackTrace();
                        Toast.makeText(getApplicationContext(), "User is not Found",
                                Toast.LENGTH_LONG).show();
                    }
                  }
                });

                userList.setAdapter(mainAdapter);
            }

        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

此外,我还需要一个xml文件来显示列表。它是list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <!-- Single ListItem -->

    <!-- Product Name -->
    <TextView android:id="@+id/user_results"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="10dip"
        android:textSize="16dip"
        android:textStyle="bold"/>    

</LinearLayout>

和主xml文件,activity_main.xml;

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.searchtest.MainActivity" >

    <EditText
        android:id="@+id/searchField"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="29dp"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/searchButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/editText1"
        android:layout_alignParentRight="true"
        android:text="Search" />

    <ListView
        android:id="@+id/userResultList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/searchField"
        android:layout_below="@+id/searchField"
        android:layout_marginTop="39dp" >
    </ListView>

</RelativeLayout>

答案 1 :(得分:0)

put uName = searchArea.getText()。toString();在侦听器内部,您只在创建视图时设置一次uName,因此它将为空。您需要在用户输入输入并单击按钮后获取文本。