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:

  1. Wow. This really made my day. Thanks a lot!
    JQuery Training JQuery Training Javascript JQuery Online Training

    JQuery Online Training JQuery Online Training Javascript Online Training JQuery Training

    JavaScript Training Courses

    ReplyDelete
  2. Congratulations guys, quality information you have given!!!..Its really useful blog. Thanks for sharing this useful information
    Best Android Training in Velachery | android development course fees in chennai

    ReplyDelete
  3. This information is impressive; I am inspired with your post writing style & how continuously you describe this topic. After reading your post, thanks for taking the time to discuss this, I feel happy about it and I love learning more about this topic.Android Training institute in chennai with placement | Best Android Training in velachery

    ReplyDelete