我有两个EditText,我想在单击按钮时获取edittext的值,并在自定义布局中显示该值。我加入了MainActivity.java和CustomAdapter.java,并以不同的方式尝试了很多次,但是都没有用。请帮忙。
MainActivity.java
add_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String ed_text_1=credit_edt.getText().toString();
String ed_text_2=gpa_edt.getText().toString();
double d_value_1=Double.parseDouble(ed_text_1);
double d_value_2=Double.parseDouble(ed_text_2);
double result=d_value_1+d_value_2;
CustomAdapter adapter=new CustomAdapter(MainActivity.this,d_value_1,d_value_2,result);
listView.setAdapter(adapter);
credit_edt.setText("");
gpa_edt.setText("");
}
});
CustomAdapter.java
class CustomAdapter extends BaseAdapter {
double str1;
double str2;
double ans;
Context context;
private LayoutInflater inflater;
public CustomAdapter(Context context,double str1, double str2, double ans) {
this.context = context;
this.str1 = str1;
this.str2 = str2;
this.ans = ans;
}
@Override
public int getCount() {
return 0;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null)
{
inflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView=inflater.inflate(R.layout.output_layout,parent,false);
}
TextView txt1=(TextView) convertView.findViewById(R.id.crt_c_id);
TextView txt2=(TextView) convertView.findViewById(R.id.gp_c_id);
TextView txt3=(TextView) convertView.findViewById(R.id.tl_c_id);
String s1=Double.toString(str1);
String s2=Double.toString(str2);
String s3=Double.toString(ans);
txt1.setText(s1);
txt2.setText(s2);
txt3.setText(s3);
return convertView;
}
答案 0 :(得分:1)
检查您的自定义适配器参数
通过活动传递值
CustomAdapter adapter=new CustomAdapter(MainActivity.this,d_value_1,d_value_2,result);
并将其插入适配器
CustomAdapter(double[] str1, double[] str2, double[] ans, Context context)
检查参数数据类型和顺序:- 在活动中,您将上下文作为第一个参数传递,在适配器中,您将在最后一个参数传递
快乐编码!