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 も用意しています。

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

}
4

2 に答える 2

0

MyActivity.java クラスを 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();
    }
}
于 2013-07-22T11:42:40.510 に答える