我根据需要定制了listview适配器。
我想要实现什么?
我的应用程序将通过外部阅读器发送serialno和
1。如果列表视图中已存在序列号,则与其关联的图像视图应更改。 2. 如果serialno不可用,则应在listview上添加(此部分已完成。)
现在我想检查列表视图中现有项目的序列号并更改其imageview。我怎么实现它?
此外,我想获得具有特定imageView的所有listview项目?
activity_scan.xml
<ListView
android:id="@+id/scan_loc_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>
activity_listview.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="82dp"
android:padding="@dimen/activity_horizontal_margin"
android:background="?android:selectableItemBackground"
android:orientation="vertical"
android:descendantFocusability="blocksDescendants"
>
<ImageView
android:id="@+id/scan_img_view"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerVertical="true"
android:layout_alignParentStart="true"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toEndOf="@+id/scan_img_view"
android:layout_marginStart="@dimen/activity_horizontal_margin"
android:orientation="vertical">
<TextView
android:id="@+id/scan_serial"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000"
android:textSize="18sp"/>
</LinearLayout>
</RelativeLayout>
ScanItem.Java
public class ScanItem {
private int imageId;
private String serial;
// Getter and Setter Methods
............
}
ScanAdapter.java
public class ScanAdapter extends ArrayAdapter<ScanItem> {
Context context;
public ScanViewAdapter(Context context, int resourceId,List<ScanItem> items) {
super(context, resourceId, items);
this.context = context;
}
private class ViewHolder {
ImageView imageView;
TextView txtSerial;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
ScanItem rowItem = getItem(position);
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.activity_listview, null);
holder = new ViewHolder();
holder.txtSerial = (TextView) convertView.findViewById(R.id.scan_serial);
holder.imageView = (ImageView) convertView.findViewById(R.id.scan_img_view);
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();
holder.txtSerial.setText(rowItem.getSerial());
holder.imageView.setImageResource(rowItem.getImageId());
return convertView;
}
}
MainActivity.java
ScanAdapter assetListAdapter = new ScanAdapter(getActivity(), R.layout.activity_listview, items); // items contains array of ScanItem objects
ListView androidListView = (ListView) getView().findViewById(R.id.scan_loc_list);
androidListView.setAdapter(assetListAdapter);
答案 0 :(得分:0)
我自己找到了答案
int count = androidListView.getAdapter().getCount();
String[] listData = new String[count];
for (int i = 0; i < count; i++) {
ScanItem sc = (ScanItem) androidListView.getAdapter().getItem(i);
// sc will have all the required data
}