YXTS122
YXTS122
采纳率100%
2017-05-10 04:12

按钮按了有反应,可onItemClick却没执行到

5
已采纳

图片说明
图片说明
1.按钮按了有反应,可onItemClick却没执行到
2.无论按哪个按钮,都显示"你点击的标题是:苹果"

 package com.example.exer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends ListActivity  {
    private static final String TAG = "MainActivity";
    private List<Map<String, Object>> mdata;
    private int positio;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main);
        ListView mListView=getListView();
        mdata = data();
        mListView.setAdapter(new MyAdapter(this,mdata));
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> l, View v, int position, long id) {
    Log.i(TAG, (String)mdata.get(position).get("title"));
    Toast.makeText(MainActivity.this, (String)mdata.get(position).get("title"), 1000).show();
    }
        });
    }

    class MyAdapter extends BaseAdapter{
    private Context context;
    private LayoutInflater minflater;
    private ArrayList<Map<String,Object>> datalist;
    public MyAdapter(Context context,List<Map<String,Object>> datalist) {
        this.context=context;
        this.minflater = LayoutInflater.from(context);
        this.datalist=(ArrayList<Map<String,Object>>)datalist;
    }

    @Override
    public int getCount() {
    // TODO Auto-generated method stub
    return datalist.size();
    }

    @Override
    public Object getItem(int position) {
    // TODO Auto-generated method stub
    return datalist.get(position);
    }

    @Override
    public long getItemId(int position) {
    // TODO Auto-generated method stub
        positio=position;
    return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

    Hodler hodler =null;    
    if(convertView == null){
    hodler = new Hodler();
    convertView = minflater.inflate(R.layout.mylist, null);
    hodler.img = (ImageView) convertView.findViewById(R.id.img);
    hodler.title = (TextView) convertView.findViewById(R.id.title);
    hodler.info = (TextView) convertView.findViewById(R.id.info);
    hodler.button = (Button) convertView.findViewById(R.id.button);
    convertView.setTag(hodler);
    }else{
    hodler = (Hodler) convertView.getTag();
    }
    hodler.img.setBackgroundResource((Integer) mdata.get(position).get("img"));
    hodler.title.setText((CharSequence) mdata.get(position).get("title"));
    hodler.info.setText((CharSequence) mdata.get(position).get("info"));
    hodler.button.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
    showdialog();   
    }
    });
    return convertView;
    }
    class Hodler {
        public ImageView img;
        public TextView title;
        public TextView info;
        public Button button;
        }

    }

    public void showdialog() {
    new AlertDialog.Builder(this).setTitle("提示信息").setMessage("你点击的标题是:  " +(String)mdata.get(positio).get("title"))
    .setPositiveButton("确定", new DialogInterface.OnClickListener(){

        @Override
        public void onClick(DialogInterface dialog, int which) {
        Toast.makeText(MainActivity.this, "确定", 1000).show();
        setResult(RESULT_OK);
        finish();
        }
     }).setNegativeButton("取消", new DialogInterface.OnClickListener() {

    @Override
    public void onClick(DialogInterface dialog, int which) {

    }
    }).show();
    }


    public List<Map<String, Object>> data() {
    List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("title", "苹果");
    map.put("info", "apple");
    map.put("img", R.drawable.ic_launcher); 
    list.add(map);
    map = new HashMap<String, Object>();
    map.put("title", "书");
    map.put("info", "book");
    map.put("img", R.drawable.ic_launcher);
    list.add(map);
    map = new HashMap<String, Object>();
    map.put("title", "电脑");
    map.put("info", "computer");
    map.put("img", R.drawable.ic_launcher);
    list.add(map);
    map = new HashMap<String, Object>();
    map.put("title", "对话");
    map.put("info", "dialog");
    map.put("img", R.drawable.ic_launcher);
    list.add(map);
    map = new HashMap<String, Object>();
    map.put("title", "运行");
    map.put("info", "run");
    map.put("img", R.drawable.ic_launcher);
    list.add(map);
    return list;
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

8条回答

  • Ai_wen_ Hunter-Lei 4年前

    你把positio定义到哪了,建议你弹窗还是用回调,在外面实现,或者你可以给showdialog()传参showdialog(int position)

    点赞 1 评论 复制链接分享
  • qq_35071078 下课睡觉 4年前

    如果ListView中的单个Item的view中存在checkbox,button等view,会导致ListView.setOnItemClickListener无效,事件会被子View捕获到,ListView无法捕获处理该事件.

    点赞 1 评论 复制链接分享
  • qq_35071078 下课睡觉 4年前

    在checkbox、button对应的view处加
    android:focusable="false"

    android:clickable="false"

    android:focusableInTouchMode="false"

    点赞 1 评论 复制链接分享
  • hfc123hfc123 郁闷的中交兵 4年前

    看一下view的事件分发机制会更好的理解

    点赞 评论 复制链接分享
  • qq_33756493 昵称_Justin 4年前
  • EX_Cj 有生之莲萨摩耶 4年前

    点击事件冲突,你的item中的button覆盖了item的点击事件

    点赞 评论 复制链接分享
  • Ai_wen_ Hunter-Lei 4年前

    inItemClick执行不到,你在listview中加上这个试试android:descendantFocusability="blocksDescendants"

    点赞 评论 复制链接分享
  • Ai_wen_ Hunter-Lei 4年前

    Toast,没有1000,只有0和1,显示时间长短

    点赞 评论 复制链接分享

为你推荐