gpt4 book ai didi

android - 如何在 ListView 中点击列表的行和行中的按钮?

转载 作者:行者123 更新时间:2023-11-29 14:39:25 25 4
gpt4 key购买 nike

我在我的 Android 程序中使用 ListView 。

我有行。 1)我在按钮中有自定义行,我想在单击按钮时打开警报框,然后单击该行然后打开新 Activity ,但只有一个按钮被单击而不是行被单击。在这种情况下如何可能。我的代码在下面。

谢谢。

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

最佳答案

试试这个,

使用 TextView 代替按钮。并将 onclickListerner 写入 TextView。我在 ListView 中遇到了同样的问题使用 textview 单击按钮现在工作正常。试试吧。

关于android - 如何在 ListView 中点击列表的行和行中的按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16458131/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com