2012-09-07 87 views
1

我的代碼如下...當我按空格鍵做了AutoCompleteTextView顯示建議列表

public class NameListActivity extends Activity implements TextWatcher { 
    private Button add = null; 
    private AutoCompleteTextView editAuto = null; 
    private Button chfrlist = null; 
    private ImageView im = null; 
    String access_token = new String(); 
    private ImageView infobtn = null; 
    private PopupWindow popupWindow; 
    private View view; 
    private ProgressDialog pd; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
     setContentView(R.layout.activity_name_list); 
     access_token = MainService.readToken(); 

     add = (Button) findViewById(R.id.add_button); 
     editAuto = (AutoCompleteTextView) findViewById(R.id.editAuto); 
     chfrlist = (Button) findViewById(R.id.chfrlistbutton); 
     im = (ImageView) findViewById(R.id.helpact); 
     im.setOnClickListener(new ImageListener()); 
     infobtn = (ImageView) findViewById(R.id.informbtn); 
     initPopupWindow(); 
     infobtn.setOnClickListener(new infobtnListener()); 
     editAuto.addTextChangedListener(this); 
     add.setOnClickListener(new addListener()); 
     chfrlist.setOnClickListener(new ChfrListListener()); 
    } 

    public class addListener implements OnClickListener { 

     public void onClick(View v) { 
      addTask task = new addTask(); 
      task.execute(); 
      editAuto.setText(""); 
     } 
    } 

    public void afterTextChanged(Editable arg0) { 

    } 

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

    public void onTextChanged(CharSequence s, int start, int before, int count) { 
     onTextChangedTask task = new onTextChangedTask(); 
     task.execute(); 
    } 

    public class onTextChangedTask extends AsyncTask<Void, Void, Void> { 

     ArrayAdapter<String> adapter = null; 
     String[] userName = null; 
     String q = null; 
     JSONArray jsonArray = new JSONArray(); 
     JSONObject jsonObject = null; 
     ArrayList<String> userNameArrayList = new ArrayList<String>(); 
     Weibo weibo = new Weibo(); 

     @Override 
     protected void onPreExecute() { 
      weibo.setToken(access_token); 
      q = editAuto.getText().toString(); 
      System.out.println("start onTextChanged"); 
     } 

     @Override 
     protected Void doInBackground(Void... params) { 
      // TODO Auto-generated method stub 
      if (q.length() != 0) { 
       System.out.println("q is " + q); 

       String s1 = "https://api.weibo.com/search/suggestions/users.json"; 
       try { 
        jsonArray = Weibo.client.get(s1, 
          new PostParameter[] { new PostParameter("q", q) }) 
          .asJSONArray(); 
       } catch (Throwable e) { 
        System.out.println("這裏有個神馬異常呢 。。。" + e); 
       } 

       System.out.println("return length is " + jsonArray.length()); 

       for (int i = 0; i < jsonArray.length(); i++) { 
        try { 
         jsonObject = jsonArray.getJSONObject(i); 
         String sname = jsonObject.getString("screen_name"); 
         userNameArrayList.add(sname); 
        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 
       } 

       userName = (String[]) userNameArrayList 
         .toArray(new String[userNameArrayList.size()]); 

       adapter = new ArrayAdapter<String>(NameListActivity.this, 
         android.R.layout.simple_dropdown_item_1line, userName); 

      } 
      return null; 
     } 

     @Override 
     protected void onPostExecute(Void v) { 
      System.out.println("post"); 
      editAuto.setAdapter(adapter); 
     } 
    } 

    void showToast(String s) { 
     Toast toast = Toast.makeText(getApplicationContext(), s, 
       Toast.LENGTH_LONG); 
     toast.show(); 
    } 

    public class addTask extends AsyncTask<Void, Void, Void> { 

     String s = null; 
     boolean flag = false; 
     User user = null; 
     Weibo weibo = new Weibo(); 
     String screen_name = null; 

     protected void onPreExecute() { 
      Toast tt = Toast.makeText(getApplicationContext(), "正在將用戶添加到備份名單", 
        Toast.LENGTH_LONG); 
      tt.setGravity(Gravity.CENTER, 0, 0); 
      tt.show(); 
      weibo.setToken(access_token); 
      screen_name = editAuto.getText().toString(); 
     } 

     @Override 
     protected Void doInBackground(Void... params) { 
      // TODO Auto-generated method stub 

      if (screen_name.length() != 0) { 
       Users um = new Users(); 
       try { 
        user = new User(Weibo.client.get(
          "https://api.weibo.com/users/show.json", 
          new PostParameter[] { new PostParameter(
            "screen_name", screen_name) }) 
          .asJSONObject()); 
       } catch (Throwable e) { 
        e.printStackTrace(); 
        flag = true; 
        s = new String("您輸入的這個用戶好像不存在唉"); 
       } 
       if (user != null) { 
        ContentValues values = new ContentValues(); 
        values.put("uid", user.getId()); 
        values.put("user_name", user.getName()); 
        SQLiteDatabase db = null; 
        try { 
         db = MainService.getDatabase(); 
        } catch (Exception e) { 
         System.out.println("db error"); 
         finish(); 
        } 
        Cursor result = db.query("users", new String[] { "uid", 
          "user_name" }, "uid=?", 
          new String[] { user.getId() }, null, null, null); 
        if (result.getCount() == 0) 
         db.insert("users", null, values); 
       } else { 
        flag = true; 
        s = new String("網絡存在問題,檢查一下吧"); 
       } 
      } else { 
       flag = true; 
       s = new String("框裏輸入點東西才能添加啊"); 
      } 

      return null; 
     } 

     @Override 
     protected void onPostExecute(Void v) { 
      if (flag == true) { 
       System.out.println("要打印的是" + s); 
       showToast(s); 
      } 
     } 
    } 

    public class infobtnListener implements OnClickListener { 

     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      System.out.println("點擊了圖片"); 
      ColorDrawable cd = new ColorDrawable(-0000); 
      popupWindow.setBackgroundDrawable(cd); 
      // popupWindow.showAsDropDown(v); 
      popupWindow.showAtLocation(findViewById(R.id.informbtn), 
        Gravity.LEFT | Gravity.BOTTOM, 0, 100); 
     } 
    } 

    public class ImageListener implements OnClickListener { 

     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      // Intent t = new Intent(NameListActivity.this, 
      // GridLayoutActivity.class); 
      // startActivity(t); 
      finish(); 
     } 

    } 

    public class ChfrListListener implements OnClickListener { 

     public void onClick(View v) { 
      if ((haveInternet() == true) 
        && (GridLayoutActivity.hasAccessToken() == true)) { 
       // TODO Auto-generated method stub 
       pd = ProgressDialog.show(NameListActivity.this, "", 
         "正在從服務器上獲取數據,可能需要較長時間,請耐心等待 ..."); 
       /* 開啓一個新線程,在新線程裏執行耗時的方法 */ 
       new Thread(new Runnable() { 
        public void run() { 
         Intent t = new Intent(NameListActivity.this, 
           ChooseFromListActivity.class); 
         startActivity(t); 
         finish(); 

         handler.sendEmptyMessage(0);// 執行耗時的方法之後發送消給handler 
        } 
       }).start(); 
      } else { 
       Intent t = new Intent(NameListActivity.this, 
         WebViewActivity.class); 
       startActivity(t); 
       finish(); 
      } 
     } 
    } 

    private void initPopupWindow() { 
     view = getLayoutInflater().inflate(R.layout.namewindow, null); 
     popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.FILL_PARENT, 
       ViewGroup.LayoutParams.WRAP_CONTENT); 
     // 這裏設置顯示PopuWindow之後在外面點擊是否有效。如果爲false的話,那麼點擊PopuWindow外面並不會關閉PopuWindow。 
     popupWindow.setOutsideTouchable(true);// 不能在沒有焦點的時候使用 
    } 

    private boolean haveInternet() { 
     NetworkInfo info = ((ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE)) 
       .getActiveNetworkInfo(); 
     if (info == null || !info.isConnected()) { 
      return false; 
     } 
     if (info.isRoaming()) { 
      // here is the roaming option you can change it if you want to 
      // disable internet while roaming, just return false 
      return true; 
     } 
     return true; 
    } 

    Handler handler = new Handler() { 
     @Override 
     public void handleMessage(Message msg) {// handler接收到消息後就會執行此方法 
      pd.dismiss();// 關閉ProgressDialog 
     } 
    }; 

    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.activity_name_list, menu); 
     return true; 
    } 
} 

我的問題是:當我的EditText輸入的話,什麼都沒有發生。但是當我按下退格鍵時,AutoCompleteTextView顯示了建議列表...問題出在editAuto.setAdapter(adapter);

出了什麼問題?

+0

顯示您的適配器! –

+0

這一步做了什麼? 「jsonArray = Weibo.client.get(s1, new PostParameter [] {new PostParameter(」q「,q)}) .asJSONArray();」 –

+0

@ Archie.bpgc在服務器上獲取數據,我認爲問題不在於這裏 – Jerry

回答

1

使你的代碼

1)替代

private AutoCompleteTextView editAuto = null;以下變化只是寫private AutoCompleteTextView editAuto;

2)該行添加到onCrate()

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, <your array name here>);

和從onTextChangedTask()刪除此行

ArrayList<String> userNameArrayList = new ArrayList<String>();

3)該行添加到onCrate()

editAuto.setAdapter(adapter);

+0

感謝您的建議。你能告訴我爲什麼要這樣做,我的錯誤在哪裏? – Jerry

+0

但我必須得到數據後doInBackground函數... – Jerry

+0

我認爲這沒用...當我需要設置適配器,適配器可能沒有準備...這是我猜,我會嘗試以後,感謝所有的一些 – Jerry

1

我知道這是這樣晚,但對於那些誰在這裏遇到類似的問題是顯示autoCompleteTextView的降液只要你想,即按下按鈕或onTextChanged。在設置了autoCompleteTextView的ArrayAdapter之後,只需放置以下行。

autoCompleteTextView.showDropDown(); 
相關問題