IMG-LOGO
Trang Chủ Chủ Đề [Video] Tìm hiểu ListView trong Android - Lập trình Android
× 1) [Share Code] Tạo dự án android đầu tay - Hello World - Lập trình Android 2) [Video] Bài giảng Activity, Intent và cách trao đổi dữ liệu qua Activity - Lập trình Android 3. [Video] Tìm hiểu ListView trong Android - Lập trình Android 4) [Video] Hướng dẫn tạo Food App bằng ListView + BaseAdapter - Lập trình android

[Video] Tìm hiểu ListView trong Android - Lập trình Android

by TRẦN VĂN ĐIỆP - 20:03 25/11/2020 1,144 Lượt Xem



package com.gokisoft.c1812l;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class FruitActivity extends AppCompatActivity {
    ListView listView;
    Button btnAdd;
    List<String> dataList = new ArrayList<>();
    ArrayAdapter<String> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fruit);

        listView = findViewById(R.id.af_listview);
        btnAdd = findViewById(R.id.af_add);

        dataList.add("Apple");
        dataList.add("Banana");
        dataList.add("Coconut");


        adapter = new ArrayAdapter<String>(this,
                R.layout.item_fruit, R.id.if_title, dataList);

        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                String value = dataList.get(position);

                Toast.makeText(FruitActivity.this, value, Toast.LENGTH_SHORT).show();
            }
        });

        btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showDialogAdd();
            }
        });
    }

    private void showDialogAdd() {
        LayoutInflater inflater = LayoutInflater.from(this);
        View view = inflater.inflate(R.layout.dialog_add_fruit, null);


//        Toast.makeText(this, "new item", Toast.LENGTH_SHORT).show();
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setView(view);

        final EditText txtTitle = view.findViewById(R.id.daf_title);

        builder.setPositiveButton("Save", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                String title = txtTitle.getText().toString();

                dataList.add(title);

                adapter.notifyDataSetChanged();
            }
        });

        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Toast.makeText(FruitActivity.this, "Cancel", Toast.LENGTH_SHORT).show();
            }
        });

        builder.show();
    }
}



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#000000"
    tools:context="com.gokisoft.c1812l.FruitActivity">

    <Button
        android:id="@+id/af_add"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Add new item"
        android:layout_margin="6dp"/>

    <ListView
        android:id="@+id/af_listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>



<?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">

    <TextView
        android:id="@+id/if_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textColor="#ffffff"
        android:textSize="12pt"
        android:text="TextView" />
</LinearLayout>



<?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">

    <EditText
        android:id="@+id/daf_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPersonName"
        android:hint="Enter new item"
        android:text="" />
</LinearLayout>







Theo dõi cập nhật nội dung học trên Youtube


Bình luận



Chia sẻ từ lớp học

Quick Quizzz!!!

User ImageUser ImageUser Image
User ImageUser ImageUser Image
User ImageUser ImageUser Image
User ImageUser ImageUser Image
User ImageUser ImageUser Image
User Image
User ImageUser ImageUser Image
>> Hiển Thị Quizzz
Đã sao chép!!!