【问题标题】:How to remove selected item from all the other spinners in android如何从android中的所有其他微调器中删除选定的项目
【发布时间】:2015-04-07 17:36:10
【问题描述】:

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

{"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;
}

}

【问题讨论】:

    标签: android listview spinner


    【解决方案1】:

    我遇到了同样的问题,但我可以成功地从列表中删除选定的项目之后我做了一个改变网。所选项目在下拉列表中显示为突出显示状态。我知道我的回答不能满足您的问题,但想与您分享。

    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" />
      

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-09-10
      • 2016-08-07
      • 2016-08-17
      • 1970-01-01
      • 2018-05-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多