1

I created custom adapter for listview which contain text and images, on click of particular list item I have to open another activity, but I am not able to fire listview click event, below is my code.

Thanks.

Adapter

public class ImageAndTextAdapter extends ArrayAdapter<String> {

private LayoutInflater mInflater;
private Context myCtx;
private String[] mStrings;
private TypedArray mIcons;

private int mViewResourceId;

public ImageAndTextAdapter(Context ctx, int viewResourceId,
        String[] strings, TypedArray icons) {
    super(ctx, viewResourceId, strings);
    myCtx = ctx;
    mInflater = (LayoutInflater)ctx.getSystemService(
            Context.LAYOUT_INFLATER_SERVICE);

    mStrings = strings;
    mIcons = icons;

    mViewResourceId = viewResourceId;
}

@Override
public int getCount() {
    return mStrings.length;
}

@Override
public String getItem(int position) {
    return mStrings[position];
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    convertView = mInflater.inflate(mViewResourceId, null);

    ImageView iv = (ImageView)convertView.findViewById(R.id.option_icon);
    iv.setImageDrawable(mIcons.getDrawable(position));


    TextView tv = (TextView)convertView.findViewById(R.id.option_text);
    tv.setText(mStrings[position]);

    return convertView;
}
}

Main acitivity

public class OurWishingWellActivity extends ListActivity implements OnClickListener{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
   // setContentView(R.layout.list_view_image);

    Context ctx = getApplicationContext();
    Resources res = ctx.getResources();

    String[] options = res.getStringArray(R.array.reg_item_names);
    TypedArray icons = res.obtainTypedArray(R.array.reg_icons);

    setListAdapter(new ImageAndTextAdapter(ctx, R.layout.list_item,
            options, icons));

}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

}


}
RobinHood
  • 10,897
  • 4
  • 48
  • 97
Ishtiaq
  • 1,206
  • 9
  • 18

2 Answers2

0

Add listener

@Override

public View getView(int position, View convertView, ViewGroup parent) { convertView = mInflater.inflate(mViewResourceId, null);

ImageView iv = (ImageView)convertView.findViewById(R.id.option_icon);
iv.setImageDrawable(mIcons.getDrawable(position));


TextView tv = (TextView)convertView.findViewById(R.id.option_text);
tv.setText(mStrings[position]);

convertView .setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            startActivity();


        }
    });

return convertView;

}

Andy
  • 5,379
  • 7
  • 39
  • 53
0

Respected Aafag#

You have write onclick of listview like below.

  @Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    super.onListItemClick(l, v, position, id);
    Toast.makeText(AdvancedListViewActivity.this, "working", Toast.LENGTH_SHORT).show();
    System.out.println("working!!!!!!!!!");
}
RobinHood
  • 10,897
  • 4
  • 48
  • 97
  • yeah you are right but i'm new to it you have posted about list view.. in my case there is not any list in xml , just a text view and image view it creates an list at run time, i need an onItemClick event that can call some other activity.. this is the actual example http://androidcookbook.com/Recipe.seam;jsessionid=5424397F3130CE7769FF47DD67742911?recipeId=1418&recipeFrom=ViewTOC – Ishtiaq Mar 26 '12 at 06:03
  • Yeah, I implemented that example and after adding this few lines codes its working for me, is it working for you also or not? – RobinHood Mar 26 '12 at 06:21
  • would you please send me the whole code.. it gives me error again.. sorry for this bro:( – Ishtiaq Mar 26 '12 at 06:52
  • yeah i have removed but now i cant see list in AVD.. its blank – Ishtiaq Mar 26 '12 at 07:03
  • my reputation is 6, i can't join if you have no problem, kindly send via mail.. ishtiaq@relax-solutions.com – Ishtiaq Mar 26 '12 at 07:09
  • Check this http://pastebin.com/zWEkQpU4 and if yet you are facing problem then Join this room..http://chat.stackoverflow.com/rooms/5098/android-people, you will find me there. – RobinHood Mar 26 '12 at 07:12