使用图像按钮,在每个listview项目行中尝试启动新活动

时间:2012-10-15 07:58:36

标签: android android-layout android-intent android-listview

我在每个listview项目行中使用图像按钮并尝试使用Button onClick侦听器启动新活动,但获取: - 源附件不包含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

}

  }

2 个答案:

答案 0 :(得分:1)

你的适配器正在返回一个视图。正确?

现在在你的onitemclickListener

ImageButton myImageButton = (ImageButton) view.findViewById(R.id.addorder);

现在在myImageButton上应用onclicklistener。

答案 1 :(得分:1)

在链接中提到的自定义适配器中,在getView方法中添加Click listner to image按钮 因此,您可以处理单击自定义适配器中的图像按钮,如果您想在活动之间发送数据,您也可以根据点击位置在该点击列表器中发送数据

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

检查一下,让我知道这是否有帮助

将您的代码与以下发布的代码进行比较

/*
 * 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);
    }


}