English 中文(简体)
点击点击ListsView 图像的收听器 - Android
原标题:onClick listener to a ListView Image - Android

我有一个 < code> ListView , 右手侧有图像。 我想通过点击 < code> ListView 上的图像, 执行 < code> on Click 听众活动。 请参见图像参考 。

""https://i.sstatic.net/j889e.jpg" alt="此处输入图像描述"/ >

我知道基本的 on Click 收听器执行情况,

忘了提到, 点击实际的 < code> ListView 将会拍摄新活动, 所以我需要维持两个方案 。

 listView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            eventsData.remove(id);
            cursor.requery(); 
        时 时

    时 时);   

以上代码通过单击任何列表元素 eventsData.remove( id); 是执行此任务的数据库助手。 如我所说, 现在需要一种方法来执行相同的进程, 点击图像而不是整个列表元素, 我希望列表元素稍后能做一些其他动作 。

我希望我现在能说清楚一点。


<强> 解决方案:

如果任何人遇到同样的情况, 那么这里是适配器的完整代码 。

 class CustomAdapter extends ArrayAdapter<String> {
 CustomAdapter() {
  super(Activity.this, R.layout.row, R.id.label, items);
时 时

public View getView(final int position, View convertView,
                    ViewGroup parent) {
  View row=super.getView(position, convertView, parent);
  deleteImg=(ImageView)row.findViewById(R.id.icon);

  deleteImg.setImageResource(R.drawable.delete);      
  deleteImg.setOnClickListener(new OnClickListener() {
      String s = items[position];
    @Override
    public void onClick(View v) {
        Toast.makeText(context, s, Toast.LENGTH_SHORT).show();          
    时 时
时 时);

  return(row);
时 时

时 时

我知道编码有点烂,所以让我忍耐一下, 我只是想展示一下实际的过程。

它为我工作:)

最佳回答

您应该在您的 Adapter 中做,而不是 Aactivity

yourImg.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // things you want to do 
    }
});
问题回答

创建一个用于从适应器向活动通信的界面

public interface ListenerActivity        
{    
public void Remove();    
}

在您的适应器类中 :

class CustomAdapter extends ArrayAdapter<String> {

ListenerActivity listener;

CustomAdapter(YourActivity obj) {
  super(Activity.this, R.layout.row, R.id.label, items);

  listener=obj;
}
 public View getView(final int position, View convertView,
                ViewGroup parent) {
ImageId.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        listener.Remove();
    }
});
}

您的活动 :

class MyActivity extends Activity implements ListenerActivity
{
 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
}
 @Override
    public void Remove() {

//Do your remove functionality here
    }         
}

添加到最后一个答案, 并在此解答中, 我添加了一些代码, 在删除一个项目或其他动作后刷新视图 :

//in your adapter you have to get list of items:

 public CartListAdapter(Context context, int resourceId,
        List<CartItemModel> items) {
    super(context, resourceId, items);
    this.context = context;
    this.items= items;
}

//after that you have to set a listner on the imageView like this:

public View getView(int position, View convertView, ViewGroup parent) {
   holder.imageViewDelete.setOnClickListener(new imageViewClickListener(position));
   // some other lines code ...
   // here we get the item to remove
    rowItem= getItem(position);
    }

//and here the definition of the listner:

private class imageViewClickListene implements OnClickListener {
   int position;
    public imageViewClickListene( int pos)
        {
            this.position = pos;
        }

    public void onClick(View v) {

    // here we  remove the selected item    
        items.remove(rowItem);
            // here we refresh the adapter
        CartListAdapter.this.notifyDataSetChanged();

}
}

它为我工作,享受吧!





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...