Re bonjour,
j'ai un petit soucis, le programme plante sur la multiautocompletetextview.
Je te met des bouts de code :
le xml :
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="50dip" android:background="@drawable/background_panel_haut">
<ImageView android:id="@+id/btnExplorer"
android:layout_weight="1"
android:src="@drawable/explorer"
android:clickable="true"
android:layout_marginLeft="5dip"
android:layout_height="wrap_content"
android:layout_width="0dip"
android:layout_marginTop="10dip"/>
<MultiAutoCompleteTextView class = "com.android.booztermobile.services.ZOKTextView"
android:id="@+id/searchZone"
android:layout_height="fill_parent"
android:maxLines="1"
android:layout_width="0dip"
android:singleLine="true"
android:textColor="@color/black"
android:layout_weight="5"
android:clickable="true"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:layout_marginTop="5dip"
android:layout_marginBottom="5dip"
android:cursorVisible="true"
android:textSize="15px"/>
<Button
android:id="@+id/btnValid"
android:text="OK"
android:layout_width="0dip"
android:layout_weight="1"
android:background="@drawable/custom_button"
android:textColor="@color/white"
android:layout_height="wrap_content"
android:layout_marginRight="5dip"
android:layout_marginLeft="5dip" android:clickable="true" android:layout_marginTop="5dip"/>
</LinearLayout>
La ZOKTextView = Ma MultiAutoCompleteTextView :
package com.android.booztermobile.services;
import com.android.booztermobile.metier.Suggestion;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.MultiAutoCompleteTextView;
public class ZOKTextView extends MultiAutoCompleteTextView{
public ZOKTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
protected CharSequence convertSelectionToString(Suggestion selectedItem) {
Suggestion suggestions = (Suggestion)selectedItem;
return suggestions.getLabel();
}
}
L'adapter :
package com.android.booztermobile.services;
import java.util.List;
import android.content.Context;
import android.graphics.Typeface;
import android.text.SpannableString;
import android.text.style.StyleSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import com.android.booztermobile.R;
import com.android.booztermobile.metier.Suggestion;
public class ZOKAdapter extends ArrayAdapter<Suggestion>{
private TextView zoneOkePlus;
public ZOKAdapter(Context context, int textViewResourceId, List<Suggestion> suggestions) {
super(context, textViewResourceId,suggestions);
}
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null){
LayoutInflater li = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = li.inflate(R.layout.zone_ok_plus, null);
}
//Create list of suggestions
Suggestion sug = getItem(position);
zoneOkePlus = (TextView)view.findViewById(R.id.ZoneOK);
if (sug != null)
{
SpannableString subjectSpan = new SpannableString(sug.getLabel());
subjectSpan.setSpan(new StyleSpan(Typeface.BOLD),0,sug.getLabel().length(),0);
zoneOkePlus.setText(subjectSpan);
}
return view;
}
}
l'activity ou elle est appelée:
package com.android.booztermobile.activity;
import java.util.ArrayList;
...
public class HeaderMailDisplayActivity extends ListActivity implements OnClickListener, OnKeyListener{
private String uidh = new String();
...
private ZOKTextView searchZone;
private List<Suggestion> suggestions = new ArrayList<Suggestion>();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.headermaillist);
//Declaration of components
searchZone = (ZOKTextView)findViewById(R.id.searchZone);
searchZone.setHint(R.string.lblSearchZone);
searchZone.setThreshold(3);
searchZone.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
posSearchZone =0;
userSearch.add(searchZone.getText().toString().substring(searchZoneLength, searchZone.length()-1));
searchZoneLength = searchZone.length();
}
});
searchZone.addTextChangedListener(new TextWatcher(){
@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 count, int after) {
int lettersCount = searchZone.getThreshold();
if (posSearchZone >= lettersCount-1){
displaySuggest();
posSearchZone++;
}else{
posSearchZone++;
}
}
});
//Les seqnums concerne des mails;
generateListView(seqnums);
}
/**
* Display suggest in ZoneOK+
* When user wrote 3 characters, ZoneOk+ call host to get a list of suggestions and put it into
* the adapter.
* */
public void displaySuggest(){
String userKey = searchZone.getText().toString();
SuggestionServletCall sugCall = new SuggestionServletCall();
try {
ArrayList<BlockSuggestion> blockSuggestionList = sugCall.suggestionCall("en", uidh,null, userKey);
suggestions.clear();
for (int i = 0; i < blockSuggestionList.size(); i++){
ArrayList<Suggestion> liste = new ArrayList<Suggestion>();
for (int j = 0 ; j < blockSuggestionList.get(i).getSuggestions().size(); j++){
suggestions.add(blockSuggestionList.get(i).getSuggestions().get(j));
}
}
searchZone.setAdapter(new ZOKAdapter(this, R.layout.zone_ok_plus, suggestions));
searchZone.setOnKeyListener(this);
searchZone.setTokenizer(new MultiAutoCompleteTextView.Tokenizer() {
@Override
public CharSequence terminateToken(CharSequence text) {
String textStr = text.toString();
CharSequence seq = textStr.endsWith(" ") ? textStr : (textStr + " ");
SpannableString sp = new SpannableString(seq);
// for(int i = 0; i < WHO.length; i++ ){
// if(textStr.equalsIgnoreCase(WHO[i])){
// sp.setSpan(new BackgroundColorSpan(getBaseContext().getResources().getColor(R.color.YELLOW)),0,textStr.length(),0);
// }
// }
// for(int i = 0; i < WHEN.length; i++ ){
// if(textStr.equalsIgnoreCase(WHEN[i])){
// sp.setSpan(new BackgroundColorSpan(getBaseContext().getResources().getColor(R.color.GREEN)),0,textStr.length(),0);
// }
// }
// for(int i = 0; i < WHERE.length; i++ ){
// if(textStr.equalsIgnoreCase(WHERE[i])){
// sp.setSpan(new BackgroundColorSpan(getBaseContext().getResources().getColor(R.color.BLUE)),0,textStr.length(),0);
// }
// }
// for(int i = 0; i < TEXT.length; i++ ){
// if(textStr.equalsIgnoreCase(TEXT[i])){
// sp.setSpan(new BackgroundColorSpan(getBaseContext().getResources().getColor(R.color.grey)),0,textStr.length(),0);
// }
// }
return sp;
}
@Override
public int findTokenStart(CharSequence text, int arg1) {
int pos = text.toString().lastIndexOf(' ', arg1);
return (pos < 0) ? 0 : (pos + 1);
}
@Override
public int findTokenEnd(CharSequence text, int arg1) {
int pos = text.toString().indexOf(' ', arg1);
return (pos < 0) ? (text.length() - 1) : (pos - 1);
}
});
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public boolean onKey(View view, int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_SPACE:
// BlockSuggestion wordSuggest = adapter.getItem(0);
// searchZone.setTextKeepState(wordSuggest.toString());
return true;
}
return false;
}
}
Note: je n'ai pas mis tout le code de l'activity. Je n'ai mis que ce qui touche à la zone MultiAutocompleteTextView ici appelée searchZone
il me jette à la ligne : searchZone = (ZOKTextView)findViewById(R.id.searchZone);
avec comme erreur : java.lang.ClassCastException: android.widget.MultiAutoCompleteTextView
En essayant de déclarer en <ZOKTextView> dans le xml, j'ai une erreur : "Error inflating class"
En déclarant en MultiAutoCompleteTextView dans l'activity, je n'ai pas d'erreur mais il ne prend pas en compte ma liste de suggestion et ne rentre pas dans l'adapter.