列表视图搜索功能+和开放活动

时间:2016-10-17 15:05:52

标签: java string listview android-activity android-edittext

我遇到的问题是我的搜索功能不起作用。我想在ListView上创建一个搜索功能,当我输入一些文本时,项目会被过滤,当我点击某个项目时,它会转到另一个活动。

我的代码:

package com.shady.letsdoit;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.HashMap;

public class Search extends AppCompatActivity {
    ArrayList<HashMap<String, String>> productList;
    private EditText search_box;
    private ArrayAdapter<String> listAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        search_box = (EditText) findViewById(R.id.search_box);
        final ListView list_view = (ListView) findViewById(R.id.list_view);
        String products[] = getResources().getStringArray(R.array.search);

/** <string-array name="search"> <item>@string/Android_whats_android</item> <item>@string/Android_perso_wallpaper</item> <item>@string/Android_perso_ringtone</item> <item>@string/Android_perso_lockscreen</item> <item>@string/Android_perso_langueg</item> <item>@string/Android_perso_dualsim</item> <item>@string/Android_perso_datetime</item> <item>@string/Android_perso_bluetooth</item> <item>@string/Android_perso_appmanagre</item> <item>@string/Android_perso_accounts</item> <item>@string/Android_f_m</item> <item>@string/Android_insta</item> <item>@string/Android_telgram</item> <item>@string/Android_bestapp</item> <item>@string/Android_root</item> <item>@string/Pc_whats_win</item> <item>@string/Pc_boot</item> <item>@string/Pc_nasb_win_7</item> <item>@string/Pc_nasb_win_8</item> <item>@string/Pc_nasb_win_10</item> <item>@string/Pc_win7_perso</item> <item>@string/Pc_win10_perso</item> <item>@string/Pc_win_internet_browser</item> <item>@string/Pc_win_internet_download</item> <item>@string/Pc_win_safe</item> <item>@string/Pc_win_errors_softwere</item> <item>@string/Pc_win_errors_hardwere</item> <item>@string/Pc_win_bestsoftweres</item> </string-array> **/

        listAdapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.subject_name, products);
        list_view.setAdapter(listAdapter);
        list_view.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String selectedItem = (String) parent.getItemAtPosition(position);
                if (selectedItem.equals("@string/Android_perso_lockscreen")) { /**each and every single of these "@string..." are an Activity for example * @string/Android_perso_wallpaper = Android_perso_wallpaper.class * that I want to open it when I click on same Item * I tried some methods but it didn't work plz help me thanks. */
                    Intent intent = new Intent(Search.this, Android_perso_lockscreen.class);
                    startActivity(intent);
                }
            }
        });
        search_box.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
            }

            @Override
            public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
                Search.this.listAdapter.getFilter().filter(cs);
            }

            @Override
            public void afterTextChanged(Editable editable) {
            }
        });
    }

    /**
     * my action bar
     **/
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.actionbar, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_search:
                Intent search = new Intent(this, Search.class);
                search.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(search);
                return true;
            case R.id.action_home:
                Intent home = new Intent(this, MainActivity.class);
                home.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(home);
                return true;
            case R.id.action_fav:
                Intent favorite = new Intent(this, Favorits.class);
                favorite.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(favorite);
                return true;
            case R.id.action_phone:
                Intent phone = new Intent(this, phon.class);
                phone.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(phone);
                return true;
            case R.id.action_pc:
                Intent pc = new Intent(this, pc.class);
                pc.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(pc);
                return true;
        }
        return false;
    }
}

1 个答案:

答案 0 :(得分:0)

要在listview中搜索,您可以调用方法::

private void setFilter(CharSequence cs) {
    String filterProducts[] = new String[]{};
    if (!TextUtils.isEmpty(cs)) {
        int j = 0;
        for (int i = 0; i < products.length; i++) {
            if (products[i].contains(cs)) {
                filterProducts[j] = cs.toString();
                j++;
            }
        }
        listAdapter = new ArrayAdapter < String > (this, R.layout.list_item, R.id.subject_name, filterProducts);
        list_view.setAdapter(listAdapter);

    }else{
        listAdapter = new ArrayAdapter < String > (this, R.layout.list_item, R.id.subject_name, products);
        list_view.setAdapter(listAdapter);
    }
}

从这里调用::

 @Override public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
          setFilter(cs);
 }

为了重定向到下一个活动。你必须解释你想做什么以及你面临的问题。因为你的代码没有那么明确的指示。