ANDROID OS - ListView (1)

Ushbu darsimizda ListView bilan ishlashni o'rganib chiqamiz.
Yangi proyekt yaratamiz File ⇒ New ⇒ Android Project

main.xml layoutimizga listview konponentasini tashlaymiz.

<?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"
        >
    <ListView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/listView"                                  android:layout_gravity="right|center_vertical"/>
</LinearLayout>

 
Biz massiv e'lon qilib olamiz, bu bizning listview imiz uchun.
   
public final static String [] programming = {"C", "Java", "C++", "Ruby on Rails",
            "Python", "C#", "Delphi","Objective C",                     "HTML","Java Script","Yii Framework","PHP"};

Dasturning toliq kodi:
package uz.sardordushamov.ListViewTutorial;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class ListViewActivity extends Activity {
    private ListView list;
    public final static String [] prog = {"C", "Java", "C++",
            "Ruby on Rails","Python", "C#", "Delphi","Objective C",
            "HTML","Java Script","Yii Framework","PHP"};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        list = (ListView)findViewById(R.id.listView);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                             android.R.layout.simple_expandable_list_item_1, prog);
        list.setAdapter(adapter);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Toast.makeText(getApplicationContext(),
                        (adapterView.getItemAtPosition(i)).toString(), 3).show();
            }
        });
    }
}

list.setOnItemClickListener()- Bu metod qachonki biz listviewdagi itemlarni bosganimizda ishga tushadi, bu misolda biz listview itemilaridan birontasi bosilganda o'sha bosilgan item ni qiymatini Toast yordamidan ekranga chiqarishni ko'rib chiqdik.



Agar biz android.R.layout.simple_expandable_list_item_1 ni o'zgartirsak:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                             android.R.layout.simple_list_item_single_choice, prog);
main.xml  ni ListView  iga 
android:choiceMode="singleChoice"
qo'shsak
    <ListView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/listView"
            android:layout_gravity="right|center_vertical"
            android:choiceMode="singleChoice"/>
 u holda:
RadioGourp li ListView ga aylanadi va quyidagicha ko'rinishda bo'ladi:

Ko'rib turganimizdek bizning listviewimiz RadioButton li ga aylandi.
Keyingi darslarimizda yana davom ettiramiz ListView bilan ishlashni.

Dastur kodini ko'chirib olish

Popular posts from this blog

Rus tili. Rus tilini o'rganish. Rus tili grammatikasi.(Грамматика русского языка в иллюстрациях.)(djvu). Mylingvo

PHP va MySQL video kursi . HTML, CSS, PHP, MYSQL dan video kurslar.