جلسه چهاردهم لیست ساده
دوشنبه, ۱ ارديبهشت ۱۳۹۹، ۰۱:۵۲ ب.ظ
package ir.ben.frog; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.Adapter; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListAdapter; import android.widget.ListView; import android.widget.Toast; import java.util.ArrayList; import java.util.List; public class SimpleList extends AppCompatActivity { ArrayAdapter adapter; ListView lv; List<String> names; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_simple_list); lv= findViewById(R.id.listv); lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(SimpleList.this, names.get(position)+" is here", Toast.LENGTH_SHORT).show(); } }); names=new ArrayList<>(); names.add("Hi"); names.add("hello"); names.add("jhjhk"); names.add("hjhjkh"); names.add("xcxc"); names.add("poppo"); names.add("qwqeq"); names.add("xdygy"); names.add("bhnhm"); names.add("kmklkm"); names.add("tytyt"); names.add("iouiui"); names.add("asasas"); adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,names) /*{ @NonNull @Override public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { return super.getView(position, convertView, parent); } }*/ ; lv.setAdapter(adapter); refresh(); } private void refresh() { adapter.notifyDataSetChanged(); } @Override public boolean onCreateOptionsMenu(Menu menu) { menu.add("Add").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { names.add("Heyyy"); adapter.notifyDataSetChanged(); return false; } }); menu.add("Remove").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { if(names.isEmpty()) return false; names.remove(names.size()-1); adapter.notifyDataSetChanged(); return false; } }); return super.onCreateOptionsMenu(menu); } }
۹۹/۰۲/۰۱