2017-03-09 73 views
0

嗨,我是新來創建一個自定義適配器,我目前在大學。 我很難找出爲什麼我的新Bmi(Bmi bmi)在我的基礎類方法中從未被使用過。這裏的代碼請看看你能看到我做錯了什麼?非常感謝新來創建一個自定義適配器

public class BaseActivity extends AppCompatActivity 
    { 
    public int   height; 
    public int   weight; 
    public int bmiTotals = 0; 
    public static List<Bmi> bmis = new ArrayList<Bmi>(); 


    public boolean newBmi(Bmi bmi) { 

    boolean nonValue = (height * weight) < 0; 
    if(!nonValue) { 

     if (height > 0 && weight > 0) { 
      int bm = height/100; 
      int bn = weight; 

      bmiTotals = bn/(bm * bm); 
      bmis.add(bmi); 

      bmiTotals = bmi.bmiTotal; 


     } else { 
      Toast.makeText(this, "No values entered!", Toast.LENGTH_SHORT).show(); 

     } 
     } 
     return nonValue; 
    } 

    ///////////////////////////////////////This is in a separate class called Bmi 
    public class Bmi { 
    public int height; 
    public int weight; 
    public int bmiTotal; 


    public Bmi (int height, int weight, int bmiTotal) 
    { 
     this.height = height; 
     this.weight = weight; 
     this.bmiTotal = bmiTotal; 
    } 

}

+1

它看起來像什麼事也沒叫你'newBmi()'方法。我認爲你想要像'bmis.add(newBmi());'在某處。我想也是,從來沒有電話給'新Bmi(176,176,200)'或是什麼。所以,你永遠不會創建Bmi對象,它們也不會被放入列表中。 – Jameson

+0

非常感謝您的幫助 – IrishProgrammer

回答

0

試試這個,

public class SMSListAdapter extends BaseAdapter 
{ 

    private Context mContext; 
    Cursor cursor; 
    public SMSListAdapter(Context context,Cursor cur) 
    { 
      super(); 
      mContext=context; 
      cursor=cur; 

    } 

    public int getCount() 
    { 
     // return the number of records in cursor 
     return cursor.getCount(); 
    } 

    // getView method is called for each item of ListView 
    public View getView(int position, View view, ViewGroup parent) 
    { 
        // inflate the layout for each item of listView 
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
        view = inflater.inflate(R.layout.listview_each_item, null); 

        // move the cursor to required position 
        cursor.moveToPosition(position); 

        // fetch the sender number and sms body from cursor 
        String senderNumber=cursor.getString(cursor.getColumnIndex("address")); 
        String smsBody=cursor.getString(cursor.getColumnIndex("body")); 

        // get the reference of textViews 
        TextView textViewConatctNumber=(TextView)view.findViewById(R.id.textViewSMSSender); 
        TextView textViewSMSBody=(TextView)view.findViewById(R.id.textViewMessageBody); 

        // Set the Sender number and smsBody to respective TextViews 
        textViewConatctNumber.setText(senderNumber); 
        textViewSMSBody.setText(smsBody); 


        return view; 
    } 

    public Object getItem(int position) { 
     // TODO Auto-generated method stub 
     return position; 
    } 

    public long getItemId(int position) { 
     // TODO Auto-generated method stub 
     return position; 
    } 
} 

完整的示例:http://www.learn-android-easily.com/2013/06/listview-with-custom-adapter.html

+0

謝謝,但我被告知要爲我的應用程序的bmi部分使用自定義適配器,我已經爲另一部分實施了數據庫。我的講師希望以這種方式爲我的項目 – IrishProgrammer

+0

創建適配器是一樣的,您可以傳遞您的列表並根據您的要求進行修改。 –

+0

好的非常感謝 – IrishProgrammer