Guruh Abdug’aniyev Ikrom


Download 43.27 Kb.
Sana24.12.2022
Hajmi43.27 Kb.
#1059863
Bog'liq
tTBBWUebCpGXsGjJXN6X12OAhpYG9esP (1)


103-19 guruh Abdug’aniyev Ikrom

ListView-ga ma'lumotlarni qanday yuklash, uni ma'lumotlar manbai bilan bog'lash mumkinligi ko'rib chiqildi. Lekin oddiygina elementlar ro'yxatini ko'rsatishdan tashqari, ListView elementni tanlash va uning tanlovini qayta ishlash imkonini beradi. Keling, buni qanday qilishni ko'rib chiqaylik. Activity_main.xml faylida quyidagi belgini aniqlang:



xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:id="@+id/selection"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="22sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
android:id="@+id/countriesList"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="@+id/selection"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent">


Endi biz ListView ro'yxatini ma'lumotlar manbasiga bog'laymiz va ro'yxat elementida unga bosish tinglovchisini tayinlaymiz:


package com.example.listapp;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
String[] countries = { "Бразилия", "Аргентина", "Колумбия", "Чили", "Уругвай"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// получаем элемент TextView
TextView selection = findViewById(R.id.selection);
// получаем элемент ListView
ListView countriesList = findViewById(R.id.countriesList);
// создаем адаптер
ArrayAdapter adapter = new ArrayAdapter(this,
android.R.layout.simple_list_item_1, countries);
// устанавливаем для списка адаптер
countriesList.setAdapter(adapter);
// добавляем для списка слушатель
countriesList.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView parent, View v, int position, long id)
{
// по позиции получаем выбранный элемент
String selectedItem = countries[position];
// установка текста элемента TextView
selection.setText(selectedItem);
}
});
}
}

Shunday qilib, setAdapter usuli ListView elementini ma'lum bir adapterga bog'laydi. Keyinchalik, OnItemClickListener tinglovchisi ro'yxat elementini tanlashni boshqarish uchun o'rnatiladi. Ushbu tinglovchida bitta onItemClick usuli mavjud, uning parametrlari orqali biz tanlangan elementni va tegishli ma'lumotlarni olishimiz mumkin. Shunday qilib, u quyidagi parametrlarni oladi:


ota-ona: bosilgan AdapterView (bu holda bizning ListView)


view : AdapterView ichidagi bosilgan vidjet


joy: AdapterView ichidagi bosilgan vidjet indeksi


id: bosilgan elementning qator identifikatori


Ushbu variantlardan foydalanib, biz tanlangan elementni turli yo'llar bilan olishimiz mumkin.


Misol uchun, bu holda, satrlar massividagi element indeksiga mos keladigan bosilgan vidjet indeksini olish orqali biz satrlar massiviga mos keladigan elementni o'rnatishimiz va shu bilan uning matnini olishimiz mumkin:


countriesList.setOnItemClickListener(new AdapterView.OnItemClickListener(){


@Override
public void onItemClick(AdapterView parent, View v, int position, long id)
{
// по позиции получаем выбранный элемент
String selectedItem = countries[position];
// установка текста элемента TextView
selection.setText(selectedItem);
}
});

Tanlangan elementni AdapterView dan ham olishimiz mumkin, u birinchi parametr - AdapterView ota-ona sifatida uzatiladi. Shunday qilib, bu holda, biz AdapterView-dagi har bir element aslida satr yoki String ob'ektini ifodalashini bilamiz, shuning uchun bu holda tanlangan elementni quyidagicha olishimiz mumkin:
countriesList.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView parent, View v, int position, long id)
{
// получаем выбранный элемент
String selectedItem = (String)parent.getItemAtPosition(position);
// установка текста элемента TextView
selection.setText(selectedItem);
}
});
getItemAtPosition usuli tanlangan elementni indeks bo'yicha qaytaradi. Agar ma'lumotlar manbai sifatida Java kodida yaratilgan qatorlar qatoridan emas, balki, masalan, xml faylida aniqlangan resursidan foydalansak, bu muhim bo'lishi mumkin.
Uchinchidan, biz ikkinchi parametr sifatida uzatiladigan tanlangan elementdan foydalanishimiz mumkin - View v. Shunday qilib, bu holda, adapter tartib turi sifatida - android.R.layout.simple_list_item_1 resursidan foydalanadi, ya'ni tanlangan element berilgan matn ko'rsatiladigan TextView elementini ifodalaydi. Shuning uchun, bu holda biz tanlangan elementni quyidagi kabi olishimiz mumkin:
countriesList.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView parent, View v, int position, long id)
{
// получаем выбранный элемент
TextView textView = (TextView) v;
String selectedItem = (String)textView.getText();
// установка текста элемента TextView
selection.setText(selectedItem);
// или так
// selection.setText(textView.getText());
}
});
Endi MainActivity kodida ro'yxat elementlarini tanlash bilan ishlashni aniqlaymiz:
package com.example.listapp;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.SparseBooleanArray;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
String[] countries = { "Бразилия", "Аргентина", "Колумбия", "Чили", "Уругвай"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// получаем элемент TextView
TextView selection = findViewById(R.id.selection);
// получаем элемент ListView
ListView countriesList = findViewById(R.id.countriesList);
// создаем адаптер
// создаем адаптер
ArrayAdapter adapter = new ArrayAdapter(this,
android.R.layout.simple_list_item_multiple_choice, countries);
// устанавливаем для списка адаптер
countriesList.setAdapter(adapter);
// добавляем для списка слушатель
countriesList.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView parent, View v, int position, long id)
{
SparseBooleanArray selected=countriesList.getCheckedItemPositions();
String selectedItems="";
for(int i=0;i < countries.length;i++)
{
if(selected.get(i))
selectedItems+=countries[i]+",";
}
// установка текста элемента TextView
selection.setText("Выбрано: " + selectedItems);
}
});
}
}
android.R.layout.simple_list_item_multiple_choice resursi bir nechta tanlov ro'yxatini yaratish uchun ramka tomonidan taqdim etilgan standart belgilashni ifodalaydi.

Va elementlarni tanlashda biz SparseBooleanArray ob'ektidagi barcha tanlangan pozitsiyalarni olamiz, so'ngra butun massiv bo'ylab takrorlaymiz va agar massivdagi elementning o'rni SparseBooleanArrayda bo'lsa, ya'ni belgilangan bo'lsa, biz belgilanganlarni qo'shamiz. satrga element.



Download 43.27 Kb.

Do'stlaringiz bilan baham:




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling