0

我有列表视图,它从网站获取数据并显示在列表视图中,我想使用搜索功能,在我的列表视图中我有标题和内容。我想单独使用内容进行搜索就足够了。如何为我的列表视图使用搜索功能.

它在这一行显示问题 List newListTwo=new List(); 错误是无法实例化类型列表

我的活动.java

@Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_item); 



        lv1 =(ListView)findViewById(R.id.list); 
        lv =(ListView)findViewById(R.id.list);

        btnGetSelected = (Button) findViewById(R.id.btnget);
        btnGetSelected.setOnClickListener(this);

         myFilter = (EditText) findViewById(R.id.myFilter);

         myFilter.addTextChangedListener(new TextWatcher() {



                @Override
                public void beforeTextChanged(CharSequence s, int start, int count,int after) {
                }

                @Override
                public void afterTextChanged(Editable s) {
                }
                public void onTextChanged(CharSequence s,
                        int start, int before, int count)
                {
                    String selection = myFilter.getText().toString().toLowerCase();



                    List<Application> newListTwo=new List<Application>();
                        int textlength = selection.trim().length();
                        System.err.println("selection" + textlength);
                        newListTwo.clear();
                        for (int i = 0; i < items.size(); i++)
                        {
                            // -------------- seach according to the content starts with -------------------------

                            if(items.get(i).getContent().toLowerCase().startsWith(selection))
                            {


                                System.err.println("Selection: " + selection);

                                newListTwo.add(items.get(i));
                            }
                        }

                        //---------------- Again Call your List View ------------------
                        adapter=new ApplicationAdapter(MainActivity.this, newListTwo);
                        setListAdapter(adapter);

                }

                private void setListAdapter(ApplicationAdapter adapter) {
                    // TODO Auto-generated method stub

                }
            });



        //praycount.setOnClickListener(this);
        initView();

}





    private void initView(){
        // show progress dialog
        dialog = ProgressDialog.show(this, "", "Loading...");
        String url = "http://www.ginfy.com/api/v1/posts.json";
        FetchDataTask task = new FetchDataTask(this);
        task.execute(url);


    }

对于我的代码如何使用搜索功能,我也有我的 applicaton.java 和 applicationadapter.java

应用适配器.java

@SuppressLint("NewApi")
public class ApplicationAdapter extends ArrayAdapter<Application> implements
TextToSpeech.OnInitListener{
    private List<Application> items;
    private LayoutInflater inflator;
    private MainActivity activity;

    private ProgressDialog dialog;
    public TextToSpeech tts;
    public ImageButton btnaudioprayer;
   public TextView text1;

    ArrayAdapter<String> adapter;



    public ApplicationAdapter(MainActivity context, List<Application> items){
        super(context, R.layout.activity_row, items);
        this.items = items;

        inflator = LayoutInflater.from(getContext());
        activity=context;


    }

    @Override
    public int getCount(){
        return items.size();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        ViewHolder holder = null;

        tts = new TextToSpeech(activity, ApplicationAdapter.this);


        //View v = convertView;
        if ( convertView == null ){ 
            convertView = inflator.inflate(R.layout.activity_row, null);
            holder = new ViewHolder();
            holder.text2 = (TextView) convertView.findViewById(R.id.text2);
            holder.text1 = (TextView) convertView.findViewById(R.id.text1);
            holder.count = (TextView) convertView.findViewById(R.id.count); 
            holder.pray  = (Button) convertView.findViewById(R.id.pray);
            holder.chk = (CheckBox) convertView.findViewById(R.id.checkbox);
            holder.btnaudioprayer = (ImageButton) convertView.findViewById(R.id.btnaudioprayer);

            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.chk.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton view,
                    boolean isChecked) {
                int getPosition = (Integer) view.getTag();
                items.get(getPosition).setSelected(view.isChecked());

            }
        });

        holder.pray.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                int getPosition= (Integer)v.getTag();
                StringBuffer sb1 = new StringBuffer();
                sb1.append("ID :");
                sb1.append(Html.fromHtml(""+items.get(getPosition).getId()));
                sb1.append("\n");
                activity.praydata(items.get(getPosition).getId());
                //activity.showAlertView(sb1.toString().trim());
                //activity.praydata(Integer.parseInt(sb1.toString().trim()));
            }


        });


         holder.btnaudioprayer.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View V) {
                    View parent = (View)V.getParent();
                    ViewHolder vh = (ViewHolder)parent.getTag();
                    TextView tv = vh.text1;
                    speakOut(tv.getText().toString());
                }

            }); 




        Application app = items.get(position);
        holder.chk.setTag(position);
        holder.pray.setTag(position);
        holder.text2.setText(Html.fromHtml(app.getTitle()));
        holder.text1.setText(Html.fromHtml(app.getContent()));
        holder.count.setText(app.getCount()+"");
        holder.chk.setChecked(app.isSelected());



        return convertView;
    }





    static class ViewHolder {
        public TextView text2;
        public TextView text1;
        public TextView count;
        public CheckBox chk;
        public Button pray;
        public ImageButton btnaudioprayer;
        private TextToSpeech tts;
    }
    //return convertView;





    @Override
    public void onInit(int status) {
        if (status == TextToSpeech.SUCCESS) {

            int result = tts.setLanguage(Locale.US);

            if (result == TextToSpeech.LANG_MISSING_DATA
                    || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                Log.e("TTS", "This Language is not supported");
            } 
        } else {
            Log.e("TTS", "Initilization Failed!");
        }


    }



     private void speakOut(String text) {

         tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
        }

}
4

2 回答 2

3

使用 addTextChangeListener 根据内容显示列表:-

searchContentEditText.addTextChangedListener(new TextWatcher()
            {
                public void afterTextChanged(Editable s)
                {

                }
                public void beforeTextChanged(CharSequence s, int start, int count, int after)
                {
                }
                public void onTextChanged(CharSequence s,
                        int start, int before, int count)
                {
                    selection= searchContentEditText.getText().toString().toLowerCase();



                        List newListTwo=new ArrayList();
                        textlength = selection.trim().length();
                        System.err.println("selection" + textlength);
                        newListTwo.clear();
                        for (int i = 0; i < contactList.size(); i++)
                        {
                            // -------------- seach according to the content starts with -------------------------

                            if(firstList.get(i).getContent().toLowerCase().startsWith(selection))
                            {


                                System.err.println("Selection: " + selection);

                                newListTwo.add(firstList.get(i));
                            }
                        }

                        //---------------- Again Call your List View ------------------
                        adapter=new ContactListAdapter(MyActivity.this, newListTwo);
                        setListAdapter(adapter);

                }
            });
于 2013-07-22T11:33:04.810 回答
0

您需要使用 TextWatcher 实现 MyActivity.java 类。

public class MyActivity extends Activity implements TextWatcher  {


myFilter = (EditText) findViewById(R.id.myFilter);
myFilter.addTextChangedListener(this);

@Override
public void afterTextChanged(Editable s) {
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {

    Group[] group_array = null;
    this.array_sort = new ArrayList<Group>();
    try {
        if(user != null) {
            group_array = this.user.getUser_groups().getData();
        }
        int textlength = this.search_bar.getText().length();
        for(int i=0; i<group_array.length; i++) { 

            if(textlength <= group_array[i].getGroup_name().length()) {

                if(((String) group_array[i].getGroup_name().subSequence(0, textlength))
                        .equalsIgnoreCase(this.search_bar.getText().toString()))
                {
                    this.array_sort.add(group_array[i]);
                }
            }
        }
        if(this.array_sort.size() > 0) { 
            Group[] groups = new Group[this.array_sort.size()];
            FetchGroups.this.group_adapter = new GroupAdapter(FetchGroups.this,R.layout.row_fetch_groups,
                    android.R.layout.simple_list_item_1, this.array_sort.toArray(groups));
            FetchGroups.this.groups_list.setAdapter(group_adapter);
        }
    } catch(Exception e) {
        e.printStackTrace();
    }
}
于 2013-07-22T11:42:40.510 回答