遍歷listview裡面全部(可見和不可見)item,從而獲取item的資訊最簡單的方法
阿新 • • 發佈:2019-02-12
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); adapter = new MyAdapter(doneNumber1); listView.setAdapter(adapter); button.setOnClickListener(this);//點選獲取listview裡面全部item } <span style="white-space:pre"> </span>@Override <span style="white-space:pre"> </span>public void onClick(View v) { for (int i=0;i<adapter.getCount();i++){ RelativeLayout relativeLayout= (RelativeLayout) listView.getAdapter().getView(i,null,null);//RelativeLayout是listview的item父佈局 <span style="white-space:pre"> </span>TextView text= (TextView) relativeLayout.getChildAt(0);//父佈局裡面的子控制元件是第幾個就填第幾個getChildAt Log.e("asddas",listbutton.getText().toString()); } break; }
XML 佈局如下
<pre name="code" class="html"><?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" android:paddingLeft="10dp"> <TextView android:id="@+id/tv_gongxudanhao" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:paddingTop="10dp" android:text="單號:" android:textColor="#f0f" android:textSize="16dp" android:textStyle="bold" /> </RelativeLayout>