我正在制作一份医疗申请
我想显示ListView for Prescription,包括Medicine Name,Timing和Image.all值来自数据库,除了图像。
意味着如果数据库中的医学类型是平板电脑而不是我想要显示ImgA如果注射医学类型,那么我想在ListView中显示ImgB。
这是我的活动:
public Integer[] Images = {R.drawable.tablet,R.drawable.injection};
public Integer[] ImageId = new Integer[]{} ;
这是我分配图像的方式:
Cursor cur = dop.getPrescriptionData();
Integer Length = cur.getCount();
if(cur!= null && cur.getCount()>0)
{
int i = 0;
if(cur.moveToFirst()){
do {
ImageId = new Integer[Length];
if(MedicineType == 1){ImageId[i] = Images[0];}
else if(MedicineType == 0){ImageId[i]= Images[1];
//other Parameters
i++;
}
while (cur.moveToNext());
}
}
}
将值传递给我的适配器:
PrescriptionAdapter pAdapter = new PrescriptionAdapter(PrescriptionActivity.this,ImageId,MedicineName,MedicineType,MedicineTimings,subMedicineTiming);
lstPrescription.setAdapter(pAdapter);
这是我的适配器:
public class PrescriptionAdapter extends BaseAdapter {
public Context context;
public ArrayList<String>MedicineNames;
public ArrayList<String>MedicineType;
public ArrayList<String>MedicineTimings;
public ArrayList<String>subMedicineTimings;
public Integer[] ImageId = new Integer[]{};
public PrescriptionAdapter(Context context,Integer[] ImageId,ArrayList<String>MedicineNames,ArrayList<String>MedicineType,
ArrayList<String>MedicineTimings,ArrayList<String>subMedicineTimings)
{
this.context = context;
this.ImageId = ImageId;
this.MedicineNames = MedicineNames;
this.MedicineType = MedicineType;
this.MedicineTimings = MedicineTimings;
this.subMedicineTimings = subMedicineTimings;
}
public int getCount(){return MedicineNames.size();}
public Object getItem(int Position){return null;}
public long getItemId(int Position){return 0;}
public class viewHolder{
TextView tvMedicineName;
ImageView imgMedicineType;
TextView tvMedicineTiming;
TextView tvSubMedicineTiming;
}
@Override
public View getView(int Position,View Child,ViewGroup Parent)
{
try
{
viewHolder vHolder;
LayoutInflater inflator;
if(Child == null)
{
inflator = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Child = inflator.inflate(R.layout.list_prescription_row,Parent,false);
vHolder = new viewHolder();
vHolder.tvMedicineName = (TextView)Child.findViewById(R.id.txtMedicineName);
vHolder.tvMedicineTiming = (TextView)Child.findViewById(R.id.txtMedicineTiming);
vHolder.tvSubMedicineTiming = (TextView)Child.findViewById(R.id.txtSubMedicineTiming);
vHolder.imgMedicineType = (ImageView)Child.findViewById(R.id.imgPrescription);
Child.setTag(vHolder);
}
else {vHolder = (viewHolder)Child.getTag();}
vHolder.tvMedicineName.setText(MedicineNames.get(Position));
vHolder.tvMedicineTiming.setText(MedicineTimings.get(Position));
vHolder.tvSubMedicineTiming.setText(subMedicineTimings.get(Position));
vHolder.imgMedicineType.setImageResource(ImageId[Position]);
return Child;
}
catch (Exception ex)
{
ex.printStackTrace();
return null;
}
}
}
我在getView()中遇到设置Image的问题:
这是我的Logcat:
05-04 10:58:21.694 15911-15911/com.example.pranita.niramay E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
at android.widget.ListView.measureScrapChild(ListView.java:1168)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1251)
at android.widget.ListView.onMeasure(ListView.java:1160)
at android.view.View.measure(View.java:12773)
at android.widget.RelativeLayout.measureChild(RelativeLayout.java:579)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:392)
at android.view.View.measure(View.java:12773)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4788)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
at android.view.View.measure(View.java:12773)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4788)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1374)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:663)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
at android.view.View.measure(View.java:12773)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4788)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
at android.view.View.measure(View.java:12773)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4788)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1374)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:663)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
at android.view.View.measure(View.java:12773)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4788)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2110)
at android.view.View.measure(View.java:12773)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1147)
at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2642)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:4624)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
at dalvik.system.NativeStart.main(Native Method)
如果我从代码中删除了图像部分,代码工作正常......在设置ImageView时出现问题。
如何解决它????
这是我的xml文件
<!--language:xml-->
><?xml version="1.0" encoding="utf-8"?><!DOCTYPE LinearLayout>
><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/rlListPrescriptionRow"
android:layout_margin="5dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imgPrescription"
android:layout_margin="5dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/imgPrescription"
android:typeface="serif"
android:text=""
android:textSize="20sp"
android:textColor="@color/black"
android:layout_marginLeft="5dp"
android:id="@+id/txtMedicineName"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/txtMedicineName"
android:id="@+id/txtMedicineTiming"
android:layout_alignLeft="@+id/txtMedicineName"
android:textSize="18sp"
android:typeface="serif"
android:textColor="@color/black"
android:text=""
android:layout_marginTop="5dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txtSubMedicineTiming"
android:layout_below="@+id/txtMedicineTiming"
android:textColor="@color/black"
android:textSize="16sp"
android:text=""
android:layout_alignLeft="@+id/txtMedicineTiming"
android:typeface="serif"
> android:layout_marginTop="2dp"/>
></RelativeLayout>
></LinearLayout>
答案 0 :(得分:1)
我找到了解决这个问题的方法
我所做的是:我使用Integer Arraylist来存储我的图像
在我的主要活动中
public int[] Images = {R.drawable.imgA,R.drawable.imgB};
public ArrayList<Integer>ImageId = new ArrayList<Integer>();
int i = 0;
if(cur.moveToFirst()){
if(MedicineType == 1)
{
ImageId.add(Images[0]);
}
else if(MedicineType == 0)
{
ImageId.add(Images[1]);
} } while(cur.moveToNext());
也在myAdapter中: 我jst将Integer Array更改为Integer Arraylist for Image
这解决了我的问题
答案 1 :(得分:0)
你也可以像这样检查适配器类
if(MedicineType==1)
{
vHolder.imgMedicineType.setImageResource(imageA);
}
else if(MedicineType==0)
{
vHolder.imgMedicineType.setImageResource(imageB);
}
答案 2 :(得分:0)
请尝试以下代码: -
public PrescriptionAdapter(Context context,Integer[] ImageId,ArrayList<String>MedicineNames,ArrayList<String>MedicineType,
ArrayList<String>MedicineTimings,ArrayList<String>subMedicineTimings)
{
this.context = context;
this.ImageId = ImageId;
this.MedicineNames = MedicineNames;
this.MedicineType = MedicineType;
this.MedicineTimings = MedicineTimings;
this.subMedicineTimings = subMedicineTimings;
}
public int getCount(){return MedicineNames.size();}
public Object getItem(int Position){return null;}
public long getItemId(int Position){return 0;}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView=inflater.inflate(R.layout.list_prescription_row, null,true);
tvMedicineName = (TextView)rowView.findViewById(R.id.txtMedicineName);
tvMedicineTiming = (TextView)rowView.findViewById(R.id.txtMedicineTiming);
tvSubMedicineTiming = (TextView)rowView.findViewById(R.id.txtSubMedicineTiming);
imgMedicineType = (ImageView)rowView.findViewById(R.id.imgPrescription);
tvMedicineName.setText(MedicineNames.get(Position));
tvMedicineTiming.setText(MedicineTimings.get(Position));
tvSubMedicineTiming.setText(subMedicineTimings.get(Position));
imgMedicineType.setImageResource(ImageId[Position]);
return rowView;
}
}
以下是我的活动:
public int[] Images = {R.drawable.tablet,R.drawable.injection};
public int[] ImageId = new int[]{} ;
这是我分配图像的方式:
ImageId = new int[Length];
if(MedicineType == 1){ImageId[i] = Images[0];}
else if(MedicineType == 0){ImageId[i]= Images[1];