Note that there are some explanatory texts on larger screens.

plurals
  1. PODelete item and get it's title for TOAST message or change the name of an item custom adapter
    text
    copied!<p>I have a custom adapter which I got from the internet. It has recipes in it with image and a title for each one. I have looked for my specific case all over google and this site but haven't found exactly what I needed. The info I did find was to complicated for me to adjust for my own case problem.</p> <p>Actually it supposed to be a simple task for anyone above beginner level, but unfortunately I am a beginner.</p> <ul> <li>I need to have the option to delete item from the list</li> <li>I need a toast message to display its name after deleting it</li> <li>I need to be able to change the title of an item.</li> </ul> <p>When it was an ArrayAdapter it was easy because I didn't have the image variable in the Adapter, but now all the functions to find title,change name and delete item are different.</p> <pre><code>public class MainActivity extends Activity { private ListView listView1; public String mName; View btnAdd; String mTitle; Cursor mCursor; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnAdd = this.findViewById(R.id.btnAdd); Recipe recipe_data[] = new Recipe[] { new Recipe(R.drawable.cherry_pie, "Cherry Pie"), new Recipe(R.drawable.chocchip_cookie, "Cocolate Chips Cookies"), new Recipe(R.drawable.quaker_cookie, "Quaker Cookies"), new Recipe(R.drawable.short_pastry, "Short Pastry"), new Recipe(R.drawable.flour, "Will be added in the future...") }; RecipeAdapter adapter = new RecipeAdapter(this, R.layout.listview_item_row, recipe_data); listView1 = (ListView)findViewById(R.id.listView1); View header = (View)getLayoutInflater().inflate(R.layout.listview_header_row, null); listView1.addHeaderView(header); listView1.setAdapter(adapter); OnClickListener listen = new OnClickListener() { @Override public void onClick(View v) { EditText edit = (EditText) findViewById(R.id.txtItem); //we get the text into a global string mName = ((TextView)edit).getText().toString(); //we reverse the edit var back to be empty edit.setText(""); //this.notifyDataSetChanged(); } }; btnAdd.setOnClickListener(listen); registerForContextMenu(listView1); } @Override public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); menu.setHeaderTitle("Context Menu"); menu.add(0, v.getId(), 0, "Change Name"); menu.add(0, v.getId(), 0, "Delete"); } @Override public boolean onContextItemSelected(MenuItem item) { AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); if(item.getTitle()=="Change Name"){changeN(item.getItemId());} else if(item.getTitle()=="Delete"){ Toast.makeText(this, "Item " + mTitle + " has been removed..", Toast.LENGTH_SHORT).show(); } else {return false;} return true; } //Change item name function public void changeN(int id){ } } public class RecipeAdapter extends ArrayAdapter&lt;Recipe&gt;{ Context context; int layoutResourceId; Recipe data[] = null; public RecipeAdapter(Context context, int layoutResourceId, Recipe[] data) { super(context, layoutResourceId, data); this.layoutResourceId = layoutResourceId; this.context = context; this.data = data; } @Override public View getView(int position, View convertView, ViewGroup parent) { View row = convertView; RecipeHolder holder = null; if(row == null) { LayoutInflater inflater = ((Activity)context).getLayoutInflater(); row = inflater.inflate(layoutResourceId, parent, false); holder = new RecipeHolder(); holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon); holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle); row.setTag(holder); } else { holder = (RecipeHolder)row.getTag(); } Recipe recipe = data[position]; holder.txtTitle.setText(recipe.title); holder.imgIcon.setImageResource(recipe.icon); return row; } static class RecipeHolder { ImageView imgIcon; TextView txtTitle; } } public class Recipe { public int icon; public String title; public Recipe(){ super(); } public Recipe(int icon, String title) { super(); this.icon = icon; this.title = title; } @Override public String toString() { return this.title; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload