Android - 将多个值传递到适配器

时间:2012-01-19 11:48:53

标签: android adapter

我的main.xml有2个编辑文本供用户插入值。我的custom_list_item.xml有一个文本视图和一个编辑文本。用户的输入将分别插入文本视图和编辑文本。

但是,目前我只能将1个值传递给适配器。如何将其他值传递给适配器?

main.java

public class main extends Activity{

ArrayList<String> noteList = new ArrayList<String>();
FancyAdapter aa = null;

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

    ListView myListView = (ListView)findViewById(R.id.noteList);
    aa = new FancyAdapter();

    final EditText price = (EditText)findViewById(R.id.price);
    final EditText name1 = (EditText)findViewById(R.id.name);
    final EditText result = (EditText)findViewById(R.id.result);
    myListView.setAdapter(aa);

    myListView.setOnItemClickListener(new OnItemClickListener() {
       public void onItemClick(AdapterView<?> arg0, View arg1,
                 int position, long arg3) {

           aa.notifyDataSetChanged();
        }
    });

    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();
            name = name1.getText().toString();

            if(position == 0)
            {
                totalPrice = totalPrice * 1.07;
                total = String.valueOf(totalPrice);
                System.out.println(total);
                //result.setText(total); 
            }
            else
            {
                totalPrice = (totalPrice * 1.1)*1.07;
                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();
        }
    });        

}

class FancyAdapter extends ArrayAdapter<String>
{
    Button calculate;
    EditText price;
    EditText result;

    FancyAdapter()
    {
        super(main.this, android.R.layout.simple_list_item_1, noteList);
    }

    public View getView(int position, View convertView, ViewGroup parent)
    {
        View row = convertView;     

        if(row == null)
        {
            LayoutInflater inflater = getLayoutInflater();
            row = inflater.inflate(R.layout.custom_list_item, null);
        }

        //((TextView)row.findViewById(R.id.nametv)).setText(noteList.get(position));
        ((EditText)row.findViewById(R.id.result)).setText(noteList.get(position));
        return (row);
    }
}

}

2 个答案:

答案 0 :(得分:0)

通过扩展BaseAdapter类来创建自定义适配器,下面是相同的

的更多参考

adapter tutorial

Same question here

答案 1 :(得分:0)

您只需将整个字符串发送到适配器即可。然后相应地拆分字符串。您可能需要添加冒号或某些标识符以进行拆分。