我有2个类,第一个是FragmentA
,第二个是ListRequestAdapter
(ArrayAdapter)。我想从我的ImageView
更改ListRequestAdapter
中的FragmentA
。但是当我在NullPointerException
中声明ImageView时,我总是收到错误FragmentA
。我怎么能这样做?
这是我的代码:
FragmentA.java类:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_item_list_request, container, false);
setHasOptionsMenu(true);
listPhoto = (ImageView) getView().findViewById(R.id.listPhoto);
listPhoto.setImageResource(R.drawable.pastel_blue);
}
ListRequestAdapter.java类:
public class ListRequestAdapter extends ArrayAdapter<ListRequestItem> {
ImageView listPhoto;
public ListRequestAdapter(Context context, List<ListRequestItem> items)
{
super(context, R.layout.style_fragment_list_request, items);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if(convertView == null) {
// inflate the GridView item layout
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.style_fragment_list_request, parent, false);
// initialize the view holder
viewHolder = new ViewHolder();
viewHolder.tvTanggal = (TextView) convertView.findViewById(R.id.tvTanggalRequest);
viewHolder.tvTipe = (TextView) convertView.findViewById(R.id.tvTipeRequest);
viewHolder.tvStatus = (TextView) convertView.findViewById(R.id.tvStatus);
viewHolder.permitid = "";
convertView.setTag(viewHolder);
} else {
// recycle the already inflated view
viewHolder = (ViewHolder) convertView.getTag();
}
// update the item view
ListRequestItem item = getItem(position);
viewHolder.tvTanggal.setText(item.tanggal);
viewHolder.tvTipe.setText(item.tipe);
viewHolder.tvStatus.setText(item.status);
viewHolder.permitid = item.permitid;
return convertView;
}
private static class ViewHolder {
TextView tvTanggal;
TextView tvTipe;
TextView tvStatus;
String permitid;
}
}
列出项目的布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:tag="reqtag"
android:id="@+id/rlListRequestWorkflow"
>
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:weightSum="1"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="0dp"
android:layout_weight="0.2"
android:orientation="vertical"
android:layout_height="wrap_content">
<!--ImageView here-->
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/listPhoto"
android:layout_width="65dp"
android:layout_height="63dp"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:src="@drawable/ic_menu_camera"
android:layout_marginTop="10dp" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_weight="0.8"
android:orientation="vertical"
android:layout_height="wrap_content">
<!--All textViews here-->
<TextView
android:layout_width="100dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Type"
android:textSize="18dp"
android:fontFamily="sans-serif"
android:textColor="@color/black"
android:id="@+id/tvTipeRequest"
android:width="130dp" />
<TextView
android:layout_width="100dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Date"
android:fontFamily="sans-serif"
android:id="@+id/tvTanggalRequest"
android:textSize="15dp"
android:width="130dp" />
<TextView
android:layout_width="100dp"
android:layout_weight="1"
android:layout_height="50dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/black"
android:text="Status"
android:textSize="15dp"
android:fontFamily="sans-serif"
android:id="@+id/tvStatus"
android:layout_column="38" />
</LinearLayout>
</LinearLayout>
答案 0 :(得分:2)
您的回答无法帮助您在ListView中更新ImageView。您只需创建一个与任何内容无关的新视图(anotherView)。要在ListView中更新ImageView,您应该在DataList中包含一些相关信息(ImageID或ImagePath等)。设置/更新信息后,只需在Fragment中调用adapter.notifyDataSetChanged()。
此外,如果没有数据,您可以修改ListRequestAdapter以隐藏ImageView,如下所示:
public class ListRequestAdapter extends ArrayAdapter<ListRequestItem> {
public ListRequestAdapter(Context context, List<ListRequestItem> items){
super(context, R.layout.style_fragment_list_request, items);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if(convertView == null) {
// inflate the GridView item layout
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.style_fragment_list_request, parent, false);
// initialize the view holder
viewHolder = new ViewHolder();
viewHolder.imgPhoto = (ImageView) convertView.findViewById(R.id.listPhoto);
viewHolder.tvTanggal = (TextView) convertView.findViewById(R.id.tvTanggalRequest);
viewHolder.tvTipe = (TextView) convertView.findViewById(R.id.tvTipeRequest);
viewHolder.tvStatus = (TextView) convertView.findViewById(R.id.tvStatus);
viewHolder.permitid = "";
convertView.setTag(viewHolder);
} else {
// recycle the already inflated view
viewHolder = (ViewHolder) convertView.getTag();
}
// update the item view
ListRequestItem item = getItem(position);
// Setup ImageView accord to DataList,
// may be imageID from DataList.
if(imageID == null) viewHolder.imgPhoto.setVisibility(View.INVISIBLE);
else{
viewHolder.imgPhoto.setImageResource(item.imageID);
viewHolder.setVisibility(VIEW_VISIBLE);
}
viewHolder.tvTanggal.setText(item.tanggal);
viewHolder.tvTipe.setText(item.tipe);
viewHolder.tvStatus.setText(item.status);
viewHolder.permitid = item.permitid;
return convertView;
}
private static class ViewHolder {
ImageView imgPhoto;
TextView tvTanggal;
TextView tvTipe;
TextView tvStatus;
String permitid;
}
}
希望这有帮助!
答案 1 :(得分:0)
通过像这样膨胀包含ImageView
的布局来解决问题:
View anotherView = inflater.inflate(R.layout.style_fragment_list_request,container,false);
imgView = (ImageView)anotherView.findViewById(R.id.listPhoto);`