I have a listview with a custom adapter, the adapter contains two TextViews and one ImageButton. The list contains objects of my class Firm. When a user presses the ImageButton I want to get some information from the Firm object, by using a get()-method.
Some Codesnippets underneath
My CustomAdapter
import java.util.Vector;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class CustomAdapter extends ArrayAdapter<Firm>
{
Context context;
int layoutResourceId;
static String datesort;
ImageButton call;
Firm mrb;
Firm currentMRB;
Vector<Firm> data;
/** Called when the activity is first created. */
// TODO Auto-generated constructor stub
public CustomAdapter(Context context, int layoutResourceId, Vector<Firm> 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;
MyStringReaderHolder holder;
if(row==null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent,false);
holder= new MyStringReaderHolder();
holder.gtumcaTvFirstName =(TextView)row.findViewById(R.id.gtumcaTvFirstName);
holder.gtumcaTvLastName =(TextView)row.findViewById(R.id.gtumcaTvLastName);
call = (ImageButton)row.findViewById(R.id.imageButton1);
row.setTag(holder);
}
else
{
holder=(MyStringReaderHolder) row.getTag();
}
mrb =data.elementAt(position);
System.out.println("Position="+position);
holder.gtumcaTvFirstName.setText(mrb.getCompanyName());
int price = (int)mrb.getPrice();
String priceString = Integer.toString(price);
holder.gtumcaTvLastName.setText(priceString);
call.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Toast.makeText(context, mrb.getNumber(), Toast.LENGTH_LONG).show();
}
});
return row;
}
Look at the getView() function, this contains a OnClickListener() method that I want to run when the user presses one of the items in the ListView, the function should raise a Toast with the object's get() method
But it doesn't work. Does anybody know how I can complete this task? Thanks in advance.
UPDATE
I've found whats wrong, I forgot that the Button was a ImageButton rather than a regular Button. But how can I get the objects number? My Toast just generates random numbers from the getNumber() method based on all the objects, more specified I want the number of the object in e.g position 3 in my list.