Android - 适配器创建多行而不是一行

发布于 2024-12-27 23:31:39 字数 5051 浏览 2 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

依 靠 2025-01-03 23:31:39
 Private class DetailBean{
    private String name;
    private double price;
    /// Getter and Setter methods....
    }

现在活动发生了变化......

public class BillSplitter extends Activity{ 

ListView listView; 

Button calculate; 
EditText result; 
String total; 
String name; 
private ArrayList<DetailBean> list;
private DetailBean entry;

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); 

    listView = (ListView)findViewById(R.id.noteList); 

list = new ArrayList<DetailBean>();
CorrectedAdapter  adapter = new CorrectedAdapter (main.this,list); 
            listView.setAdapter(adapter); 

    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 = "";   
entry = new  DetailBean();          
            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("");             
entry.setName(name);
entry.setPrice(total);
list.add(entry);
adapter.notifyDataSetChanged();             
        }                        
    });                     

void roundTwoDecimals(double d) { 
    DecimalFormat twoDForm = new DecimalFormat("#.##"); 
    twoDForm.format(d); 

  } 
} 

现在来主要修正Adpater......

public class CorrectedAdapter extends BaseAdapter {                

private Activity activity;                
private ArrayList<DetailBean> list;
private DetailBean entry;
private static LayoutInflater inflater=null;                

public Adapter(Activity a,ArrayList<DetailBean> l) {                
    activity = a;                
   list=l;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);                
}                

public int getCount() {                
    return list.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) {                
entry = list.get(position);    
View vi=convertView;                
    ViewHolder holder;                
    if(convertView==null){                
       convertView = inflater.inflate(R.layout.custom_list_item, null);                
        holder=new ViewHolder();                
        holder.text=(TextView) convertView.findViewById(R.id.nametv);;                
        holder.edittext=(EditText) convertView.findViewById(R.id.result);;                
       convertView setTag(holder);                

    }                
    else               
{ 

    holder=(ViewHolder) convertView getTag();                
}  
  holder.text.setText(entry.getName);                
    holder.edittext.setText(entry.getPrice);                
    return convertView;                
}                
}                
 Private class DetailBean{
    private String name;
    private double price;
    /// Getter and Setter methods....
    }

Now the Activity Changes....

public class BillSplitter extends Activity{ 

ListView listView; 

Button calculate; 
EditText result; 
String total; 
String name; 
private ArrayList<DetailBean> list;
private DetailBean entry;

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); 

    listView = (ListView)findViewById(R.id.noteList); 

list = new ArrayList<DetailBean>();
CorrectedAdapter  adapter = new CorrectedAdapter (main.this,list); 
            listView.setAdapter(adapter); 

    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 = "";   
entry = new  DetailBean();          
            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("");             
entry.setName(name);
entry.setPrice(total);
list.add(entry);
adapter.notifyDataSetChanged();             
        }                        
    });                     

void roundTwoDecimals(double d) { 
    DecimalFormat twoDForm = new DecimalFormat("#.##"); 
    twoDForm.format(d); 

  } 
} 

Now Come the main correction the Adpater......

public class CorrectedAdapter extends BaseAdapter {                

private Activity activity;                
private ArrayList<DetailBean> list;
private DetailBean entry;
private static LayoutInflater inflater=null;                

public Adapter(Activity a,ArrayList<DetailBean> l) {                
    activity = a;                
   list=l;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);                
}                

public int getCount() {                
    return list.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) {                
entry = list.get(position);    
View vi=convertView;                
    ViewHolder holder;                
    if(convertView==null){                
       convertView = inflater.inflate(R.layout.custom_list_item, null);                
        holder=new ViewHolder();                
        holder.text=(TextView) convertView.findViewById(R.id.nametv);;                
        holder.edittext=(EditText) convertView.findViewById(R.id.result);;                
       convertView setTag(holder);                

    }                
    else               
{ 

    holder=(ViewHolder) convertView getTag();                
}  
  holder.text.setText(entry.getName);                
    holder.edittext.setText(entry.getPrice);                
    return convertView;                
}                
}                
淤浪 2025-01-03 23:31:39

您的适配器实现是错误的。您的适配器类中应该有一个结果列表。您可能需要再次检查适配器代码。

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

转角预定愛 2025-01-03 23:31:39

这个适配器不能与 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文