【问题标题】:Only one time last item of array is showing in listView in AndroidAndroid 的 listView 中仅显示最后一次数组的最后一项
【发布时间】:2017-04-24 07:03:52
【问题描述】:

我正在尝试在 ListView 中动态添加项目,但是当我使用自定义适配器时,只有数组的最后一个元素与 ListView 绑定。

public class CustomAdapterForChat extends BaseAdapter 
{
    Context context;
    ArrayList<String>  message;
    LayoutInflater inflter;
    public CustomAdapterForChat(Context applicationContext, ArrayList<String> 
    message) 
    {//, int[] images,String[] statuses,String[] PhoneNumbers
        this.message=message;
        inflter = (LayoutInflater.from(applicationContext));
    }

    @Override
    public int getCount() {
        return message.size();
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        view = inflter.inflate(R.layout.senderlayout, null);
        TextView SenderMesssage=(TextView)view.findViewById(R.id.SenderMesssage);
        TextView time=(TextView)view.findViewById(R.id.timeOfmessage);
        SenderMesssage.setText(message.get(i));
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); 
        Date date = new Date();
        time.setText(date.toString());
        return view;
    }
}

这是我调用自定义适配器类的代码,我的数组大小为 5,在自定义适配器构造函数中我得到了正确的值,但在 getView 方法中,i 仅在 0 中传递。

public class ChatPage extends ListActivity {

     String listItem[]={"Dell Inspiron", "HTC One X", "HTC Wildfire S", 
                        "HTC Sense", "HTC Sensation XE"};
     EditText et;
     List<String> arrayList;
     ImageView button;

     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_chat_page);
         et = (EditText) findViewById(R.id.writemessage);
         ArrayList values = new ArrayList();
         for (int i = 0; i < listItem.length; i++) {
             values.add(listItem[i]);
         }
         CustomAdapterForChat adapter = new CustomAdapterForChat(this,values);
         setListAdapter(adapter);
     }

     public void onClick(View view) {
          CustomAdapterForChat adapter = (CustomAdapterForChat) getListAdapter();
          String device;
          ArrayList myList = new ArrayList();
          for (int i=0;i<adapter.getCount();i++)
          {
              myList.add(adapter.getItem(i));
          }
          myList.add(et.getText().toString());
          CustomAdapterForChat adapternew = new CustomAdapterForChat(this,myList);
          setListAdapter(adapternew);

      }

 }

ListView.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="tahatechno.tahatechno.ChatPage">
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffff"
    android:orientation="vertical"
    >

    <ScrollView
        android:layout_width="match_parent"
        android:layout_weight="20"
        android:layout_height="wrap_content">

        <ListView
        android:id="@id/android:list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:stackFromBottom="true">


        </ListView>
    </ScrollView>



    <include
        layout="@layout/type_message_area"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="bottom"
        />
 </LinearLayout>
 </android.support.constraint.ConstraintLayout>

Sender.XML

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" android:layout_width="match_parent"
 android:paddingLeft="@dimen/paddinfleftchatpage_"
 android:paddingRight="@dimen/padding_right_chat_page"
 android:layout_height="match_parent">
 <LinearLayout
    android:id="@+id/SenderLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/SenderMesssage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/layout_marginTop_chat_page"
        android:layout_weight="6"
        android:background="@drawable/rounded_corner"
        android:padding="@dimen/text_view_padding"
        android:text="Android charting application xml ui design tutorial 
 with example. Android charting application xml ui design tutorial with 
example. Android charting application xml ui design tutorial with example. 
Android charting application xml ui design tutorial with example."
        android:textColor="#000" />



   </LinearLayout>
    <LinearLayout
    android:id="@+id/time"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="right"
    android:orientation="vertical">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:id="@+id/timeOfmessage"
        android:text="5:25 am"/>
    </LinearLayout>
  </LinearLayout>

【问题讨论】:

  • 显示你的代码loop
  • ArrayList 消息的大小是多少?
  • 请将您的设计 xml 文件和 java 活动放在创建和附加适配器对象的位置。
  • 请看我的编辑,我有更新 xml 和 java 活动

标签: android listview


【解决方案1】:

你的循环是错误的。每次运行循环时,您都将相同的项目插入到列表中。制作要插入到列表中的对象的单例实例。或者粘贴循环代码

【讨论】:

  • 真的吗?这不是答案,这必须是评论
  • 您的循环错误。每次运行循环时,您都将相同的项目插入到列表中。制作要插入到列表中的对象的单例实例。或者按照你的说法粘贴循环代码 OK SIR .. @ShayanPourvatan
  • 请检查我的编辑..我也添加了循环代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-30
  • 1970-01-01
  • 1970-01-01
  • 2019-10-31
  • 1970-01-01
相关资源
最近更新 更多