我已經實現了帶有自定義適配器的ListView的即時消息傳遞佈局。每行顯示一條消息和小時。實現列表項的xml包含一個Button(@ + id/alert_button),其可見性消失,如果發送的消息出錯,該按鈕應該可見。如何更改ListView中按鈕的可見性只有一行
這裏是XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:id="@+id/content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/contentWithBackground"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/risponditore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:textSize="10sp"
android:textStyle="bold"
android:text=""
android:visibility="gone"
android:textColor="@color/bluScuro"/>
<TableLayout
android:id="@+id/tabellaMessaggioOra"
android:layout_below="@+id/risponditore"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TableRow
android:id="@+id/rigaMessaggio"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/txtMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/black"
android:maxWidth="245dp"
android:textSize="12sp"
android:layout_gravity="left"/>
</TableRow>
<TableRow
android:id="@+id/rigaOra"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity ="right">
<TextView
android:id="@+id/ora"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10sp"
android:textColor="@color/grigioScuro"
android:layout_gravity="right"/>
</TableRow>
</TableLayout>
</RelativeLayout>
<Button
android:id="@+id/alert_button"
android:layout_toRightOf="@+id/contentWithBackground"
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_marginRight="5dp"
android:background="@drawable/alert"
android:layout_centerVertical="true"
android:visibility="gone"/>
</RelativeLayout>
</RelativeLayout>
這是適配器:
public class ChatAdapter extends BaseAdapter implements
StickyListHeadersAdapter {
private List<ChatMessage> lista_messaggi_chat;
private static final String DATE_FORMAT_ORA = "HH:mm";//formato h24
private static final String DATE_FORMAT_GIORNO = "dd";
private Context context;
private ViewHolder viewHolder;
//private boolean visibility;
public ViewHolder getViewHolder(){
return viewHolder;
}
public ChatAdapter(Context context) {
this.context = context;
this.lista_messaggi_chat = new ArrayList<>();
}
@Override
public int getCount() {
return lista_messaggi_chat.size();
}
@Override
public Object getItem(int position) {
return lista_messaggi_chat.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
//Log.i(HoustonConstants.TAG,"ChatAdapter getView");
//ViewHolder viewHolder;
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
if (convertView == null) {
//The view is not a recycled one: we have to inflate
convertView = inflater.inflate(R.layout.list_item_message,null);
viewHolder = createViewHolder(convertView);
convertView.setTag(viewHolder);
}else{
// View recycled !
// no need to inflate
// no need to findViews by id
viewHolder = (ViewHolder) convertView.getTag();
}
final ChatMessage messaggio_chat = (ChatMessage)getItem(position);
if (messaggio_chat.isIncoming()) {
viewHolder.risponditore.setText(messaggio_chat.getAuthor());
viewHolder.risponditore.setVisibility(View.VISIBLE);
}else{
viewHolder.risponditore.setText("");
viewHolder.risponditore.setVisibility(View.GONE);
}
viewHolder.txtMessage.setText(messaggio_chat.getText());
viewHolder.ora.setText(getTimeTextOra(messaggio_chat.getTime()));
if(!messaggio_chat.isIncoming()
&& messaggio_chat == (ChatMessage)getItem(getCount()-1)
&& messaggio_chat.getAlertVisibility()){
viewHolder.alertButton.setVisibility(View.VISIBLE);
}
return convertView;
}
public void add(ChatMessage message) {
lista_messaggi_chat.add(message);
}
public void add(List<ChatMessage> messages) {
Collections.sort(messages, new Comparator<ChatMessage>() {
@Override
public int compare(ChatMessage lhs, ChatMessage rhs) {
return lhs.getTime().compareTo(rhs.getTime());
}
});
this.lista_messaggi_chat = messages;
}
private static class ViewHolder {
public TextView txtMessage;
public RelativeLayout content;
public RelativeLayout contentWithBG;
public TextView ora;
public TextView giorno;
public RelativeLayout contenitoreGiorno;
public TextView risponditore;
public TableRow rigaOra;
public TableLayout tabellaMessaggioOra;
public Button alertButton;
//public TextView alertMessage;
}
private ViewHolder createViewHolder(View v) {
ViewHolder holder = new ViewHolder();
holder.txtMessage = (TextView) v.findViewById(R.id.txtMessage);
holder.content = (RelativeLayout) v.findViewById(R.id.content);
holder.contentWithBG = (RelativeLayout)
v.findViewById(R.id.contentWithBackground);
holder.ora = (TextView) v.findViewById(R.id.ora);
holder.giorno = (TextView) v.findViewById(R.id.giorno);
holder.contenitoreGiorno = (RelativeLayout)
v.findViewById(R.id.contenitore_giorno);
holder.risponditore = (TextView) v.findViewById(R.id.risponditore);
holder.rigaOra = (TableRow) v.findViewById(R.id.rigaOra);
holder.tabellaMessaggioOra = (TableLayout)
v.findViewById(R.id.tabellaMessaggioOra);
holder.alertButton = (Button) v.findViewById(R.id.alert_button);
//holder.alertMessage = (TextView) v.findViewById(R.id.alert_msg);
return holder;
}
}
在getView適配器僅如果消息不icoming按鈕應該變得可見,如果是最後,如果messaggio_chat.getAlertVisibility()返回true。我在處理髮送錯誤的活動中將其設置爲true,我將消息添加到列表中,並且該按鈕是可見的。
我的問題是,該按鈕變得不僅看到最後一條消息,而且隨機看到其他消息。
你能告訴我我做錯了什麼嗎?
預先感謝您。
編輯:
更具體地講,用戶可以發送錯誤發送多條消息,並且可以有更多的消息,所以我需要更多的行設置可見警報按鈕。然後,如果用戶點擊一個提醒按鈕,他可以重新發送一條消息(這還沒有實施,但將是下一個)。
將getView()方法的最終int位置更改爲int位置。 – Ankita
@Ankita仍然有隨機警報按鈕可見...謝謝 –