OnClickListener无法在android中运行

时间:2014-06-18 06:27:36

标签: android onclicklistener

在我的应用程序中,我添加了一个名为' add'的按钮。如果我单击该按钮,我想执行另一个活动。但如果我点击按钮,则没有任何反应。谁能告诉我我的错误是什么?

ListViewAdapter上课:

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

    }   
}

9 个答案:

答案 0 :(得分:0)

将可聚焦和可聚焦的触摸模式设置为FALSE,以获得除按钮以外的行布局的其他视图。

答案 1 :(得分:0)

你的onClick听众应该是这样的:

button.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
                // Write your code here     
    }
});

答案 2 :(得分:0)

试试这个。

Add.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent i = new Intent(getApplicationContext(), First.class);
        startActivity(i);
    }
});

答案 3 :(得分:0)

onClick()方法为空:

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

}

空方法显然没有做任何事情。

您可能希望将未使用的onItemClick()作为onClick()

之后,您可能希望将空getApplicationContext()startActivity()替换为对类似命名方法的实际实现的调用。

答案 4 :(得分:0)

试试这个更新的代码:

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

    }   
}

答案 5 :(得分:0)

使用此代码代替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();
    }
});

答案 6 :(得分:0)

在您的代码中尝试此操作:

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

    // your code

    }
} 

答案 7 :(得分:0)

您应该使用setOnClickListener方法:

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();
    }
});

答案 8 :(得分:0)

尝试

holder.Add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
    Intent i = new Intent(context, First.class);
    context.startActivity(i);
}
});