0

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.

1
  • I've found what I did wrong. So there is another question in the bottom of my post. Commented Apr 27, 2012 at 17:39

3 Answers 3

1
call = (ImageButton)row.findViewById(R.id.imageButton1);
call.setFocusable(false);   <----- add this line
row.setTag(holder);

to get the number use position variable

Toast.makeText(context, ""+position, Toast.LENGTH_LONG).show();
Sign up to request clarification or add additional context in comments.

Comments

0

If you want the item that was click in a ListView use this listener:

listView.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
       // The position is passed here, do as you please
    }
}

1 Comment

This will only work outside the CustomAdapter class. I want to use a ImageButton from the layout of the ListView..
0

I solved my problem by doing it this way:

 static class MyStringReaderHolder
 {
    TextView gtumcaTvFirstName,gtumcaTvLastName,gtumcaTvBirthDate;
    ImageView gtumcaIvIcon;
    ImageButton call;
 }

And create a new instance of the static class

        holder= new MyStringReaderHolder();
        holder.call = (ImageButton)row.findViewById(R.id.imageButton1);
        holder.call.setFocusable(false);

Finally I added an OnClickListener to the ImageButton

holder.call.setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {


        Toast.makeText(context,  "" +  data.elementAt(position).getCompanyName(), Toast.LENGTH_SHORT).show(); 

        }
    });

Thanks guys! :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.