搜索功能不起作用



感谢您的帮助。我不知道为什么没成功。我增加了以下功能来搜索用户。我调试它,它似乎卡在过滤器(argo)函数。我的代码如下。logcat错误中没有任何内容。谢谢你的帮助。

My code (updated code)

import java.util.List;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.app.SearchManager;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.SearchView.OnQueryTextListener;
import com.parse.FindCallback;
import com.parse.ParseException;
import com.parse.ParseQuery;
import com.parse.ParseRelation;
import com.parse.ParseUser;
import com.parse.SaveCallback;
public class EditFriendsActivity extends ListActivity {
    public static final String TAG = EditFriendsActivity.class.getSimpleName();
    protected ParseRelation<ParseUser> mFriendsRelation;
    protected ParseUser mCurrentUser;
    protected List<ParseUser> mUsers;
    CustomAdaptor adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        setContentView(R.layout.activity_edit_friends);
        // Show the Up button in the action bar.
        setupActionBar();
        getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

    }

    @Override
    protected void onResume() {
        super.onResume();
        mCurrentUser = ParseUser.getCurrentUser();
        mFriendsRelation = mCurrentUser.getRelation(ParseConstants.KEY_FRIENDS_RELATION);
        setProgressBarIndeterminateVisibility(true);
        ParseQuery<ParseUser> query = ParseUser.getQuery();
        query.orderByAscending(ParseConstants.KEY_USERNAME);
        query.setLimit(1000);
        query.findInBackground(new FindCallback<ParseUser>() {
            @Override
            public void done(List<ParseUser> users, ParseException e) {
                setProgressBarIndeterminateVisibility(false);
                if (e == null) {
                    // Success
                    mUsers = users;
                    String[] usernames = new String[mUsers.size()];
                    int i = 0;
                    for(ParseUser user : mUsers) {
                        usernames[i] = user.getUsername();
                        i++;
                    }
                    ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                            EditFriendsActivity.this, 
                            android.R.layout.simple_list_item_checked,
                            usernames);
                    setListAdapter(adapter);
                    addFriendCheckmarks();
                }
                else {
                    Log.e(TAG, e.getMessage());
                    AlertDialog.Builder builder = new AlertDialog.Builder(EditFriendsActivity.this);
                    builder.setMessage(e.getMessage())
                        .setTitle(R.string.error_title)
                        .setPositiveButton(android.R.string.ok, null);
                    AlertDialog dialog = builder.create();
                    dialog.show();
                }
            }
        });
    }

    private void setupActionBar() {
        getActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.findrecipients, menu); 
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView = (SearchView) menu.findItem(R.id.action_search)
                .getActionView();
        searchView.setSearchableInfo(searchManager
                .getSearchableInfo(getComponentName()));
        SearchView.OnQueryTextListener queryTextListener = new OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String arg0) {
                // TODO Auto-generated method stub
                android.content.Context context;
                context = getApplicationContext();
                Search srchContact = new Search(context, adapter, (ListView) mUsers);
                srchContact.Search(arg0);
                return true;
            }
            @Override
            public boolean onQueryTextChange(String arg0) {
                // TODO Auto-generated method stub
                return true;
            }
        };
        searchView.setOnQueryTextListener(queryTextListener);
                return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            NavUtils.navigateUpFromSameTask(this);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    public class Search
    {
        android.content.Context context;
        CustomAdaptor tempAdptr;
        CustomAdaptor adptr;
        android.widget.ListView lstContact;
        java.util.ArrayList<ParseUser> arrMyObject;
        public Search(android.content.Context con , CustomAdaptor adp , android.widget.ListView lst )
        {
            context = con;
            arrMyObject = new java.util.ArrayList<ParseUser>();
            tempAdptr = new CustomAdaptor(context, android.R.layout.simple_list_item_checked, arrMyObject);
            adptr = adp;
            lstContact = lst;
        }
        public void Search(String strSearch)
        {
            lstContact.setAdapter(tempAdptr);
            if(strSearch.equals(""))
            {
                lstContact.setAdapter(adptr);
                return ; 
            }
            arrMyObject.removeAll(arrMyObject) ; 
            for(int i = 0 ; i < mUsers.size() ; i++)
            {
                if(mUsers.get(i).getUsername().contains(strSearch)) 
                {
                    //Add item to your list
                }
            }
            tempAdptr.notifyDataSetChanged();
        }
    }

public class CustomAdaptor extends ArrayAdapter<ParseUser> {
    android.content.Context context;
    int resId;
    java.util.ArrayList<ParseUser> arrData;
    public CustomAdaptor(Context ac, int resource, java.util.ArrayList<ParseUser> data) {
        super(ac, resource, data);
        // TODO Auto-generated constructor stub
         context = ac;
            resId = resource;
            arrData = data;
    }

    @Override
    public android.view.View getView(final int position,android.view.View convertView, android.view.ViewGroup parent) 
    {
        try
        {
            ParseUser member = arrData.get(position);
            android.view.LayoutInflater inflater = (LayoutInflater) context.getSystemService(android.content.Context.LAYOUT_INFLATER_SERVICE);
            android.view.View Row =inflater.inflate(resId, parent , false);
            // Set Your XML file
            return Row;
        } 
        catch (Exception e)
        {
            return null;
        }
    }
}   

我有类似的代码,可以是有用的:
在此代码中,我们有几个类:
第一个:在列表中搜索并在列表视图中显示结果的搜索类
第二:准备列表视图的CustomAdapter
第三:在Search类中调用Search函数

public class Search
{
    android.content.Context context;
    CustomAdaptor tempAdptr;
    CustomAdaptor adptr;
    android.widget.ListView lstContact;
    java.util.ArrayList<MyObject> arrMyObject;
    public SearchContact(android.content.Context con , CustomAdaptor adp , android.widget.ListView lst )
    {
        context = con;
        arrMyObject = new java.util.ArrayList<MyObject>();
        tempAdptr = new CustomAdaptor(context, R.layout.contact_row, arrMyObject);
        adptr = adp;
        lstContact = lst;
    }
    public void Search(String strSearch)
    {
        lstContact.setAdapter(tempAdptr);
        if(strSearch.equals(""))
        {
            lstContact.setAdapter(adptr);
            return ; 
        }
        arrMyObject.removeAll(arrMyObject) ; 
        for(int i = 0 ; i < MainActivity.GetArrMyObject().size() ; i++)
        {
            if(arrlist.get(i).contains(strSearch)) 
            {
                //Add item to your list
            }
        }
        tempAdptr.notifyDataSetChanged();
    }
}

//------------------------------------------------------------------------------------
public class CustomAdaptor extends ArrayAdapter<MyObject> {
android.content.Context context;
int resId;
java.util.ArrayList<MyObject> arrData;
public ContactAdaptor(android.content.Context ac, int resource , java.util.ArrayList<MyObject> data ) {
    super(ac, resource, data);
    context = ac;
    resId = resource;
    arrData = data;
}
@Override
public android.view.View getView(final int position,android.view.View convertView, android.view.ViewGroup parent) 
{
    try
    {
        MyObject member = arrData.get(position);
        android.view.LayoutInflater inflater = (LayoutInflater) context.getSystemService(android.content.Context.LAYOUT_INFLATER_SERVICE);
        android.view.View Row =inflater.inflate(resId, parent , false);
        // Set Your XML file
        return Row;
    } 
    catch (Exception e)
    {
        return null;
    }
}
}


现在您可以在main中使用以下代码:

SearchContact srchContact = new SearchContact(context, adptr, arraylist);
    srchContact.Search(YourObject);

相关内容

  • 没有找到相关文章

最新更新