自定义适配器反向放置数据

时间:2014-08-29 06:17:18

标签: android eclipse adapter

我正在使用自定义适配器和列表视图,但是它多次显示数据并且数据被反转,我的意思是当我添加一行时,它显示在列表视图的顶部而不是最后一个 当我使用阵列适配器时,我没有发现任何问题 请帮助 这是我的代码:

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Locale;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class ListViewRowChatAdapter extends ArrayAdapter<Spannable> {

    Context context;
   int layoutResourceId;

   ArrayList<Spannable> data=new ArrayList<Spannable>();

   public ListViewRowChatAdapter(Context context, int layoutResourceId, ArrayList<Spannable> data) {
       super(context, layoutResourceId, data);
       this.layoutResourceId = layoutResourceId;
       this.context = context;
       this.data = data;
     ;
   }

   @Override
   public View getView(int position, View convertView, ViewGroup parent) {
       View row = convertView;
       ImageHolder holder = null;

       if(row == null)
       {
           LayoutInflater inflater = ((Activity)context).getLayoutInflater();
           row = inflater.inflate(layoutResourceId, parent, false);

           Spannable tx=data.get(data.size()-1);

           holder = new ImageHolder();
           holder.txtTitle = (TextView)row.findViewById(R.id.text_chat);
           holder.txtTitle.setText(tx);

           row.setTag(holder);
       }
       else
       {
           holder = (ImageHolder)row.getTag();
       }

       return row;
   }

   static class ImageHolder
   {
       TextView txtTitle;
   }
}





    list = new ArrayList<Spannable>();
    itemsAdapter =new ListViewRowChatAdapter(this, R.layout.chat_shape, list);
    listView = (ListView) findViewById(android.R.id.list);
    listView.setAdapter(itemsAdapter);


public void SendChat(View v)
{
    Spannable text = et_chatInput.getText();
        if(text.length() > 0)
        {
            itemsAdapter.add(text);

        }

    et_chatInput.setText("");
}

2 个答案:

答案 0 :(得分:1)

您需要在此处添加内容:

   if(row == null)
   {
       LayoutInflater inflater = ((Activity)context).getLayoutInflater();
       row = inflater.inflate(layoutResourceId, parent, false);


       holder = new ImageHolder();
       holder.txtTitle = (TextView)row.findViewById(R.id.text_chat);
       // remove code here
       row.setTag(holder);
   }
   else
   {
       holder = (ImageHolder)row.getTag();
   }
      // add code here
       Spannable tx=data.get(data.size()-1);
       holder.txtTitle.setText(tx);

答案 1 :(得分:0)

问题解决了谢谢!

@Override
 public View getView(int position, View convertView, ViewGroup parent) {
   View row = convertView;
   ImageHolder holder = null;

   if(row == null)
   {
       LayoutInflater inflater = ((Activity)context).getLayoutInflater();
       row = inflater.inflate(layoutResourceId, parent, false);



       holder = new ImageHolder();
       holder.txtTitle = (TextView)row.findViewById(R.id.text_chat);


       row.setTag(holder);
   }
   else
   {
       holder = (ImageHolder)row.getTag();
   }

Spannable tx=data.get(position);
holder.txtTitle.setText(tx);
   return row;
}