WELCOME Abdennour : Software engineer

May 26, 2012

ListView & TextWatcher & AutoComplete


Dernier Tp Android

I/ ListView :
a)Layout XML :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<ListView
android:id="@+id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></ListView>

</LinearLayout>

b)Activity :
public class Tp2Activity extends ListActivity {
static final String[] countries=new String[]{
"Afghastan","Albanie","Tunisie","Ukrania","Tchekozlovakia"
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,countries);
setListAdapter(adapter);


}

}
II/ ListView & TextWatcher :=>Filtrer un listView :



a)Activity :
public class MainActivity extends ListActivity {
private EditText txtInput;
static final String[] countries = new String[] { "Afghastan", "Albanie",
"Tunisie", "Ukrania", "Tchekozlovakia" };

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setContentView(R.layout.main);
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, countries);
setListAdapter(adapter);
txtInput = (EditText) findViewById(R.id.editText1);
txtInput.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable theWatchedText) {

}

public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
// Toast.makeText(getApplicationContext(), "BTC " + arg0,
// 1).show();
}

public void onTextChanged(CharSequence arg0, int start, int count,
int after) {
adapter.getFilter().filter(arg0.toString());

// Toast.makeText(getApplicationContext(), "OTC " + arg0,
// 1).show();
}
}); // addTextChangedListener
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {

public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getApplicationContext(),
((TextView) view).getText(), Toast.LENGTH_SHORT).show();

}

});
/* AutoCompleteTextView textView=(AutoCompleteTextView)findViewById(R.id.autocomplete_country);
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, R.layout.list_item, countries);
textView.setAdapter(adapter);*/
}
}
b)Xml Layout :
b.1=>list_item.xml :
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textColor="#000"
android:textSize="16sp" >

</TextView>
b.2=>main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >


<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/hint1" >

<requestFocus />
</EditText>

<ListView
android:id="@+id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>

</LinearLayout>
d
III/AutoComplete :


1)Activity :
public class MainActivity extends Activity {

static final String[] countries = new String[] { "Afghastan", "Albanie",
"Tunisie", "Ukrania", "Tchekozlovakia" };
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
AutoCompleteTextView textView=(AutoCompleteTextView)findViewById(R.id.autocomplete_country);
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, R.layout.list_item, countries);
textView.setAdapter(adapter);
}
}


2)LAyout :
=>list_item.xml =>Meme que l'app précédente .

=>main.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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="country" />

<AutoCompleteTextView
android:id="@+id/autocomplete_country"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp" />
</LinearLayout>

3 comments: