我有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);
}
}
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>
答案 0 :(得分:4)
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;
}
}
答案 1 :(得分:0)
您的适配器实现错误。您应该在适配器类中有一个结果列表。您可能需要再次检查适配器代码。
http://mylifewithandroid.blogspot.com/2008/04/custom-widget-adapters.html
http://techdroid.kbeanie.com/2009/07/custom-listview-for-android.html
答案 2 :(得分:0)
这个适配器不适用于Spinner。我很确定。如果你想要那个设计 取一个自定义Dialog.In自定义对话框获取listview,然后将此适配器设置为Listview。 这也将像spinner一样弹出对话框。如果您对此感兴趣,那么我也可以通过编辑此问题为您提供代码