Wednesday, February 20, 2019

Lesson 4– ListView

Right click to class name, New> Class, name it forth, finish.
Edit code to make it like this
public class forth extends Activity {
         
          @Override
          protected void onCreate(Bundle savedInstanceState) {
               super.onCreate(savedInstanceState);
               setContentView(R.layout.ac4);

}
}
Ignore red mark at ac4, we create it now.
Right click to folder layout, New > Android XML File, name it ac4, finish.
Copy in to file ac4.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
   
<ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="2dp"       
        android:divider="#ff8f40"
        android:dividerHeight="1px"
        android:lineSpacingExtra="-8dp"
        android:singleLine="true"
        android:textColor="#00F" >
    </ListView>
     <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="5dp"
        android:text="This is ListView"
        android:textColor="#800000"
        android:textSize="20sp" />
</LinearLayout>
Double click file AndroidManifest.xml to declare class forth, exactly it ‘s name.

Back to class forth.
First declare a ListView, array and adapter, find address.
String[] fru = { "Pear","Banana", "Cashew", "Orange", "Water melon", "Peach", "Grape", "Mango", "Plum" };
     private ArrayAdapter<String> adapter;
Screen now like this.

Add mode choice for List.
lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
Set adapter to show list.
          adapter = new ArrayAdapter<String>(forth.this,
                     android.R.layout.simple_list_item_checked, fru);
               lv.setOnItemClickListener(new OnItemClickListener() {

                     @Override
          public void onItemClick(AdapterView<?> arg0, View arg1,
                               int arg2, long arg3) {
                         

                     }
               });
               lv.setAdapter(adapter);
Press Ctrl+Shift+O to import library. Class forth now look like this.

Back to MainActivity change code in Next button to.
Intent in = new Intent(MainActivity.this,forth.class);                 
startActivity(in);
Run, press next, screen look like this.

Check to list to see green checkmark.
Add this in to setOnItemClickListener, we will show a toast when click list row.
int row = arg2;
Toast.makeText(forth.this, "You press the line "+row,
                                    Toast.LENGTH_SHORT).show();

Run and press first row, it show line 0, the first element of array, to see true index line, we add row=arg2+1.

In real app, we can do anything when click list row, for example, open a new screen.
To remove app name on top screen, add to above setContentView(R.layout.ac4);
requestWindowFeature(Window.FEATURE_NO_TITLE);
Import library, run to see result.
Back to file ac4.xml, let look at android:textColor="#00F"

We color string to blue but why it still black? Because you can’t color string listview in file xml. We must use another way, see in topic about Listview.

Now you can go to make android app and code a real app.

No comments:

Post a Comment