2016-09-29 112 views
0

今天我有一个具体的问题,我在谷歌搜索,但找不到任何可以帮助的答案。AutoCompleteTextView OnItemClickListener不起作用

我的活动有一个AsyncTask从数据库加载一些字符串并在autocompletetextview中显示它们。我使用HttpUrlConnection获取字符串,然后使用json-simple库解析它们,然后在autocompletetextview中显示它们。下面是ACTV)全码这是onPostExecute(:

ArrayList<Object> result = new ArrayList<>(); 
for (Shift_OneTime sot : shifts) { 
    result.add(sot.getName() + " " + sot.getDay() + " " + sot.getMonth() + " " + sot.getYear()); 
    } 
    for (Employee e : emps) { 
     result.add(e.toString()); 
    } 

    acs = (AutoCompleteTextView) findViewById(R.id.autocompletesearch); 

    ArrayAdapter<Object> adapter = new ArrayAdapter<>(MainScreen.this, android.R.layout.simple_list_item_1, result); 
    acs.setAdapter(adapter); 
    acs.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 
       acs.setText(""); 
       acs.clearFocus(); 
       Object o = adapterView.getItemAtPosition(i); 
       if (o instanceof Employee) { 
        Intent n = new Intent(MainScreen.this, EmployeeDetailsActivity.class); 
        n.putExtra("employeeid", ((Employee) o).getId()); 
        startActivity(n); 
       } else if (o instanceof Shift_OneTime) { 
        Intent n = new Intent(MainScreen.this, ShiftDetailsActivity.class); 
        n.putExtra("shiftid", ((Shift_OneTime) o).getId()); 
        startActivity(n); 
       } 
      } 
     }); 

即时得到这一堆警告:当我点击字符串输入一些内容后显示出来

W/IInputConnectionWrapper: getSelectedText on inactive InputConnection 
W/IInputConnectionWrapper: requestCursorAnchorInfo on inactive InputConnection 
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection 
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection 
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection 
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection 
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection 
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection 

这些错误出现autocompletetextview。现在我的观察并不确定,但是上次它正在使用旧的DefaultHTTPClient或者现在不赞成使用的东西。这就是我改为HttpURLConnection的原因。在更改之后,我还将ACTV移动到了AsyncTask中,就像Im从我的php webservice中读取字符串一样,并且我还添加了ActionBarDrawerToggle并覆盖其方法,以便在打开导航抽屉时隐藏软键盘。

我试图删除扩展切换,我也试图把onItemClickListener()放回到AsyncTask之外,但它没有帮助。我不能真正回到现在已弃用的HTTP客户端,所以我想知道是否有人有任何想法...非常感谢

回答

0

您需要使用setOnClickListener();

+0

不,setOnClickListener会在您点击ACTV时作出反应。这不是我想 –

+0

你错过了,而且只是为了确保你得到正确的东西 – Catluc

+0

我以前在那里,没有任何ifs被处理,但那还是 –

0

我没有注意到的问题是Object o = adapterView.getItemAtPosition(i);正在获取String类型的对象(注意我填充'result'列表的方式),既不是Employee对象也不是Shift_OneTime。所以当我检查'o instanceof XXX'时,它既不是一个也不是其他的。因此,与其填充字符串列表,我不得不插入对象本身,FE: 前:

result.add(sot.getName() + ....); 

后:

result.add(e); 

在ACTV字符的搜索中的toString使用值完成()方法在Employee和Shift_OneTime类中。

希望这将有助于未来的人。

0

我已经尝试了很多东西,我发现这个解决方案

我这样做解决方案时Onclicklistener没有在我的代码

在getView把Onclicklistener在您的自定义适配器()工作。

@Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     final Customer customer = getItem(position); 
     if (convertView == null) { 
      convertView = LayoutInflater.from(getContext()).inflate(R.layout.customer_row, parent, false); 
     } 
     convertView.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Toast.makeText(yourContext, "text clicked", Toast.LENGTH_SHORT).show(); 
      } 
     }); 

//....do your stuff here 

     return convertView; 
    } 
相关问题