1

我在我的 Android 程序中使用列表视图。

我有行。1)我在按钮中有自定义行,我想在单击按钮时打开警报框并单击此行然后打开新活动,但仅单击一个按钮未单击行。在这种情况下如何可能。我的代码在下面。

谢谢你。

public class AlMessagesAdapter extends ArrayAdapter<DtoAllMessages> {

    private LayoutInflater inflator;
    private ArrayList<DtoAllMessages> userlist;

    public AlMessagesAdapter(Activity context, ArrayList<DtoAllMessages> list) {
        super(context, R.layout.custom_list, list);

        this.userlist = list;
        inflator = context.getLayoutInflater();
    }

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

        ViewHolder holder = null;
        if (convertView == null) {
            convertView = inflator.inflate(R.layout.custom_list, null);
            holder = new ViewHolder();
            holder.title = (TextView) convertView.findViewById(R.id.tvName);
            holder.date_cr = (TextView) convertView.findViewById(R.id.tvDate);
            holder.img = (ImageView)convertView.findViewById(R.id.ivIcon);
            holder.tokenBtn = (Button)convertView.findViewById(R.id.tokenBtn);
            convertView.setTag(holder);
            convertView.setTag(R.id.tvName, holder.title);
            convertView.setTag(R.id.tvDate, holder.date_cr);
            convertView.setTag(R.id.ivIcon,holder.img);
            convertView.setTag(R.id.tokenBtn,holder.tokenBtn);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        String token = userlist.get(position).getToken();

        Log.v("MessageList", "token:" + token);

        token = token.substring(0,token.length()-3);

        holder.title.setText(userlist.get(position).getName()+"("+token+")");

        String type_data =  userlist.get(position).getType().toString();

        if((type_data.equals("text")) || (type_data.equals("photo")))
        {
            Log.v("log", "  if  text photo ");
            holder.date_cr.setText(userlist.get(position).getType()+":Received "+userlist.get(position).getCreated_date());
            holder.tokenBtn.setVisibility(View.VISIBLE);
            list.setItemsCanFocus(true);
        }
        else if(type_data.equals("out"))
        {
            Log.v("log", " else out ");
            holder.date_cr.setText(userlist.get(position).getType()+":Sent "+userlist.get(position).getCreated_date());
            holder.tokenBtn.setVisibility(View.GONE);
        }

        if(type_data.equals("text"))
        {
            Log.v("log", " if text ");
            holder.img.setBackgroundResource(R.drawable.chatmessage);

        }
        else if(type_data.equals("photo"))
        {
            Log.v("log", " ese if photo ");
            holder.img.setBackgroundResource(R.drawable.photomessage);

        }
        else if(type_data.equals("out"))
        {
            Log.v("log", " ese if out ");
            holder.img.setBackgroundResource(R.drawable.outmessafe);
        }


        if(position%2==0)
        {
            convertView.setBackgroundResource(R.drawable.whitebackground);
        }
        else
        {
            convertView.setBackgroundResource(R.drawable.greybackground);
        }

        holder.tokenBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Log.v("log_tag"," token button clicked");
            }
        });


        return convertView;
    }

     class ViewHolder {
        protected ImageView img;
        protected TextView date_cr;
        protected TextView title;
        protected Button tokenBtn;
    }
} 

并在下面列出点击事件::

 list.setOnItemLongClickListener(new OnItemLongClickListener() {

            @Override
            public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                    int position, long arg3) {
                // TODO Auto-generated method stub

                 msg = userLIstArray.get(position).getMessage();
                  token = userLIstArray.get(position).getToken();
                  type = userLIstArray.get(position).getType();
                  int msgId = userLIstArray.get(position).getMessageid();
                  token = token.substring(0,token.length()-3);
                  int token_value = Integer.parseInt(token) * 1000;   

                if(type.equals("text"))
                {
                    Log.v("log",  " if in text to Display " + msg + " token "+token);
                   Intent i = new Intent(MessagesList.this,DisplayPopupActivity.class);
                   i.putExtra("msg", msg);
                   i.putExtra("token", token);
                   i.putExtra("msgid", msgId);
                   startActivity(i);

                }
                else if(type.equals("photo"))
                {
                    Log.v("log",  " else in IMage to Display " + msg + " token "+token);

                     Log.v("log","token "+token+" type "+type + " position  "+position + "msgId "+ msgId);

                    Intent i = new Intent(MessagesList.this,DisplayImageActivity.class);
                       i.putExtra("imgData", msg);
                       i.putExtra("token", token);
                       i.putExtra("msgid", msgId);
                       startActivity(i);

               //Log.v("log"," Message" +message);
               //Toast.makeText(AllMessageActivity.this, "Message "+message, Toast.LENGTH_LONG).show();
                }

                return false;
            }
         });
    }
4

5 回答 5

3

尝试这个,

而不是按钮使用TextView。并写入onclickListerner. 我在使用 textview 的按钮单击TextView中遇到了同样的问题,现在它工作正常。ListView去尝试一下。

于 2013-05-09T09:36:53.097 回答
2

您可以使用以下方法添加行点击事件:

listView.setOnItemClickListener(new OnItemClickListener() {
  @Override
  public void onItemClick(final AdapterView<?> parent, final View view, final int position, long id) {
          //go to new activity
  }); 

和按钮事件,就像你正在做的那样..

holder.tokenBtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Log.v("log_tag"," token button clicked");
            //show alert
        }
    });
于 2013-05-09T09:10:35.163 回答
1

在 if 条件下调用你的 clickevent

if (convertView == null) {
            convertView = inflator.inflate(R.layout.custom_list, null);
            holder = new ViewHolder();
            holder.title = (TextView) convertView.findViewById(R.id.tvName);
            holder.date_cr = (TextView) convertView.findViewById(R.id.tvDate);
            holder.img = (ImageView)convertView.findViewById(R.id.ivIcon);
            holder.tokenBtn = (Button)convertView.findViewById(R.id.tokenBtn);
           holder.tokenBtn.setOnClickListener(click);

        }

在外面创建clicklistner。

private OnClickListener click = new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        // do your stuff here
    }
};
于 2013-05-09T09:41:27.687 回答
1

在您的 Adapter 类中设置一个 OnclickListner

private LayoutInflater inflator;
private ArrayList<DtoAllMessages> userlist;
private Context context; //added

public AlMessagesAdapter(Activity context, ArrayList<DtoAllMessages> list) {
    super(context, R.layout.custom_list, list);
    this.context=context; //added
    this.userlist = list;
    inflator = context.getLayoutInflater();
}

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

    ViewHolder holder = null;
    if (convertView == null) {
        convertView = inflator.inflate(R.layout.custom_list, null);
        holder = new ViewHolder();
        holder.title = (TextView) convertView.findViewById(R.id.tvName);
        holder.date_cr = (TextView) convertView.findViewById(R.id.tvDate);
        holder.img = (ImageView)convertView.findViewById(R.id.ivIcon);
        holder.tokenBtn = (Button)convertView.findViewById(R.id.tokenBtn);

        holder.tokenBtn.setOnClickListener((OnClickListener)context); //added portion

        convertView.setTag(holder);
        convertView.setTag(R.id.tvName, holder.title);
        convertView.setTag(R.id.tvDate, holder.date_cr);
        convertView.setTag(R.id.ivIcon,holder.img);
        convertView.setTag(R.id.tokenBtn,holder.tokenBtn);

    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    String token = userlist.get(position).getToken();

    Log.v("MessageList", "token:" + token);

    token = token.substring(0,token.length()-3);

    holder.title.setText(userlist.get(position).getName()+"("+token+")");

    String type_data =  userlist.get(position).getType().toString();

    if((type_data.equals("text")) || (type_data.equals("photo")))
    {
        Log.v("log", "  if  text photo ");
        holder.date_cr.setText(userlist.get(position).getType()+":Received "+userlist.get(position).getCreated_date());
        holder.tokenBtn.setVisibility(View.VISIBLE);
        list.setItemsCanFocus(true);
    }
    else if(type_data.equals("out"))
    {
        Log.v("log", " else out ");
        holder.date_cr.setText(userlist.get(position).getType()+":Sent "+userlist.get(position).getCreated_date());
        holder.tokenBtn.setVisibility(View.GONE);
    }

    if(type_data.equals("text"))
    {
        Log.v("log", " if text ");
        holder.img.setBackgroundResource(R.drawable.chatmessage);

    }
    else if(type_data.equals("photo"))
    {
        Log.v("log", " ese if photo ");
        holder.img.setBackgroundResource(R.drawable.photomessage);

    }
    else if(type_data.equals("out"))
    {
        Log.v("log", " ese if out ");
        holder.img.setBackgroundResource(R.drawable.outmessafe);
    }


    if(position%2==0)
    {
        convertView.setBackgroundResource(R.drawable.whitebackground);
    }
    else
    {
        convertView.setBackgroundResource(R.drawable.greybackground);
    }

    /*holder.tokenBtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Log.v("log_tag"," token button clicked");
        }
    });*/


    return convertView;
}

 class ViewHolder {
    protected ImageView img;
    protected TextView date_cr;
    protected TextView title;
    protected Button tokenBtn;
}
} 

进入你的主课

public Main extends Activity implements OnClickListener{

     public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.casual_layout);

         Button tokenBtn=(Button)findViewById(R.id.tokenBtn);
                tokenBtn.setOnClickListener(this);
         }
       public void onClick(View v) {
    switch (v.getId()) {
                  case R.id.tokenBtn:
                    //Write a code here to execute alertdialog
                    Log.d("ALERT HERE","ALERT HERE"); 
                  break;
             }
}
于 2013-05-09T09:47:45.890 回答
0

如果你想使用 a Button而不是TextViewset

   android:focusable="false"

给你的Button

于 2013-05-09T10:19:48.800 回答