-9

In my application, I have added one button named as 'add'. If I click that button, I want to perform some another activity. But if I click the button, nothing happens. Can anyone please tell me what is my mistake?

ListViewAdapter class:

public class ListViewAdapter extends BaseAdapter {

    // Declare Variables
    Context context;
    LayoutInflater inflater;
    ArrayList<HashMap<String, String>> data;
    ImageLoader imageLoader;
    HashMap<String, String> resultp = new HashMap<String, String>();

    public ListViewAdapter(Context context,
            ArrayList<HashMap<String, String>> arraylist) {
        this.context = context;
        data = arraylist;
        imageLoader = new ImageLoader(context);
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }
     /* private view holder class */
    private class ViewHolder {

        Button Add;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {
        // Declare Variables
        TextView title;
         ViewHolder holder = null;
        ImageView thumb_url;


        LayoutInflater mInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.listview_item, null);
            holder = new ViewHolder();
            holder.Add = (Button) convertView.findViewById(R.id.add);



            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
            convertView.setTag(holder);
        }
        //View itemView = mInflater.inflate(R.layout.listview_item, parent, false);
        // Get the position
        resultp = data.get(position);

        // Locate the TextViews in listview_item.xml
        title = (TextView) convertView.findViewById(R.id.rank);

        // Locate the ImageView in listview_item.xml
        thumb_url = (ImageView) convertView.findViewById(R.id.flag);

        // Capture position and set results to the TextViews
        title.setText(resultp.get(MainActivity.TITLE));

        // Capture position and set results to the ImageView
        // Passes flag images URL into ImageLoader.class
        imageLoader.DisplayImage(resultp.get(MainActivity.THUMB_URL), thumb_url);
        // Capture ListView item click
        Button Add = (Button) convertView.findViewById(R.id.add);
        try {
            holder.Add.setOnClickListener(new OnClickListener() {

                @SuppressWarnings("unused")
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {

                    try {                              

                        //call ur intent here
                        Intent in = new Intent(getApplicationContext(), First.class);



                        startActivity(in);

                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                }

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

                }
            });
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return convertView;
    }

    protected Context getApplicationContext() {
        // TODO Auto-generated method stub
        return null;
    }

    protected void startActivity(Intent in) {
        // TODO Auto-generated method stub

    }   
}
John Smith
  • 7,243
  • 6
  • 49
  • 61
user3739863
  • 41
  • 1
  • 9

9 Answers9

0

Set focusable and focusable in touch mode to FALSE for other view of your row layout other than button.

John Smith
  • 7,243
  • 6
  • 49
  • 61
Sunit Kumar Gupta
  • 1,203
  • 1
  • 11
  • 19
0

Your onClick listener should be like this:

button.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
                // Write your code here     
    }
});
John Smith
  • 7,243
  • 6
  • 49
  • 61
Jogendra Gouda
  • 405
  • 4
  • 17
0

Try this.

Add.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent i = new Intent(getApplicationContext(), First.class);
        startActivity(i);
    }
});
pbaris
  • 4,525
  • 5
  • 37
  • 61
Aman Singh
  • 370
  • 1
  • 9
0

The onClick() method is empty:

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

}

An empty method obviously doesn't do anything.

You probably wanted to have your unused onItemClick() as your onClick().

After that you probably want to replace empty getApplicationContext() and startActivity() with calls to actual implementations of similarly named methods.

laalto
  • 150,114
  • 66
  • 286
  • 303
0

Try this updated code:

public class ListViewAdapter extends BaseAdapter {

    // Declare Variables
    Context context;
    LayoutInflater inflater;
    ArrayList<HashMap<String, String>> data;
    ImageLoader imageLoader;
    HashMap<String, String> resultp = new HashMap<String, String>();

    public ListViewAdapter(Context context,
            ArrayList<HashMap<String, String>> arraylist) {
        this.context = context;
        data = arraylist;
        imageLoader = new ImageLoader(context);
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }
     /* private view holder class */
    private class ViewHolder {

        Button Add;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {
        // Declare Variables
        TextView title;
         ViewHolder holder = null;
        ImageView thumb_url;


        LayoutInflater mInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.listview_item, null);
            holder = new ViewHolder();




            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
            convertView.setTag(holder);
        }
        //View itemView = mInflater.inflate(R.layout.listview_item, parent, false);
        // Get the position
        resultp = data.get(position);

        // Locate the TextViews in listview_item.xml
        title = (TextView) convertView.findViewById(R.id.rank);

        // Locate the ImageView in listview_item.xml
        thumb_url = (ImageView) convertView.findViewById(R.id.flag);
        Add = (Button) convertView.findViewById(R.id.add);

        // Capture position and set results to the TextViews
        title.setText(resultp.get(MainActivity.TITLE));

        // Capture position and set results to the ImageView
        // Passes flag images URL into ImageLoader.class
        imageLoader.DisplayImage(resultp.get(MainActivity.THUMB_URL), thumb_url);
        // Capture ListView item click

        try {
            holder.Add.setOnClickListener(new OnClickListener() {

                @SuppressWarnings("unused")
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {

                    try {                              

                        //call ur intent here
                        Intent in = new Intent(getApplicationContext(), First.class);



                        startActivity(in);

                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                }

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
  Toast.makeText(context, "Clicked Add Button", Toast.LENGTH_SHORT).show();
                }
            });
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return convertView;
    }

    protected Context getApplicationContext() {
        // TODO Auto-generated method stub
        return null;
    }

    protected void startActivity(Intent in) {
        // TODO Auto-generated method stub

    }   
}
John Smith
  • 7,243
  • 6
  • 49
  • 61
Engr Waseem Arain
  • 1,163
  • 1
  • 17
  • 36
0

Use This code instead of your onClick.

holder.Add.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        //call ur intent here
        Intent in = new Intent(getApplicationContext(), First.class);
        startActivity(in);
        Toast.makeText(context, "Clicked Add Button", Toast.LENGTH_SHORT).show();
    }
});
John Smith
  • 7,243
  • 6
  • 49
  • 61
Sreejith SP
  • 171
  • 3
  • 22
0

Try this in you code:

holder.Add.setOnClickListener(new View.OnClickListener()
{
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

    // your code

    }
} 
JJJ
  • 32,902
  • 20
  • 89
  • 102
0

You should use setOnClickListener method:

Add.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
       //call ur intent here
       Intent in = new Intent(getApplicationContext(), First.class);
       startActivity(in);
       Toast.makeText(context, "Clicked Add Button", Toast.LENGTH_SHORT).show();
    }
});
Aleksandr
  • 4,906
  • 4
  • 32
  • 47
0

Try

holder.Add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
    Intent i = new Intent(context, First.class);
    context.startActivity(i);
}
});
PEHLAJ
  • 9,980
  • 9
  • 41
  • 53
user8060686
  • 15
  • 1
  • 7