user1339609
user1339609

Reputation: 11

How can I use setOnClickListener in my adapter Android?

In my aplication I have ListView and adapter to my ListView. My item ListView have two elements Text and Image. Now I want to separate the text and click on the picture.

public class MyAdapter extends ArrayAdapter<String> {
private Activity context;
private ArrayList<String> categories;
public static boolean remove = true;

public MyAdapter(Activity context, ArrayList<String> categories) {
    super(context, R.layout.my_list_element, categories);
    this.context = context;
    this.categories = categories;
}

static class ViewHolder {
    public TextView tvLanguage;
    public ImageView remove;
}

public View getView(int position, View convertView, ViewGroup parent) {
    View rowView = convertView;
    if (rowView == null) {
        LayoutInflater inflater = context.getLayoutInflater();
        rowView = inflater.inflate(R.layout.my_list_element, null);
        ViewHolder viewHolder = new ViewHolder();
        viewHolder.tvLanguage = (TextView) rowView.findViewById(R.id.tvLanguage);
        viewHolder.remove = (ImageView) rowView.findViewById(R.id.remove);
        rowView.setTag(viewHolder);
    }
    ViewHolder holder = (ViewHolder) rowView.getTag();
    holder.tvLanguage.setText(categories.get(position));
    if(remove)
        holder.remove.setVisibility(View.GONE);
    else
        holder.remove.setVisibility(View.VISIBLE);
    return rowView;
}

How i should separate text and image and success use setOnClickListener on two elements ?

Upvotes: 1

Views: 8903

Answers (3)

Joey
Joey

Reputation: 697

Try to add flags to your intent like this:

Intent i = new Intent (this, AddRSS.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);

The code above will start an activity and it will become the start of a new task on the history stack.

Upvotes: 0

user1339609
user1339609

Reputation: 11

I solved my problem :) I can't use this code

Intent i = new Intent (this, AddRSS.class); 
startActivity (i);

because my class MyAdapter does not extend Activity so I could not use a method startActivity(i);

The solution is simple, I use context :

Intent i = new Intent (context, AddRSS.class); 
context.startActivity (i);

and now works ;)

Upvotes: 0

Mahdi Hijazi
Mahdi Hijazi

Reputation: 4454

modify the first block to be like this:

if (rowView == null) {
        LayoutInflater inflater = context.getLayoutInflater();
        rowView = inflater.inflate(R.layout.my_list_element, null);
        ViewHolder viewHolder = new ViewHolder();
        viewHolder.tvLanguage = (TextView) rowView.findViewById(R.id.tvLanguage);
        viewHolder.remove = (ImageView) rowView.findViewById(R.id.remove);
        rowView.setTag(viewHolder);

        viewHolder.tvLanguage.setOnClickListener(new View.OnClickListener() {
           public void onClick(View v){  
              // handle your text view  
           }
        });

        viewHolder.remove.setOnClickListener(new View.OnClickListener() {
           public void onClick(View v){    
              // handle your imageview
           }
        });

}

Upvotes: 4

Related Questions