How to create custom toast message for different item in a list in a custom adapter










0















So I have a program where I need to display a different toast depending on what item the user selects in a list. I created a custom adapter since I need to add pictures to the list and created an object that goes by the name Day that contains the day of the week, the image, and the custom text that needs to go in the toast. My problem is that I don't know how to create an onItemClickListener that will use my custom text for my toast.
This is my custom adapter



public class DayAdapter extends ArrayAdapter<Day> {


private Context mContext;
int mResource;



public DayAdapter(@NonNull Context context, int resource, @NonNull ArrayList<Day> objects) 
super(context, resource, objects);
mContext = context;
mResource = resource;




@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent)
String jour = getItem(position).getDay();
int image = getItem(position).getImage();
String message = getItem(position).getMessage();

Day journee = new Day(jour, image, message);

LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(mResource, parent, false);

TextView tvJour = (TextView) convertView.findViewById(R.id.textViewJour);
ImageView tvImage = (ImageView) convertView.findViewById(R.id.imageViewJour);


tvJour.setText(jour);
tvImage.setImageResource(image);


return convertView;



This is my MainActivity



public class MainActivity extends AppCompatActivity 

ListView liste;

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

liste = (ListView) findViewById(R.id.dayList);
ArrayList<Day> semaine = new ArrayList<Day>();
Day lundi = new Day("Lundi", R.drawable.happyface, "Prog lundi");
Day mardi = new Day("Mardi", R.drawable.happyface, "Musique mardi");
Day mercredi = new Day("Mercredi", R.drawable.happyface, "Mercredi");
Day jeudi = new Day("Jeudi", R.drawable.beer, "Work jeudi");
Day vendredi = new Day("Vendredi", R.drawable.beer, "also work vendredi");
Day samedi = new Day("Samedi", R.drawable.malade, "also also work samedi");
Day dimanche = new Day("Dimanche", R.drawable.etude, "Love Dimanche");

semaine.add(lundi);
semaine.add(mardi);
semaine.add(mercredi);
semaine.add(jeudi);
semaine.add(vendredi);
semaine.add(samedi);
semaine.add(dimanche);

DayAdapter adapter = new DayAdapter (this, R.layout.row, semaine);

liste.setAdapter(adapter);

liste.setOnItemClickListener(new AdapterView.OnItemClickListener()
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l)
String messa = adapterView.getAdapter().getItem(i).toString();
Toast.makeText(MainActivity.this, messa, Toast.LENGTH_LONG).show();

);












share|improve this question






















  • In your ArrayAdapter class set the onItemClickListener on the item that you want to be clicked. Then inside, you can create a Toast.

    – Ishaan Javali
    Nov 15 '18 at 15:02











  • What item in your listView needs to be clicked for the Toast?

    – Ishaan Javali
    Nov 15 '18 at 15:03











  • I need to click the whole item(I want the toast to pop up when I click on the item of the list)

    – Alex
    Nov 15 '18 at 15:05











  • Is your app crashin or what?

    – Alesandro Giordano
    Nov 15 '18 at 15:05











  • My app is not crashing, all I am getting when I click the Item is a directory, it doesn't display my custom toast

    – Alex
    Nov 15 '18 at 15:06















0















So I have a program where I need to display a different toast depending on what item the user selects in a list. I created a custom adapter since I need to add pictures to the list and created an object that goes by the name Day that contains the day of the week, the image, and the custom text that needs to go in the toast. My problem is that I don't know how to create an onItemClickListener that will use my custom text for my toast.
This is my custom adapter



public class DayAdapter extends ArrayAdapter<Day> {


private Context mContext;
int mResource;



public DayAdapter(@NonNull Context context, int resource, @NonNull ArrayList<Day> objects) 
super(context, resource, objects);
mContext = context;
mResource = resource;




@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent)
String jour = getItem(position).getDay();
int image = getItem(position).getImage();
String message = getItem(position).getMessage();

Day journee = new Day(jour, image, message);

LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(mResource, parent, false);

TextView tvJour = (TextView) convertView.findViewById(R.id.textViewJour);
ImageView tvImage = (ImageView) convertView.findViewById(R.id.imageViewJour);


tvJour.setText(jour);
tvImage.setImageResource(image);


return convertView;



This is my MainActivity



public class MainActivity extends AppCompatActivity 

ListView liste;

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

liste = (ListView) findViewById(R.id.dayList);
ArrayList<Day> semaine = new ArrayList<Day>();
Day lundi = new Day("Lundi", R.drawable.happyface, "Prog lundi");
Day mardi = new Day("Mardi", R.drawable.happyface, "Musique mardi");
Day mercredi = new Day("Mercredi", R.drawable.happyface, "Mercredi");
Day jeudi = new Day("Jeudi", R.drawable.beer, "Work jeudi");
Day vendredi = new Day("Vendredi", R.drawable.beer, "also work vendredi");
Day samedi = new Day("Samedi", R.drawable.malade, "also also work samedi");
Day dimanche = new Day("Dimanche", R.drawable.etude, "Love Dimanche");

semaine.add(lundi);
semaine.add(mardi);
semaine.add(mercredi);
semaine.add(jeudi);
semaine.add(vendredi);
semaine.add(samedi);
semaine.add(dimanche);

DayAdapter adapter = new DayAdapter (this, R.layout.row, semaine);

liste.setAdapter(adapter);

liste.setOnItemClickListener(new AdapterView.OnItemClickListener()
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l)
String messa = adapterView.getAdapter().getItem(i).toString();
Toast.makeText(MainActivity.this, messa, Toast.LENGTH_LONG).show();

);












share|improve this question






















  • In your ArrayAdapter class set the onItemClickListener on the item that you want to be clicked. Then inside, you can create a Toast.

    – Ishaan Javali
    Nov 15 '18 at 15:02











  • What item in your listView needs to be clicked for the Toast?

    – Ishaan Javali
    Nov 15 '18 at 15:03











  • I need to click the whole item(I want the toast to pop up when I click on the item of the list)

    – Alex
    Nov 15 '18 at 15:05











  • Is your app crashin or what?

    – Alesandro Giordano
    Nov 15 '18 at 15:05











  • My app is not crashing, all I am getting when I click the Item is a directory, it doesn't display my custom toast

    – Alex
    Nov 15 '18 at 15:06













0












0








0








So I have a program where I need to display a different toast depending on what item the user selects in a list. I created a custom adapter since I need to add pictures to the list and created an object that goes by the name Day that contains the day of the week, the image, and the custom text that needs to go in the toast. My problem is that I don't know how to create an onItemClickListener that will use my custom text for my toast.
This is my custom adapter



public class DayAdapter extends ArrayAdapter<Day> {


private Context mContext;
int mResource;



public DayAdapter(@NonNull Context context, int resource, @NonNull ArrayList<Day> objects) 
super(context, resource, objects);
mContext = context;
mResource = resource;




@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent)
String jour = getItem(position).getDay();
int image = getItem(position).getImage();
String message = getItem(position).getMessage();

Day journee = new Day(jour, image, message);

LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(mResource, parent, false);

TextView tvJour = (TextView) convertView.findViewById(R.id.textViewJour);
ImageView tvImage = (ImageView) convertView.findViewById(R.id.imageViewJour);


tvJour.setText(jour);
tvImage.setImageResource(image);


return convertView;



This is my MainActivity



public class MainActivity extends AppCompatActivity 

ListView liste;

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

liste = (ListView) findViewById(R.id.dayList);
ArrayList<Day> semaine = new ArrayList<Day>();
Day lundi = new Day("Lundi", R.drawable.happyface, "Prog lundi");
Day mardi = new Day("Mardi", R.drawable.happyface, "Musique mardi");
Day mercredi = new Day("Mercredi", R.drawable.happyface, "Mercredi");
Day jeudi = new Day("Jeudi", R.drawable.beer, "Work jeudi");
Day vendredi = new Day("Vendredi", R.drawable.beer, "also work vendredi");
Day samedi = new Day("Samedi", R.drawable.malade, "also also work samedi");
Day dimanche = new Day("Dimanche", R.drawable.etude, "Love Dimanche");

semaine.add(lundi);
semaine.add(mardi);
semaine.add(mercredi);
semaine.add(jeudi);
semaine.add(vendredi);
semaine.add(samedi);
semaine.add(dimanche);

DayAdapter adapter = new DayAdapter (this, R.layout.row, semaine);

liste.setAdapter(adapter);

liste.setOnItemClickListener(new AdapterView.OnItemClickListener()
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l)
String messa = adapterView.getAdapter().getItem(i).toString();
Toast.makeText(MainActivity.this, messa, Toast.LENGTH_LONG).show();

);












share|improve this question














So I have a program where I need to display a different toast depending on what item the user selects in a list. I created a custom adapter since I need to add pictures to the list and created an object that goes by the name Day that contains the day of the week, the image, and the custom text that needs to go in the toast. My problem is that I don't know how to create an onItemClickListener that will use my custom text for my toast.
This is my custom adapter



public class DayAdapter extends ArrayAdapter<Day> {


private Context mContext;
int mResource;



public DayAdapter(@NonNull Context context, int resource, @NonNull ArrayList<Day> objects) 
super(context, resource, objects);
mContext = context;
mResource = resource;




@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent)
String jour = getItem(position).getDay();
int image = getItem(position).getImage();
String message = getItem(position).getMessage();

Day journee = new Day(jour, image, message);

LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(mResource, parent, false);

TextView tvJour = (TextView) convertView.findViewById(R.id.textViewJour);
ImageView tvImage = (ImageView) convertView.findViewById(R.id.imageViewJour);


tvJour.setText(jour);
tvImage.setImageResource(image);


return convertView;



This is my MainActivity



public class MainActivity extends AppCompatActivity 

ListView liste;

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

liste = (ListView) findViewById(R.id.dayList);
ArrayList<Day> semaine = new ArrayList<Day>();
Day lundi = new Day("Lundi", R.drawable.happyface, "Prog lundi");
Day mardi = new Day("Mardi", R.drawable.happyface, "Musique mardi");
Day mercredi = new Day("Mercredi", R.drawable.happyface, "Mercredi");
Day jeudi = new Day("Jeudi", R.drawable.beer, "Work jeudi");
Day vendredi = new Day("Vendredi", R.drawable.beer, "also work vendredi");
Day samedi = new Day("Samedi", R.drawable.malade, "also also work samedi");
Day dimanche = new Day("Dimanche", R.drawable.etude, "Love Dimanche");

semaine.add(lundi);
semaine.add(mardi);
semaine.add(mercredi);
semaine.add(jeudi);
semaine.add(vendredi);
semaine.add(samedi);
semaine.add(dimanche);

DayAdapter adapter = new DayAdapter (this, R.layout.row, semaine);

liste.setAdapter(adapter);

liste.setOnItemClickListener(new AdapterView.OnItemClickListener()
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l)
String messa = adapterView.getAdapter().getItem(i).toString();
Toast.makeText(MainActivity.this, messa, Toast.LENGTH_LONG).show();

);









android






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 '18 at 14:59









AlexAlex

32




32












  • In your ArrayAdapter class set the onItemClickListener on the item that you want to be clicked. Then inside, you can create a Toast.

    – Ishaan Javali
    Nov 15 '18 at 15:02











  • What item in your listView needs to be clicked for the Toast?

    – Ishaan Javali
    Nov 15 '18 at 15:03











  • I need to click the whole item(I want the toast to pop up when I click on the item of the list)

    – Alex
    Nov 15 '18 at 15:05











  • Is your app crashin or what?

    – Alesandro Giordano
    Nov 15 '18 at 15:05











  • My app is not crashing, all I am getting when I click the Item is a directory, it doesn't display my custom toast

    – Alex
    Nov 15 '18 at 15:06

















  • In your ArrayAdapter class set the onItemClickListener on the item that you want to be clicked. Then inside, you can create a Toast.

    – Ishaan Javali
    Nov 15 '18 at 15:02











  • What item in your listView needs to be clicked for the Toast?

    – Ishaan Javali
    Nov 15 '18 at 15:03











  • I need to click the whole item(I want the toast to pop up when I click on the item of the list)

    – Alex
    Nov 15 '18 at 15:05











  • Is your app crashin or what?

    – Alesandro Giordano
    Nov 15 '18 at 15:05











  • My app is not crashing, all I am getting when I click the Item is a directory, it doesn't display my custom toast

    – Alex
    Nov 15 '18 at 15:06
















In your ArrayAdapter class set the onItemClickListener on the item that you want to be clicked. Then inside, you can create a Toast.

– Ishaan Javali
Nov 15 '18 at 15:02





In your ArrayAdapter class set the onItemClickListener on the item that you want to be clicked. Then inside, you can create a Toast.

– Ishaan Javali
Nov 15 '18 at 15:02













What item in your listView needs to be clicked for the Toast?

– Ishaan Javali
Nov 15 '18 at 15:03





What item in your listView needs to be clicked for the Toast?

– Ishaan Javali
Nov 15 '18 at 15:03













I need to click the whole item(I want the toast to pop up when I click on the item of the list)

– Alex
Nov 15 '18 at 15:05





I need to click the whole item(I want the toast to pop up when I click on the item of the list)

– Alex
Nov 15 '18 at 15:05













Is your app crashin or what?

– Alesandro Giordano
Nov 15 '18 at 15:05





Is your app crashin or what?

– Alesandro Giordano
Nov 15 '18 at 15:05













My app is not crashing, all I am getting when I click the Item is a directory, it doesn't display my custom toast

– Alex
Nov 15 '18 at 15:06





My app is not crashing, all I am getting when I click the Item is a directory, it doesn't display my custom toast

– Alex
Nov 15 '18 at 15:06












1 Answer
1






active

oldest

votes


















0














Try making the Toast in your ArrayAdapter class, specifically in the getView method like



convertview.setOnClickListener(new OnClickListener() 
@Override
public void onClick(View view)
Toast.makeText(mContext, "Custom Toast", Toast.LENGTH_SHORT).show();



);


If you want, you can use an onTouchListener instead of the onClickListener.






share|improve this answer























  • This fixed it, thank you very much.

    – Alex
    Nov 15 '18 at 15:16











  • You are most welcome Alex. I am happy that I was able to help!

    – Ishaan Javali
    Nov 15 '18 at 15:16










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53322232%2fhow-to-create-custom-toast-message-for-different-item-in-a-list-in-a-custom-adap%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Try making the Toast in your ArrayAdapter class, specifically in the getView method like



convertview.setOnClickListener(new OnClickListener() 
@Override
public void onClick(View view)
Toast.makeText(mContext, "Custom Toast", Toast.LENGTH_SHORT).show();



);


If you want, you can use an onTouchListener instead of the onClickListener.






share|improve this answer























  • This fixed it, thank you very much.

    – Alex
    Nov 15 '18 at 15:16











  • You are most welcome Alex. I am happy that I was able to help!

    – Ishaan Javali
    Nov 15 '18 at 15:16















0














Try making the Toast in your ArrayAdapter class, specifically in the getView method like



convertview.setOnClickListener(new OnClickListener() 
@Override
public void onClick(View view)
Toast.makeText(mContext, "Custom Toast", Toast.LENGTH_SHORT).show();



);


If you want, you can use an onTouchListener instead of the onClickListener.






share|improve this answer























  • This fixed it, thank you very much.

    – Alex
    Nov 15 '18 at 15:16











  • You are most welcome Alex. I am happy that I was able to help!

    – Ishaan Javali
    Nov 15 '18 at 15:16













0












0








0







Try making the Toast in your ArrayAdapter class, specifically in the getView method like



convertview.setOnClickListener(new OnClickListener() 
@Override
public void onClick(View view)
Toast.makeText(mContext, "Custom Toast", Toast.LENGTH_SHORT).show();



);


If you want, you can use an onTouchListener instead of the onClickListener.






share|improve this answer













Try making the Toast in your ArrayAdapter class, specifically in the getView method like



convertview.setOnClickListener(new OnClickListener() 
@Override
public void onClick(View view)
Toast.makeText(mContext, "Custom Toast", Toast.LENGTH_SHORT).show();



);


If you want, you can use an onTouchListener instead of the onClickListener.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 15 '18 at 15:12









Ishaan JavaliIshaan Javali

1,3493820




1,3493820












  • This fixed it, thank you very much.

    – Alex
    Nov 15 '18 at 15:16











  • You are most welcome Alex. I am happy that I was able to help!

    – Ishaan Javali
    Nov 15 '18 at 15:16

















  • This fixed it, thank you very much.

    – Alex
    Nov 15 '18 at 15:16











  • You are most welcome Alex. I am happy that I was able to help!

    – Ishaan Javali
    Nov 15 '18 at 15:16
















This fixed it, thank you very much.

– Alex
Nov 15 '18 at 15:16





This fixed it, thank you very much.

– Alex
Nov 15 '18 at 15:16













You are most welcome Alex. I am happy that I was able to help!

– Ishaan Javali
Nov 15 '18 at 15:16





You are most welcome Alex. I am happy that I was able to help!

– Ishaan Javali
Nov 15 '18 at 15:16



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53322232%2fhow-to-create-custom-toast-message-for-different-item-in-a-list-in-a-custom-adap%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

政党

天津地下鉄3号線