Android - 适配器创建多行而不是一行
我有一个 GUI,供用户在插入 2 个值后添加行。将添加一行,同时显示用户添加的 2 个值。但显然它创建的行数比预期的要多。例如,单击 1 次可以创建 5 行,而本应仅创建 1 行。
下一个问题是适配器用较新的值覆盖旧值,而不是创建新行。
总结一下面临的问题:
1) 适配器创建多行而不是一行。
2) 适配器用新行覆盖旧行。
有什么解决办法吗?
main.java
public class BillSplitter extends Activity{
ListView list;
Button calculate;
EditText result;
String total;
String name;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Spinner spinner = (Spinner)findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.spinner_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
list = (ListView)findViewById(R.id.noteList);
final EditText price = (EditText)findViewById(R.id.price);
final EditText name1 = (EditText)findViewById(R.id.name);
Button btnSimple = (Button)findViewById(R.id.btnSimple);
btnSimple.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
double totalPrice = Double.parseDouble(price.getText().toString());
int position = spinner.getSelectedItemPosition();
String namename = "";
String totaltotal = "";
namename = name1.getText().toString();
if(position == 0)
{
totalPrice = totalPrice * 1.07;
roundTwoDecimals(totalPrice);
total = String.valueOf(totalPrice);
System.out.println(total);
//result.setText(total);
}
else
{
totalPrice = (totalPrice * 1.1)*1.07;
roundTwoDecimals(totalPrice);
total = String.valueOf(totalPrice);
System.out.println(total);
//result.setText(total);
}
/*noteList.add(0, total);
System.out.println(total);
name1.setText("");
price.setText("");
aa.notifyDataSetChanged();*/
price.setText("");
name1.setText("");
Adapter adapter = new Adapter(main.this, name, total);
list.setAdapter(adapter);
}
});
}
void roundTwoDecimals(double d) {
DecimalFormat twoDForm = new DecimalFormat("#.##");
twoDForm.format(d);
}
}
适配器.java
public class Adapter extends BaseAdapter {
private Activity activity;
private String result;
private String text;
private static LayoutInflater inflater=null;
public Adapter(Activity a, String d, String t) {
activity = a;
text=d;
result = t;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return result.length();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public static class ViewHolder{
public TextView text;
public EditText edittext;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
ViewHolder holder;
if(convertView==null){
vi = inflater.inflate(R.layout.custom_list_item, null);
holder=new ViewHolder();
holder.text=(TextView)vi.findViewById(R.id.nametv);;
holder.edittext=(EditText)vi.findViewById(R.id.result);;
vi.setTag(holder);
}
else
holder=(ViewHolder)vi.getTag();
holder.text.setText(text);
holder.edittext.setText(result);
return vi;
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center">
<EditText
android:id="@+id/name"
android:layout_width="125dp"
android:layout_height="wrap_content"
android:hint="Name"
android:maxLength="10"
/>
<EditText
android:id="@+id/price"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:hint="Price"
android:inputType="numberDecimal"
android:maxLength="5"
/>
</LinearLayout>
<Button
android:id="@+id/btnSimple"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center">
<ListView
android:id="@+id/noteList"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
I have a GUI
for the user to add rows after inserting 2 values. A row would be added while displaying the 2 values which the user added. But apparently it is creating more rows than it is supposed to. For example, 1 click can create 5 rows when it is supposed to only create 1.
Next problem is that the adapter overwrites the older values with the newer values instead of creating new rows.
To sum up the problems faced:
1) Adapter creating multiple rows instead of one.
2) Adapter overwriting old rows with new rows.
Any solutions?
main.java
public class BillSplitter extends Activity{
ListView list;
Button calculate;
EditText result;
String total;
String name;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Spinner spinner = (Spinner)findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.spinner_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
list = (ListView)findViewById(R.id.noteList);
final EditText price = (EditText)findViewById(R.id.price);
final EditText name1 = (EditText)findViewById(R.id.name);
Button btnSimple = (Button)findViewById(R.id.btnSimple);
btnSimple.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
double totalPrice = Double.parseDouble(price.getText().toString());
int position = spinner.getSelectedItemPosition();
String namename = "";
String totaltotal = "";
namename = name1.getText().toString();
if(position == 0)
{
totalPrice = totalPrice * 1.07;
roundTwoDecimals(totalPrice);
total = String.valueOf(totalPrice);
System.out.println(total);
//result.setText(total);
}
else
{
totalPrice = (totalPrice * 1.1)*1.07;
roundTwoDecimals(totalPrice);
total = String.valueOf(totalPrice);
System.out.println(total);
//result.setText(total);
}
/*noteList.add(0, total);
System.out.println(total);
name1.setText("");
price.setText("");
aa.notifyDataSetChanged();*/
price.setText("");
name1.setText("");
Adapter adapter = new Adapter(main.this, name, total);
list.setAdapter(adapter);
}
});
}
void roundTwoDecimals(double d) {
DecimalFormat twoDForm = new DecimalFormat("#.##");
twoDForm.format(d);
}
}
Adapter.java
public class Adapter extends BaseAdapter {
private Activity activity;
private String result;
private String text;
private static LayoutInflater inflater=null;
public Adapter(Activity a, String d, String t) {
activity = a;
text=d;
result = t;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return result.length();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public static class ViewHolder{
public TextView text;
public EditText edittext;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
ViewHolder holder;
if(convertView==null){
vi = inflater.inflate(R.layout.custom_list_item, null);
holder=new ViewHolder();
holder.text=(TextView)vi.findViewById(R.id.nametv);;
holder.edittext=(EditText)vi.findViewById(R.id.result);;
vi.setTag(holder);
}
else
holder=(ViewHolder)vi.getTag();
holder.text.setText(text);
holder.edittext.setText(result);
return vi;
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center">
<EditText
android:id="@+id/name"
android:layout_width="125dp"
android:layout_height="wrap_content"
android:hint="Name"
android:maxLength="10"
/>
<EditText
android:id="@+id/price"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:hint="Price"
android:inputType="numberDecimal"
android:maxLength="5"
/>
</LinearLayout>
<Button
android:id="@+id/btnSimple"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center">
<ListView
android:id="@+id/noteList"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
现在活动发生了变化......
现在来主要修正Adpater......
Now the Activity Changes....
Now Come the main correction the Adpater......
您的适配器实现是错误的。您的适配器类中应该有一个结果列表。您可能需要再次检查适配器代码。
http://mylifewithandroid.blogspot.com/2008/04/custom-widget -adapters.html
http://techdroid.kbeanie.com/2009/07/custom-listview-for-android.html
Your adapter implementation is wrong. You should have a list of results in your adapter class. You probably need to go through your adapter code again.
http://mylifewithandroid.blogspot.com/2008/04/custom-widget-adapters.html
http://techdroid.kbeanie.com/2009/07/custom-listview-for-android.html
这个适配器不能与 Spinner 一起使用。我对此很确定。如果您想要那种设计,那么
采用一个自定义对话框。在自定义对话框中采用列表视图,然后将此适配器设置为列表视图。
这也会弹出像微调器一样的对话框。如果您对此感兴趣,那么我也可以通过编辑这个问题为您提供代码
This adapter will not work with Spinner.I am sure about this.If you want that design then
take one custom Dialog.In custom dialog take listview and then set this adapter to Listview.
This will also popup dialog like spinner.If you interested in this then i can provide you code also by editing this question