如何从android中的所有其他微调器中删除所选项目

时间:2015-04-06 12:05:51

标签: android listview spinner

我是android的初学者。我有一个ListView,包含一个微调器,edittext和一个用于删除该行的图像按钮。微调器从列表中填充。有一个添加按钮,可以将这些项添加到列表视图中每次单击时。如果我在微调器中选择一个项目,那么我需要从该列表视图中的所有其他微调器中删除该项目,并且当我删除所选微调器行或更改微调器中的所选项目时,项目也变得可见。为简单起见,我的Spinners填充了以下数据:

{"data1", "data2", "data3", "data4", "data5"};

例如,如果我选择我的第一个ListView的Spinner值为“data3”,然后“data3”条目从所有其他ListView Spinner中消失,这将仅在我更改第一个listview微调器中的选定值时返回或删除该行,类似于每个ListView微调器。如何做到这一点?有人请帮帮我。下面是我的代码。

receipt_trans.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout
    android:id="@+id/LinLay3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="5dp"
    android:layout_marginTop="5dp"
    android:orientation="horizontal" >
    <TextView
        android:id="@+id/txtGrossAmt"
        android:layout_width="95dp"
        android:layout_height="wrap_content"
        android:text="Gross amount :"
        android:textColor="#000" />

    <EditText
        android:id="@+id/edtGrossAmt"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:background="@drawable/border"
        android:focusableInTouchMode="false"
        android:layout_toRightOf="@+id/txtAccount" />
    <Button 
        android:id="@+id/btnAddRow"
        android:layout_width="wrap_content"
        android:layout_height="25dp"
        android:layout_marginLeft="10dp"
        android:text="Add Row"
        android:layout_toRightOf="@+id/edtGrossAmt" />

</LinearLayout>

<LinearLayout
    android:id="@+id/LinLay4"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="5dp"
    android:orientation="horizontal" >
    <TextView
        android:id="@+id/txtFundStores"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="40dp"
        android:text="Fund Stores"
        android:textColor="#000" />
    </LinearLayout>
    <ListView

    android:id="@+id/lstFundStore"

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:layout_marginTop="10dp" >

</ListView>
</LinearLayout>

fundstore_row.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<Spinner
    android:id="@+id/spnAccount"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_alignBottom="@+id/imageButton1"
    android:layout_marginBottom="15dp"
    android:layout_toRightOf="@+id/textView1" />

<EditText
    android:id="@+id/edtAccAmount"
    android:layout_width="0sp"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:gravity="center"
    android:inputType="numberDecimal"
    android:textAppearance="?android:attr/textAppearanceSmall"/>

<ImageButton
    android:id="@+id/imgDel"
    android:layout_width="20sp"
    android:layout_height="fill_parent"
    android:contentDescription="delete"
    android:src="@drawable/delete2" />

</LinearLayout>

ReceiptTransaction.java:

public class ReceiptTransaction extends Activity{
RestTemplate restTemplate=new RestTemplate();
String constr="http://192.168.1.14:8080/ServPro/stock/";
ListView lstFund;
Button btnAddRw;
private FundStoreAdapter adapter;
ArrayList<COAAccount> subList;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.receipt_trans);
    lstFund=(ListView) findViewById(R.id.lstFundStore);
    btnAddRw=(Button) findViewById(R.id.btnAddRow);
    setFundStore();
    setDeduction();
    btnAddRw.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            adapter.add(new FundReceipt(subList.get(0), 0.0f));
        }
    });
}

public void setFundStore(){
    String accList=restTemplate.getForObject(constr+"getAllCBOnly", String.class);
    Gson gson = new Gson(); // Or use new GsonBuilder().create();
    CoaAccountList dropCoaList = gson.fromJson(accList, CoaAccountList.class);
    subList=new ArrayList<COAAccount>();
    COAAccount newAccount = new COAAccount();
    newAccount.setFunds(-1, -2, "Select...", "select", "SE",0.0f);
    subList.add(newAccount);
    ArrayList<COAAccount> listItem=(ArrayList<COAAccount>) dropCoaList.getCoaList();
    for(COAAccount addCoa:listItem){
        subList.add(addCoa);
    }
    adapter=new FundStoreAdapter(ReceiptTransaction.this, R.layout.fundstore_row, new ArrayList<FundReceipt>(),subList);
    lstFund.setAdapter(adapter);
    adapter.add(new FundReceipt(subList.get(0), 0.0f));
}
public void setDeduction(){

}
}

FundStoreAdapter.java:

public class FundStoreAdapter extends ArrayAdapter<FundReceipt> {
protected static final String LOG_TAG = FundStoreAdapter.class.getSimpleName();
private final Context context;
private final int resourceID;
private List<FundReceipt> items;
private ArrayList<COAAccount> list;

    public FundStoreAdapter(Context context, int resource, List<FundReceipt> items, ArrayList<COAAccount> subList) {
        super(context, resource,items);
        this.context = context;
        this.resourceID = resource;
        this.items=items;
        this.list=subList;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        FundStoreHolder holder = null;
        LayoutInflater inflater =  ((Activity) context).getLayoutInflater();
        View rowView = inflater.inflate(resourceID, parent, false);
        holder=new FundStoreHolder();
        holder.fundRecpt=items.get(position);
        holder.imgDeleteFund=(ImageButton) rowView.findViewById(R.id.imgDel);
        holder.imgDeleteFund.setTag(holder.fundRecpt);
        removeRow(holder);
        holder.edtAmount=(EditText) rowView.findViewById(R.id.edtAccAmount);
        setValueTextListeners(holder);
        holder.spnAcct=(Spinner) rowView.findViewById(R.id.spnAccount);
        setNameTextChangeListener(holder);
        rowView.setTag(holder);
        setupItem(holder);

        return rowView;
    }

    private void removeRow(FundStoreHolder holder) {
        holder.imgDeleteFund.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                FundReceipt itemToRemove = (FundReceipt)v.getTag();
                remove(itemToRemove);
            }
        });     
    }

    private void setupItem(FundStoreHolder holder) {
        holder.edtAmount.setText(String.valueOf(holder.fundRecpt.getAmount()));
        //ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, list);
        ArrayAdapter<COAAccount> dataAdapter =new ArrayAdapter<COAAccount>(context, android.R.layout.simple_spinner_item,list);
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        holder.spnAcct.setAdapter(dataAdapter);
        holder.spnAcct.setSelection(dataAdapter.getPosition(holder.fundRecpt.getSelAcct()));
    }

    public static class FundStoreHolder{
        FundReceipt fundRecpt;
        Spinner spnAcct;
        EditText edtAmount;
        ImageButton imgDeleteFund;
    }

    private void setNameTextChangeListener(final FundStoreHolder holder) {
        holder.spnAcct.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                // TODO Auto-generated method stub
                holder.fundRecpt.setSelAcct((COAAccount) parent.getSelectedItem());
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // TODO Auto-generated method stub

            }
        });
    }

    private void setValueTextListeners(final FundStoreHolder holder) {
        holder.edtAmount.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                try{
                    holder.fundRecpt.setAmount(Float.parseFloat(s.toString()));
                }catch (NumberFormatException e) {
                    Log.e(LOG_TAG, "error reading float value: " + s.toString());
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) { }

            @Override
            public void afterTextChanged(Editable s) { }
        });
    }
}

FundReceipt.java:

public class FundReceipt implements Serializable {
private static final long serialVersionUID = -5435670920302756945L;

private Float amount = 0.0f;
private COAAccount selAcct;
public FundReceipt(COAAccount selAcct, Float amount) {
    this.setAmount(amount);
    this.setSelAcct(selAcct);
}
public Float getAmount() {
    return amount;
}
public void setAmount(Float amount) {
    this.amount = amount;
}
public COAAccount getSelAcct() {
    return selAcct;
}
public void setSelAcct(COAAccount selAcct) {
    this.selAcct = selAcct;
}

}

1 个答案:

答案 0 :(得分:0)

我面临同样的问题,但我可以成功从列表中删除所选项目之后我做了一个改变网络。所选项目在下拉列表中显示突出显示状态。我知道我的答案不符合您的问题,但想与您分享。

  1. 你的微调器:

             <Spinner
                android:id="@+id/sp_handi_cap"
                style="@style/SpinnerAppTheme"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"                    android:background="@drawable/selector_rectangular_gray_trans_bg"
                android:descendantFocusability="afterDescendants"
                android:gravity="start"
                android:minWidth="250dp"
                android:padding="5dp"
                android:singleLine="true" />
    
  2. selector_rectangular_gray_trans_bg.xml

    <solid android:color="@android:color/transparent" />
    
    <stroke
        android:width="1dp"
        android:color="@color/gray6" />
    
    <corners
        android:bottomLeftRadius="0dp"
        android:bottomRightRadius="0dp"
        android:topLeftRadius="0dp"
        android:topRightRadius="0dp" /></shape>
    

    创建布局:

    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@android:id/text1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/selector_spinner_item_activited"
        android:gravity="center"
        android:padding="3dp"
        android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
        android:paddingStart="?android:attr/listPreferredItemPaddingStart"
        android:textAppearance="?android:attr/textAppearanceListItemSmall"
        android:textSize="12sp" />
    

    selector selector_spinner_item_activited.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    
        <item android:drawable="@drawable/selector_rectangular_black_trans_bg" android:state_activated="true"/>
        <item android:drawable="@android:color/transparent"/>
    
    </selector>
    

    创建适配器:

    ArrayAdapter objAdapter = new ArrayAdapter<String>(AddStudentActivity.this,
                    R.layout.layout_text_single_line_activated, getResources()
                            .getStringArray(R.array.arr_handicap_option));
    

    在微调器对象中设置适配器。

    spinner.setAdapter(objAdapter);