2016-05-04 2 views
1

Я Создание одного медицинского примененияНазначение изображений через адаптер

Я хочу показать ListView для Prescription включая медицины имени, сроки и значения Image.all поступают из базы данных, за исключением изображений.

Значит, если тип медицины из базы данных - это планшет, чем я хочу показать ImgA, если тип медицины, если инъекция, то я хочу показать ImgB в ListView.

Вот моя активность:

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() для настройки изображения:

Вот мой 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) 

Если удалить часть изображения из моего кода, код работает fine..it дает мне проблему при настройке 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

дубликата вопрос [ 'java.lang.NullPointerException'] (HTTP: // StackOverflow .com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – ELITE

+0

попробуйте использовать библиотеку picaso – Abhishek

+0

put 'public Integer [] Images = {R.drawable. таблетки, R.drawable.injection};» в onCreate .. может ли это решить исключение nullpointwr – iroiroys

ответ

0

Вы также можете проверить в классе адаптера, как этот

if(MedicineType==1) 
{ 
    vHolder.imgMedicineType.setImageResource(imageA); 
} 
else if(MedicineType==0) 
{ 
    vHolder.imgMedicineType.setImageResource(imageB); 
} 
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; 
    } 
} 

Здесь is My Activity:

это, как я Назначение Изображения:

ImageId = new int[Length]; 
        if(MedicineType == 1){ImageId[i] = Images[0];} 
        else if(MedicineType == 0){ImageId[i]= Images[1]; 
+0

nope .. не работает для меня .. дает мне такую ​​же проблему .. – Pranita

+0

вы можете публиковать основной код операции ... –

+0

отредактировали часть основного кода операции в самом вопросе – Pranita

1

Я получил решение этой проблемы

то, что я сделал: Я взял Integer Список_массивов для хранения моих изображений

В моей основной деятельности

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 Arraylist для изображения

это решить мою проблему