使用图像按钮,每个列表视图项行,并尝试启动新活动 [英] using image button, in each listview item row and trying to start a new activity

查看:101
本文介绍了使用图像按钮,每个列表视图项行,并尝试启动新活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用图像按钮,每个列表视图项行,并尝试使用按钮的onClick监听器启动一个新的活动,但得到: - 源附件不包含源为ComponentName.class

 包com.example.androidhive;公共类LazyAdapter延伸BaseAdapter {受保护的静态最终上下文的背景下= NULL;
私人活动活动;
私人的ArrayList<&HashMap的LT;字符串,字符串>>数据;
私有静态LayoutInflater吹气= NULL;
公共ImageLoader的ImageLoader的;公共LazyAdapter(活动一,ArrayList的<&HashMap的LT;字符串,字符串> D 1和D){
    活性=一个;
    数据= D;
    吹气=(LayoutInflater)活动。
  getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    ImageLoader的=新ImageLoader的(activity.getApplicationContext());
}公众诠释的getCount(){
    返回data.size();
}公共对象的getItem(INT位置){
    返回的位置;
}众长getItemId(INT位置){
    返回的位置;
}公共查看getView(INT位置,查看convertView,父母的ViewGroup){
    查看VI = convertView;
    如果(convertView == NULL)
        VI = inflater.inflate(R.layout.list_row,NULL);    TextView的标题=(TextView中)vi.findViewById(R.id.title);
    TextView的艺术家=(TextView中)vi.findViewById(R.id.description);
    TextView的持续时间=(TextView中)vi.findViewById(R.id.cost);
    TextView的常规=(TextView中)vi.findViewById(R.id.regular);
    TextView的小=(TextView中)vi.findViewById(R.id.small);
    TextView的大型=(TextView中)vi.findViewById(R.id.large);
    ImageView的thumb_image =(ImageView的)vi.findViewById(R.id.item_image);
    的ImageButton btn_add =(的ImageButton)vi.findViewById(R.id.addorder);    HashMap的<字符串,字符串>歌=新的HashMap<字符串,字符串>();    曲= data.get(位置);    //设置列表视图中的所有值
    title.setText(song.get(CustomizedListView.KEY_TITLE));
    artist.setText(song.get(CustomizedListView.KEY_DESC));
    duration.setText(song.get(CustomizedListView.KEY_COST));
    regular.setText(song.get(CustomizedListView.KEY_REGULAR));
    small.setText(song.get(CustomizedListView.KEY_SMALL));
    large.setText(song.get(CustomizedListView.KEY_LARGE));    imageLoader.DisplayImage(song.get(CustomizedListView.KEY_THUMB_URL),thumb_image);    btn_add.setOnClickListener(新OnClickListener()
    {
        公共无效的onClick(视图v)
        {
           意向意图=新意图(上下文,FinalOrder.class);
           startActivity(意向);
        }
    });    返回VI;   }保护无效startActivity(意向意图){
    // TODO自动生成方法存根}  }


解决方案
在您的链接中提到,在 getView 方法

在自定义适配器点击听者添加到图像按钮
因此,你可以处理你的图像按钮的点击自定义适配器,如果你想发送活动之间的数据,您还可以在点击听者发送基于对点击位置

  myImageButton.setOnClickListener(新OnClickListener()
            {
                公共无效的onClick(视图v)
                {
意向意图=新意图(上下文,nextactivity.class);
intent.putExtra(价值,ValueArray [位置]);
startActivity(意向);
})};

检查出来,让我知道,如果这可以帮助你。

下面贴code比较你的code

  / *
 *版权所有(C)2010埃里克·哈洛
 *
 *在Apache许可,2.0版(以下简称许可证);
 *您不能使用这个文件除了在遵守许可。
 *您可以在获得许可证的副本
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 *除非适用法律要求或书面同意,软件
 *许可下发布的分布在原样的基础,
 *无担保或任何形式的条件,无论是前preSS或暗示的保证。
 *请参阅许可证的特定语言的管理权限和
 *根据许可证的限制。
 * /包com.ericharlow.DragNDrop;进口的java.util.ArrayList;进口android.content.Context;
进口android.content.Intent;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.view.ViewGroup;
进口android.widget.BaseAdapter;
进口android.widget.ImageView;
进口android.widget.TextView;
进口android.widget.Toast;公共final类DragNDropAdapter扩展了BaseAdapter实现的removeListener,DropListener {    //私人诠释[] MIDS;
    //私人诠释[] mLayouts;
    私人LayoutInflater mInflater;
    私人的ArrayList<串GT; mContent;
    私人语境mContext;    公共DragNDropAdapter(上下文的背景下,ArrayList的<串gt;内容){
        mContext =背景;
        的init(背景下,新的INT [] {} android.R.layout.simple_list_item_1,新的INT [] {} android.R.id.text1,内容);
    }    / *公共DragNDropAdapter(上下文的背景下,INT [] itemLayouts,INT [] itemIDs,ArrayList的<串gt;内容){
        的init(背景下,itemLayouts,itemIDs,内容);
    } * /    私人无效的init(上下文的背景下,INT []布局,INT [] IDS,ArrayList的<串gt;内容){
        //缓存的LayoutInflate避免要求每次一个新的。
        mInflater = LayoutInflater.from(上下文);
        // MIDS = IDS;
        // mLayouts =布局;
        mContent =内容;
    }    / **
     *项目列表中的数
     * @see android.widget.ListAdapter#getCount将()
     * /
    公众诠释的getCount(){
        返回mContent.size();
    }    / **
     *由于数据来自阵列,只是返回指数是
     *足以获得数据。如果我们使用一个更复杂的数据
     *结构,我们将返回任​​何对象重新presents在一排
     *名单。
     *
     * @see android.widget.ListAdapter#的getItem(INT)
     * /
    公共字符串的getItem(INT位置){
        返回mContent.get(位置);
    }    / **
     *使用数组索引作为一个唯一的ID。
     * @see android.widget.ListAdapter#getItemId(INT)
     * /
    众长getItemId(INT位置){
        返回的位置;
    }    / **
     *做一个视图来保存每一行。
     *
     * @see android.widget.ListAdapter#getView(INT,android.view.View,
     * android.view.ViewGroup)
     * /
    公共查看getView(最终诠释的立场,观点convertView,父母的ViewGroup){
        //一个ViewHolder不断提及孩子的意见,以避免不必要的来电
        //在每个行findViewById()。
        ViewHolder持有人;        //当convertView不为空,我们可以直接重用,也没有必要
        //为reinflate它。我们只吹一个新的View提供的convertView时
        //通过ListView控件为空。
        如果(convertView == NULL){
            mInflater = LayoutInflater.from(mContext);
            convertView = mInflater.inflate(R.layout.dragitem,NULL);
            //创建两个孩子意见ViewHolder和存储引用
            //我们需要将数据绑定到。
            持有人=新ViewHolder();
            holder.text =(TextView中)convertView.findViewById(R.id.TextView01);
            holder.image =(ImageView的)convertView.findViewById(R.id.ImageView01);
            convertView.setTag(保持器);
        }其他{
            //获取ViewHolder回才能到TextView的快速访问
            //和ImageView的。
            支架=(ViewHolder)convertView.getTag();
        }
        holder.image.setOnClickListener(新OnClickListener(){
            公共无效的onClick(视图v){                Toast.makeText(mContextImageClickClick+位置,Toast.LENGTH_LONG).show();
                意图noteIntent =新意图(mContext,AnotherActivity.class);
                noteIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                mContext.startActivity(noteIntent);
            }
        });
        //与支架有效地结合的数据。
        holder.text.setText(mContent.get(位置));        返回convertView;
    }    静态类ViewHolder {
        TextView的文本;
        ImageView的形象;
    }    公共无效onRemove(INT它){
        如果(这℃,其中|| GT&; mContent.size())回报;
        mContent.remove(所);
    }    公共无效onDrop(从int,int值){
        字符串TEMP = mContent.get(从);
        mContent.remove(从);
        mContent.add(至,温度);
    }
}

I am using image button, in each listview item row and trying to start a new activity using Button onClick listener, but getting:- the source attachment does not contain source for the ComponentName.class

package com.example.androidhive;

public class LazyAdapter extends BaseAdapter {

protected static final Context Context = null;
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader; 

public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
    activity = a;
    data=d;
    inflater = (LayoutInflater)activity.
  getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    imageLoader=new ImageLoader(activity.getApplicationContext());
}

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

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
    View vi=convertView;
    if(convertView==null)
        vi = inflater.inflate(R.layout.list_row, null);

    TextView title = (TextView)vi.findViewById(R.id.title); 
    TextView artist = (TextView)vi.findViewById(R.id.description); 
    TextView duration = (TextView)vi.findViewById(R.id.cost); 
    TextView regular = (TextView)vi.findViewById(R.id.regular); 
    TextView small = (TextView)vi.findViewById(R.id.small); 
    TextView large = (TextView)vi.findViewById(R.id.large); 
    ImageView thumb_image=(ImageView)vi.findViewById(R.id.item_image); 
    ImageButton btn_add=(ImageButton)vi.findViewById(R.id.addorder);





    HashMap<String, String> song = new HashMap<String, String>();

    song = data.get(position);

    // Setting all values in listview
    title.setText(song.get(CustomizedListView.KEY_TITLE));
    artist.setText(song.get(CustomizedListView.KEY_DESC));
    duration.setText(song.get(CustomizedListView.KEY_COST));
    regular.setText(song.get(CustomizedListView.KEY_REGULAR));
    small.setText(song.get(CustomizedListView.KEY_SMALL));
    large.setText(song.get(CustomizedListView.KEY_LARGE));

    imageLoader.DisplayImage(song.get(CustomizedListView.KEY_THUMB_URL), thumb_image);

    btn_add.setOnClickListener(new OnClickListener()
    {
        public void onClick(View v)
        {
           Intent intent = new Intent(Context, FinalOrder.class);
           startActivity(intent);
        }
    });

    return vi;

   }

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

}

  }

解决方案

In your custom adapter as mentioned in your link, In the getView method add click listner to image button So you can handle click of your image buttons in the custom adapter and if you want to send data between activities you can also send it in that click listner on based of position on click

myImageButton.setOnClickListener(new OnClickListener()
            {
                public void onClick(View v)
                {
Intent intent = new Intent(Context, nextactivity.class);
intent.putExtra("value",ValueArray[position]);
startActivity(intent);
})};

Check it out and let me know if this helps you

Compare your code with below posted code

/*
 * Copyright (C) 2010 Eric Harlow
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.ericharlow.DragNDrop;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public final class DragNDropAdapter extends BaseAdapter implements RemoveListener, DropListener{

    //private int[] mIds;
    //private int[] mLayouts;
    private LayoutInflater mInflater;
    private ArrayList<String> mContent;
    private Context mContext;

    public DragNDropAdapter(Context context, ArrayList<String> content) {
        mContext = context;
        init(context,new int[]{android.R.layout.simple_list_item_1},new int[]{android.R.id.text1}, content);
    }

    /*public DragNDropAdapter(Context context, int[] itemLayouts, int[] itemIDs, ArrayList<String> content) {
        init(context,itemLayouts,itemIDs, content);
    }*/

    private void init(Context context, int[] layouts, int[] ids, ArrayList<String> content) {
        // Cache the LayoutInflate to avoid asking for a new one each time.
        mInflater = LayoutInflater.from(context);
        //mIds = ids;
        //mLayouts = layouts;
        mContent = content;
    }

    /**
     * The number of items in the list
     * @see android.widget.ListAdapter#getCount()
     */
    public int getCount() {
        return mContent.size();
    }

    /**
     * Since the data comes from an array, just returning the index is
     * sufficient to get at the data. If we were using a more complex data
     * structure, we would return whatever object represents one row in the
     * list.
     *
     * @see android.widget.ListAdapter#getItem(int)
     */
    public String getItem(int position) {
        return mContent.get(position);
    }

    /**
     * Use the array index as a unique id.
     * @see android.widget.ListAdapter#getItemId(int)
     */
    public long getItemId(int position) {
        return position;
    }

    /**
     * Make a view to hold each row.
     *
     * @see android.widget.ListAdapter#getView(int, android.view.View,
     *      android.view.ViewGroup)
     */
    public View getView(final int position, View convertView, ViewGroup parent) {
        // A ViewHolder keeps references to children views to avoid unneccessary calls
        // to findViewById() on each row.
        ViewHolder holder;

        // When convertView is not null, we can reuse it directly, there is no need
        // to reinflate it. We only inflate a new View when the convertView supplied
        // by ListView is null.
        if (convertView == null) {
            mInflater = LayoutInflater.from(mContext);
            convertView = mInflater.inflate(R.layout.dragitem, null);
            // Creates a ViewHolder and store references to the two children views
            // we want to bind data to.
            holder = new ViewHolder();
            holder.text = (TextView) convertView.findViewById(R.id.TextView01);
            holder.image=(ImageView)convertView.findViewById(R.id.ImageView01);
            convertView.setTag(holder);
        } else {
            // Get the ViewHolder back to get fast access to the TextView
            // and the ImageView.
            holder = (ViewHolder) convertView.getTag();
        }
        holder.image.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {

                Toast.makeText(mContext,"ImageClickClick "+position, Toast.LENGTH_LONG).show();
                Intent noteIntent = new Intent(mContext,AnotherActivity.class);
                noteIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                mContext.startActivity(noteIntent);
            }
        });
        // Bind the data efficiently with the holder.
        holder.text.setText(mContent.get(position));

        return convertView;
    }

    static class ViewHolder {
        TextView text;
        ImageView image;
    }

    public void onRemove(int which) {
        if (which < 0 || which > mContent.size()) return;       
        mContent.remove(which);
    }

    public void onDrop(int from, int to) {
        String temp = mContent.get(from);
        mContent.remove(from);
        mContent.add(to,temp);
    }


}

这篇关于使用图像按钮,每个列表视图项行,并尝试启动新活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆