Java 创建自定义适配器的新增功能
您好,我是新创建的自定义适配器,我目前在大学。 我发现很难弄明白为什么我的基本分类方法中的新Bmi(Bmi-Bmi)从未被使用。这是代码请看你能看到我做错了什么吗?非常感谢Java 创建自定义适配器的新增功能,java,android,Java,Android,您好,我是新创建的自定义适配器,我目前在大学。 我发现很难弄明白为什么我的基本分类方法中的新Bmi(Bmi-Bmi)从未被使用。这是代码请看你能看到我做错了什么吗?非常感谢 public class BaseActivity extends AppCompatActivity { public int height; public int weight; public int bmiTotals = 0;
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;
}
公共类BaseActivity扩展了AppCompativity
{
公众内部高度;
公共权重;
公共整数=0;
公共静态列表bmis=new ArrayList();
公共管理信息(Bmi-Bmi){
布尔非值=(高度*重量)<0;
如果(!非值){
如果(高度>0和重量>0){
int bm=高度/100;
int bn=重量;
BMI总计=bn/(bm*bm);
添加体重指数(bmi);
bmi总计=bmi.bmi总计;
}否则{
Toast.makeText(这是“未输入值!”,Toast.LENGTH_SHORT.show();
}
}
返回非值;
}
///////////////////////////////////////这是在一个单独的类称为体重指数
公共类Bmi{
公众内部高度;
公共权重;
公共卫生总费用;
公共Bmi(身高、体重、体重)
{
高度=高度;
重量=重量;
this.bmiTotal=bmiTotal;
}
}试试这个
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;
}
}
完整示例:看起来好像从来没有调用过你的
newBmi()
方法。我想你应该像bmis.add(newBmi());
某个地方。我想也从来没有调用过newBmi(176176200)
或其他。因此,您从未创建Bmi对象,也从未将它们放入列表中。非常感谢您的帮助谢谢您,但我被告知要为我的应用程序的Bmi部分使用自定义适配器,我已经为另一部分实现了数据库。我的讲师希望我的项目采用这种方式创建适配器相同,您可以通过r根据您的要求列出并修改。