0

I have listview,its getting data from website and displaying in an Listview,I want to use search function,in my listview i am having title and content.I want to search using content alone thats enough.How to use search function for my listview.

It shows problem in this line List newListTwo=new List(); the error is Cannot instantiate the type List

Myactivity.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);


    }

For my code how to use the search function,i have my applicaton.java and applicationadapter.java also

Applicationadapter.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);
        }

}

2 Answers 2

3

Use addTextChangeListener to show list according to the content:-

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);

                }
            });
Sign up to request clarification or add additional context in comments.

Comments

0

You need to implement your MyActivity.java class with TextWatcher.

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();
    }
}

1 Comment

Dude can you see my adapter class,i want to search the content alone in that function,for that how to do

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.