如何从listview中的numberpicker获取不同值的不同值

时间:2016-03-17 19:29:40

标签: java android json xml android-studio

我想从用户通过数字选择器获取数量。我正在获得价值但是一个数字选择器输入的值也在所有其他数字选择器中重复。你可以帮我解释一下代码吗

MainActivity.java

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;


public class MainActivity extends Activity implements OnClickListener {

  ListView mListView;
  Button btnShowCheckedItems;
  ArrayList<Menu> mMenus;
  MultiSelectionAdapter<Menu> mAdapter;
  HashMap<Integer,Integer> quantityList;
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    bindComponents();
    init();
    addListeners();
  }
  private void bindComponents() {
    // TODO Auto-generated method stub
    mListView = (ListView) findViewById(android.R.id.list);
    btnShowCheckedItems = (Button) findViewById(R.id.btnShowCheckedItems);
  }
  private void init() {
    // TODO Auto-generated method stub
    mMenus = new ArrayList<Menu>();
    mMenus.add(new Menu("Pendrive"));
    mMenus.add(new Menu("Laptop"));
    mMenus.add(new Menu("Mouse"));

    quantityList=new HashMap<Integer,Integer>();
    quantityList=null;

    mAdapter = new MultiSelectionAdapter<Menu>(this, mMenus);
    mListView.setAdapter(mAdapter);
  }
  private void addListeners() {
    // TODO Auto-generated method stub
    btnShowCheckedItems.setOnClickListener(this);
  }
  @Override
  public void onClick(View v) {
  // TODO Auto-generated method stub
    if(mAdapter != null) {
      ArrayList<Menu> mArrayMenus = mAdapter.getCheckedItems();
      Log.d(MainActivity.class.getSimpleName(), "Selected Items: " + mArrayMenus.toString());
      Toast.makeText(getApplicationContext(), "Selected Items: " + mArrayMenus.toString(), Toast.LENGTH_LONG).show();


      Toast.makeText(getApplicationContext(),"Item-qty"+quantityList.toString(),Toast.LENGTH_LONG).show();
    }
  }
}

MultiSelectionAdapter.java

package com.example.anjali.mylistviewdemo;

/**
 * Created by jaisnain on 17/3/16.
 */

import android.content.Context;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.NumberPicker;
import android.widget.TextView;

import java.util.ArrayList;
public class MultiSelectionAdapter<T> extends BaseAdapter{
    Context mContext;
    LayoutInflater mInflater;
    ArrayList<T> mList;
    NumberPicker np;
    TextView tv1;
    String newV;
    SparseBooleanArray mSparseBooleanArray;
    public MultiSelectionAdapter(Context context, ArrayList<T> list) {
        // TODO Auto-generated constructor stub
        this.mContext = context;
        mInflater = LayoutInflater.from(mContext);
        mSparseBooleanArray = new SparseBooleanArray();
        mList = new ArrayList<T>();
        this.mList = list;


    }
    public ArrayList<T> getCheckedItems() {
        ArrayList<T> mTempArry = new ArrayList<T>();
        for(int i=0;i<mList.size();i++) {
            if(mSparseBooleanArray.get(i)) {
                mTempArry.add(mList.get(i));
            }
        }
        return mTempArry;
    }
    @Override
    public int getCount() {
        return mList.size();
    }
    @Override
  public Object getItem(int position) {
        // TODO Auto-generated method stub
        return mList.get(position);
    }
    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        if(convertView == null) {
            convertView = mInflater.inflate(R.layout.row, null);
        }

        NumberPicker np = (NumberPicker) convertView.findViewById(R.id.numberPicker);
        tv1 = (TextView)convertView.findViewById(R.id.editText);

        np.setMinValue(0);
        np.setMaxValue(10);
        np.setValue(0);
        np.setWrapSelectorWheel(false);

        TextView tv=(TextView)convertView.findViewById(R.id.editText);
        tv1.setTag(position);

        np.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                // TODO Auto-generated method stub

                //newV = newVal + " ";
                if (newVal > 0) {
                    //mSparseBooleanArray.put(position, true);
                    System.out.println("Position:"+position+"NewVal:"+newVal);
                    MainActivity m = new MainActivity();
                    m.quantityList.put(position, newVal);
                    tv1.setText(newVal);

                }
                //System.out.println("New Value:" + newV);
            }
        });

        TextView tvTitle = (TextView) convertView.findViewById(R.id.tvTitle);
        tvTitle.setText(mList.get(position).toString());




        CheckBox mCheckBox = (CheckBox) convertView.findViewById(R.id.chkEnable);
        mCheckBox.setTag(position);
        mCheckBox.setChecked(mSparseBooleanArray.get(position));
        mCheckBox.setOnCheckedChangeListener(mCheckedChangeListener);

        return convertView;
    }
    OnCheckedChangeListener mCheckedChangeListener = new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // TODO Auto-generated method stub
            mSparseBooleanArray.put((Integer) buttonView.getTag(), isChecked);
        }
    };
}

Menu.java

package com.example.anjali.mylistviewdemo;

/**
 * Created by jaisnain on 17/3/16.
 */

public class Menu {
    String name;

public Menu(String name) {
        // TODO Auto-generated constructor stub
        this.name = name;

    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return this.name;
    }

}

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <ListView
        android:id="@android:id/list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_above="@+id/btnShowCheckedItems"
        />
    <Button
        android:id="@+id/btnShowCheckedItems"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="Show"
        />
</RelativeLayout>

row.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="10dp" >
    <CheckBox
        android:id="@+id/chkEnable"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:text=""/>
    <TextView
        android:id="@+id/tvTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:text="title"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="39dp"
        android:layout_marginStart="39dp"
        android:layout_alignBottom="@+id/chkEnable"
        android:layout_toRightOf="@+id/chkEnable"
        android:layout_toEndOf="@+id/chkEnable" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/editText"
        android:text="qty"
        android:layout_centerVertical="true"
        android:layout_alignLeft="@+id/tvTitle"
        android:layout_alignStart="@+id/tvTitle" />

    <NumberPicker
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/numberPicker"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />


</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

1)删除quantityList = null;在MainActivity.java中

2)删除TextView tv =(TextView)convertView.findViewById(R.id.editText);在MultiSelectionAdapter.java中。重复的代码。你已初始化为tv1。

3)在适配器中实现回调

 private AdapterCallback mAdapterCallback;

 public static interface AdapterCallback {
    void onCallback(int position, int value);
 }

 public MultiSelectionAdapter(Context context, ArrayList<T> list) {
        // TODO Auto-generated constructor stub
        this.mContext = context;
        mInflater = LayoutInflater.from(mContext);
        mSparseBooleanArray = new SparseBooleanArray();
        mList = new ArrayList<T>();
        this.mList = list;

        try {
            this.mAdapterCallback = ((AdapterCallback) context);
        } catch (ClassCastException e) {
            throw new ClassCastException("Activity must implement AdapterCallback.");
        }

 }

 public void onValueChange(NumberPicker picker, int oldVal, int newVal) {

    if (newVal > 0) {

        mAdapterCallback.onCallback(position, newVal);
        tv1.setText(Integer.toString(newVal));

    }
}

MainActivity.java

@Override
public void onCallback(int position, int value) {
            quantityList.put(position, value);

}