button click on listview displays the same output - java

public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.joinlistviewitem, parent, false);
resultp = data.get(position);
name = (TextView) itemView.findViewById(R.id.personname);
name.setText(resultp.get(Joinlistview.RANK));
Button deletejoin=(Button)itemView.findViewById(R.id.delete);
deletejoin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//resultp.get("userId");
// TODO Auto-generated method stub
Log.e("delete clicked", "delete clicked");
Toast.makeText(context,"clicked"+resultp.get(Joinlistview.RANK), Toast.LENGTH_SHORT).show();
}
});
return itemView;
}
I used the above code for button click on listview.If i click a button it sows the user name with its position. But it displays the same name in all position.??How to solve this??

Use getTag/setTag methods for getting current row value inside onClick as:
Button deletejoin=(Button)itemView.findViewById(R.id.delete);
deletejoin.setTag(resultp.get(Joinlistview.RANK));
And on Button Click:
Toast.makeText(context,"clicked "+ v.getTag().toString(),
Toast.LENGTH_SHORT).show();

Related

Set button and imageview in gridview and how to set each of them listener

Hi guys I need your help. I want to set listner imageview and button in gridview and when I click to imageview app should show dialog with image and when I click to button app should do something.
like this:
enter image description here
You need to create Custom Adapter for your GridView with Custom UI with ImageView and Button, and then inside the getVieew(){} you need to make click listener for Image and button, and there you can do your action with the help of position with your array list.
here I am posting demo adapter for your reference:
here you can change TextView to Button.
public class CustomAdapter extends BaseAdapter{
String [] result;
Context context;
int [] imageId;
private static LayoutInflater inflater=null;
public CustomAdapter(MainActivity mainActivity, String[] prgmNameList, int[] prgmImages) {
// TODO Auto-generated constructor stub
result=prgmNameList;
context=mainActivity;
imageId=prgmImages;
inflater = ( LayoutInflater )context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return result.length;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public class Holder
{
TextView tv;
ImageView img;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Holder holder=new Holder();
View rowView;
rowView = inflater.inflate(R.layout.program_list, null);
holder.tv=(TextView) rowView.findViewById(R.id.textView1);
holder.img=(ImageView) rowView.findViewById(R.id.imageView1);
holder.tv.setText(result[position]);
holder.img.setImageResource(imageId[position]);
rowView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(context, "You Clicked "+result[position], Toast.LENGTH_LONG).show();
}
});
return rowView;
}
}
ref:-https://caveofprogramming.com/guest-posts/custom-gridview-with-imageview-and-textview-in-android.html

How to start activity based on listview item click?

I have the following code but I have tried for days now and I cannot seem to figure out how to start an activity based on the listview item clicked, the result[position] does not work for start intent.
public class CustomAdapter extends BaseAdapter{
String [] result;
Context context;
int [] imageId;
private static LayoutInflater inflater=null;
public CustomAdapter(MainActivity mainActivity, String[] prgmNameList, int[] prgmImages) {
// TODO Auto-generated constructor stub
result=prgmNameList;
context=mainActivity;
imageId=prgmImages;
inflater = ( LayoutInflater )context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return result.length;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public class Holder
{
TextView tv;
ImageView img;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Holder holder=new Holder();
View rowView;
rowView = inflater.inflate(R.layout.program_list, null);
holder.tv=(TextView) rowView.findViewById(R.id.textView1);
holder.img=(ImageView) rowView.findViewById(R.id.imageView1);
holder.tv.setText(result[position]);
holder.img.setImageResource(imageId[position]);
rowView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(context, "You Clicked "+result[position], Toast.LENGTH_LONG).show();
}
});
return rowView;
}
EDIT : If the first list item is clicked I want it to start an activity (images class) , if second clicked to start images2 class and so on..
Starting an activity based on a ListView's item click is not done in the adapter. It is done in the Activity where you show the ListView:
In your Activity:
mListView.setAdapter(mAdapter); //mAdapter is your CustomAdapter
mListView.setClickable(true);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Start the activity here.
//based on the value of position or id.
}
});
Please try this in onclick
if(position ==1){
--fire your first intent here.--
} else if(position ==2) {
-------your other intent------
}
----etc
Try something like this
rowView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent(v.getContext(),ActivityYouWantToGo.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
v.getContext().startActivity(intent);
Toast.makeText(context, "You Clicked "+result[position], Toast.LENGTH_LONG).show();
}
});
EDIT
Just create this method on your BaseAdapter
public void StartActivityForImages(Context context, int i){
switch (i){
case 0:
Intent Intent = new Intent(context, Image0.class);
context.startActivity(Intent);
break;
case 1:
Intent Intent2 = new Intent(context, Image1.class);
context.startActivity(Intent2);
break;
case 2:
Intent Intent3 = new Intent(context, Image3.class);
context.startActivity(Intent3);
break;
......
}
}
And on your setOnClickListener()
You call this method as follows :
StartActivityForImages(v.getContext(),position);
Toast.makeText(context, "You Clicked " + result[position], Toast.LENGTH_LONG).show();
Efficient way to do it
If you don't want to create a method and do this stuff, you could do something like this :
Create a String with the name of your next class like :
String NextActivity = getPackageName()+".Image"+position;
And then you can do something like this :
try {
String className =getPackageName()+".Image"+2;
Intent openNewIntent = new Intent(v.getContext(), Class.forName( className ) );
startActivity( openNewIntent );
} catch (ClassNotFoundException e) {
Log.d("ERRORPEW", e.getMessage());
e.printStackTrace();
}
Don't make position as a final static parameter in getView(), its a wronge way. Use below getView method to startActivity from list Item,
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Holder holder=new Holder();
View rowView;
rowView = inflater.inflate(R.layout.program_list, null);
holder.tv=(TextView) rowView.findViewById(R.id.textView1);
holder.img=(ImageView) rowView.findViewById(R.id.imageView1);
holder.tv.setText(result[position]);
holder.img.setImageResource(imageId[position]);
rowView.setTag(position);
rowView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
int pos = (Integer) v.getTag;
Intent i = new Intent(context, YourActivity.class);
context.startActivity(i);
}
});
return rowView;
}

How to get value of textviews on button click from a listview?

i have a listview with a custom adapter class . Each of the list items contain 2 text view and one Button . i want to get the data of those text views while i click on the Button inside the list .
below is the demo model of listview
please help me out guys
#Override
public View getView(int position, View convertView, ViewGroup parent) {
IconView icon;
TextView title;
TextView description;
Button favour;
if (convertView == null) {
convertView = ((Activity) getContext()).getLayoutInflater().inflate(textViewResourceId, parent, false);
}
icon = (IconView) convertView.findViewById(R.id.station_icon);
title = (TextView) convertView.findViewById(R.id.station_title);
description = (TextView) convertView.findViewById(R.id.station_description);
favour=(Button)convertView.findViewById(R.id.favbutton);
Station station = getItem(position);
convertView.setTag(station);
title.setText(station.title);
icon.setIcon(station.thumb);
description.setText(station.description);
return convertView;
}
}
You can use your list widgets in getView method in your custom adapter as follows-
public View getView(int pos, View convertView, ViewGroup parent) {
View gridView = convertView;
if (gridView == null) {
gridView = mInflater.inflate(layoutId, null);
}
TextView tv1 = (TextView) gridView.findViewById(R.id.tv1);
TextView tv2 = (TextView) gridView.findViewById(R.id.tv2);
Button button = (Button)gridView.findViewById(R.id.btn);
}
Now on button click, you can use text views.
listView.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id)
{
Value That u Pass In Adapter
Toast.makeText(getApplicationContext(), ""+position, Toast.LENGTH_LONG).show();
}
});
inside the getview methed also see Toast
title.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
}
});
icon.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
}
});
favour.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
}
});
just set an onclick listener in your getView method;
icon = (IconView) convertView.findViewById(R.id.station_icon);
title = (TextView) convertView.findViewById(R.id.station_title);
description = (TextView) convertView.findViewById(R.id.station_description);
favour=(Button)convertView.findViewById(R.id.favbutton);
then inside your onclick listener get the TextView's text.
favour.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
//title.getText()
// description.getText();
}
//Here I have one textview and edit text and submit button in list view
#Override
public View getView (int position, View convertView, final ViewGroup parent)
{
final View v = super.getView(position, convertView, parent);
final TextView txt= (TextView) v.findViewById(R.id.lblMenuname);
final EditText QtyView = (EditText) v.findViewById(R.id.editText);
Button b=(Button)v.findViewById(R.id.btnInterview);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String txt2 = txt.getText().toString();
Toast.makeText(ItemList.this,txt2.toString() + "=" + QtyView.getText().toString(),Toast.LENGTH_SHORT).show();
}
});
return v;
}

Buttons in ListView each item layout

I have a listview and I am populating that list view from a ArrayList of custom objects.
This is the custom adapter code.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final int a=position;
FileHolder holder=null;
View row = convertView;
if(row==null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(textViewResourceId, parent, false);
holder=new FileHolder();
holder.file=files.get(position);
holder.deleteButton=(ImageButton) row.findViewById(R.id.deleteButton);
holder.downloadButton=(ImageButton) row.findViewById(R.id.downloadButton);
holder.deleteButton.setTag(holder.file);
holder.downloadButton.setTag(holder.file);
holder.fileName= (TextView) row.findViewById(R.id.fileName);
holder.fileName.setText(holder.file.getFileName());
}
else
{
holder=(FileHolder) row.getTag();
}
fPOJO=holder.file;
final AsyncCall call=new AsyncCall();
holder.downloadButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
MODE=0;
//call.execute(MODE);
}
});
holder.deleteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getContext(), "Delete File "+(String.valueOf(a))+" "+fPOJO.getFileName(), Toast.LENGTH_LONG).show();
MODE=1;
//call.execute(MODE);
}
});
return row;
}
public static class FileHolder{
FilesPOJO file;
TextView fileName;
ImageButton downloadButton;
ImageButton deleteButton;
}
There are two buttons one for file delete and other for file download. I have implemented on click listeners for these two buttons. The problem is when I click any button the fileName in the Toast message is different from the file name that I see on the screen display. For ex: I have 5 files with names
delete.png
upload.png
share.png
referesh.png
copy.png
and now when I click delete button corresponding to referesh.png file the toast pop ups and in that the file name is different than referesh.png.
how to resolve this issue.
EDITS:
This is how I set up this adapter
FilesAdapter adapter=new FilesAdapter(rootView.getContext(), R.layout.file_list_view_item_row, Application.files);
View header = (View) View.inflate(rootView.getContext(), R.layout.files_list_view_header_row, null);
fileListHeading.inflate(rootView.getContext(), R.layout.files_list_view_header_row, null);
filesListView.addHeaderView(header);
filesListView.setAdapter(adapter);
I think the problem is with your fPOJO variable when onClick is called it may have different value because after every getView you're changing it's value.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final int a=position;
final FileHolder holder;
View row = convertView;
if(row==null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(textViewResourceId, parent, false);
holder=new FileHolder();
holder.file=files.get(position);
holder.deleteButton=(ImageButton) row.findViewById(R.id.deleteButton);
holder.downloadButton=(ImageButton) row.findViewById(R.id.downloadButton);
holder.deleteButton.setTag(holder.file);
holder.downloadButton.setTag(holder.file);
holder.fileName= (TextView) row.findViewById(R.id.fileName);
holder.fileName.setText(holder.file.getFileName());
}
else
{
holder=(FileHolder) row.getTag();
}
final AsyncCall call=new AsyncCall();
holder.downloadButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
MODE=0;
//call.execute(MODE);
}
});
holder.deleteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getContext(), "Delete File "+(String.valueOf(a))+" "+holder.fileName, Toast.LENGTH_LONG).show();
MODE=1;
//call.execute(MODE);
}
});
return row;
}
Just make holder final and get you're file name from it.
You store your list item in holder, this is your problem, ListView adapter creates as much convertViews as needed to fill the screen so if you have list of 10+ elements, only few will be added to row.
First of all don't store your list element in holder, you gave adapter method called getItem(positoin) to get it.
In your holder view store only references to layout views (TextView, Buttons etc) and (if(row == null) call findViewById on them and setTag.
Then set text, listeners outside the if statement, like so:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final int a=position;
FileHolder holder=null;
View row = convertView;
if(row==null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(textViewResourceId, parent, false);
holder=new FileHolder();
holder.deleteButton=(ImageButton) row.findViewById(R.id.deleteButton);
holder.downloadButton=(ImageButton) row.findViewById(R.id.downloadButton);
holder.fileName= (TextView) row.findViewById(R.id.fileName);
row.setTag(holder);
}
else
{
holder=(FileHolder) row.getTag();
}
fPOJO= getItem(position);
final AsyncCall call=new AsyncCall();
holder.downloadButton.setOnClickListener(null);
holder.downloadButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
MODE=0;
//call.execute(MODE);
}
});
holder.deleteButton.setOnClickListener(null);
holder.deleteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getContext(), "Delete File "+(String.valueOf(a))+" "+fPOJO.getFileName(), Toast.LENGTH_LONG).show();
MODE=1;
//call.execute(MODE);
}
});
return row;
}
public static class FileHolder{
TextView fileName;
ImageButton downloadButton;
ImageButton deleteButton;
}
Also you can do some code optimalization, for example don't initialize LayoutInflater in getView() method, you can do it in adapter Constructor

How to get position of button in GridView where to place it using tag?

I want to know position, where the button located in GridView using tag, and after getting the position, get letter from that button using position.
Here is the code
public View getView(final int position, View convertView, ViewGroup arg2) {
View v = convertView;
if (v == null)
{
LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.buttonlist, null);
}
final Button btn= (Button)v.findViewById(R.id.letterbtn);
btn.setText(word[position]+"");
btn.setOnClickListener(new OnClickListener(){
public void onClick(View v)
{
btn.setTag(btn);
String tag=btn.getTag().toString();
btn.setVisibility(View.GONE);
}
});
return v;
}
Try this:
final Button btn= (Button)v.findViewById(R.id.letterbtn);
// btn.setText(word[position]+"");
btn.setTag(position);
btn.setOnClickListener(new OnClickListener(){
public void onClick(View v)
{
// btn.setTag(btn);
String pos=btn.getTag().toString();
//You get a position of that button
Log.e("position",pos);
//You get a word of that position
Log.e("word",""+word[Integer.parseInt(pos)]);
btn.setVisibility(View.GONE);
}
});

Categories