0% found this document useful (0 votes)
7 views4 pages

Bai 1

Uploaded by

phongle777ab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views4 pages

Bai 1

Uploaded by

phongle777ab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

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"
android:padding="16dp">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="type text:" />

<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter text here" />

<Button
android:id="@+id/addButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Please Click Me!" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="List of value:" />

<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp" />

</LinearLayout>

JAVA
package com.example.listview;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
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;

public class MainActivity extends AppCompatActivity {

private EditText editText;


private Button addButton;
private ListView listView;
private ArrayList<String> itemList;
private ArrayAdapter<String> adapter;

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

// Ánh xạ các view


editText = findViewById(R.id.editText);
addButton = findViewById(R.id.addButton);
listView = findViewById(R.id.listView);

// Khởi tạo danh sách và adapter


itemList = new ArrayList<>();
adapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, itemList);
listView.setAdapter(adapter);

// Xử lý sự kiện khi nhấn nút thêm


addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String text = editText.getText().toString().trim();
if (!text.isEmpty()) {
itemList.add(text);
adapter.notifyDataSetChanged();
editText.setText("");
} else {
Toast.makeText(MainActivity.this, "Please enter some text",
Toast.LENGTH_SHORT).show();
}
}
});

// Xử lý sự kiện khi chọn một item trong ListView


listView.setOnItemClickListener(new
AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int
position, long id) {
String selectedItem = itemList.get(position);
Toast.makeText(MainActivity.this, "Selected: " + selectedItem,
Toast.LENGTH_SHORT).show();
}
});
}
}

You might also like