addTextChangedListner列表视图 [英] addTextChangedListner listview

查看:178
本文介绍了addTextChangedListner列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class DictionaryListActivity extends Activity {

TextView userTextView;
EditText searchEditText;
Button searchButton;
ListView dictionaryListView;

String logTagString="DICTIONARY";
ArrayList<WordDefinition> allWordDefinitions=new ArrayList<WordDefinition>();


DictionaryDatabaseHelper myDictionaryDatabaseHelper;
SharedPreferences sharedPreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dictionary_list);

    Log.d("DICTIONARY", "second activity started");


    userTextView=(TextView) findViewById(R.id.personTextView);

     SharedPreferences settings = getSharedPreferences(Jam.SHARED_NAME_STRING, 0);
     String d =settings.getString(Jam.USER_NAME_STRING, null);
     userTextView.setText(d);





    searchEditText=(EditText) findViewById(R.id.searchEditText);
    searchButton=(Button) findViewById(R.id.searchButton);
    dictionaryListView=(ListView) findViewById(R.id.dictionaryListView);



    //SharedPrefernce for TextView
    myDictionaryDatabaseHelper=new DictionaryDatabaseHelper(this, "Dictionary", null, 1);
    sharedPreferences=getSharedPreferences(FragmentA.SHARED_NAME_STRING2, MODE_PRIVATE);


    boolean initialized=sharedPreferences.getBoolean("initialized", false);

       if (initialized==false) {
            //Log.d(logTagString, "initializing for the first time");
              initializeDatabase();
              SharedPreferences.Editor editor=sharedPreferences.edit();
              editor.putBoolean("initialized", true);
              editor.commit();

       }else {
              Log.d(logTagString, "db already initialized");
       }

    allWordDefinitions=myDictionaryDatabaseHelper.getAllWords();




    //ListView adapter

    dictionaryListView.setAdapter(new BaseAdapter() {

          @Override
          public View getView(int position, View view, ViewGroup arg2) {
               if (view==null) {
                     view=getLayoutInflater().inflate(R.layout.list_item, null);
               }
               TextView textView=(TextView) view.findViewById(R.id.listItemTextView);
               Typeface font = Typeface.createFromAsset(getAssets(), "AnjaliOldLipi.ttf");
               textView.setTypeface(font);
               textView.setText(allWordDefinitions.get(position).word +"\n" + allWordDefinitions.get(position).definition );
              return view;
          }


          @Override
          public long getItemId(int arg0) {
               // TODO Auto-generated method stub
               return 0;
          }

          @Override
          public Object getItem(int arg0) {
               // TODO Auto-generated method stub
               return null;
          }

          @Override
          public int getCount() {
               // TODO Auto-generated method stub
               return allWordDefinitions.size();
          }
    });



    //ListView ClickListner

    dictionaryListView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View view, int position,
                long arg3) {
            Intent intent =new Intent("android.intent.action.JAM");
            intent.putExtra("word", allWordDefinitions.get(position).word);
            intent.putExtra("definition", allWordDefinitions.get(position).definition);

            startActivity(intent);
        }
    });



    //Button listener

    searchButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            String string=searchEditText.getText().toString();
            WordDefinition wordDefinition=myDictionaryDatabaseHelper.getWordDefinition(string);

            if (string ==null) {
                Toast.makeText(DictionaryListActivity.this, "No word Entered", Toast.LENGTH_LONG).show();
            }
            else if (wordDefinition==null && string !=null) {
                Toast.makeText(DictionaryListActivity.this, "Word not found", Toast.LENGTH_LONG).show();            
            }else {
                Intent intent =new Intent("android.intent.action.JAM");
                intent.putExtra("word", wordDefinition.word);
                intent.putExtra("definition", wordDefinition.definition);

                startActivity(intent);
            }


        }
    });




}

//Initializing DB

private void initializeDatabase() {
    InputStream inputStream=getResources().openRawResource(R.raw.dictionary);
    BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
    DictionaryLoader.loadData(bufferedReader, myDictionaryDatabaseHelper);

}

}

我要搜索功能添加到使用addTextChangedListner列表视图但在这个code有没有arrayadpater只是一个ArrayList中,通过这个code是从tutorial..And的方式,你可以解释一下它是什么?以及如何添加使用的ArrayList&LT该功能;>如果你可以改变这个code ..

I want to add search functionality to the listview using addTextChangedListner but in this code there is no arrayadpater only a ArrayList,By the way this code is from a tutorial..And can you explain what it is? And How to add that feature using Arraylist<>.If you can alter this code..

推荐答案

您需要为ListView控件创建适配器。创建适配器后,你需要穿上AutocomPleteTextView。在此之后,阅读本: http://www.tutorialspoint.com/android/android_auto_complete.htm

You need to create adapter for that ListView. after you create adapter, you need to put on AutocomPleteTextView. After that, read this: http://www.tutorialspoint.com/android/android_auto_complete.htm

您需要有一个将通过阵列的方法是在ListView并找出wheather字符串或字符匹配。

YOu need to have a method that will go through your array that is in the ListView and find out wheather the strings or characters match.

这篇关于addTextChangedListner列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆