Android在将微调器添加到列表视图并获取数据时遇到麻烦

时间:2018-07-10 16:56:14

标签: listview spinner

所以我是Android和Java的新手。我在寻找将微调器添加到列表视图的代码,并找到了以下代码。列表视图显示正常。每个微调器的值为1-6。当用户单击按钮时,我想为每一行查找微调器的值。有人可以告诉我这样做的代码吗?

列表行的

xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center_vertical"
android:padding="20dp">

<TextView
android:id="@+id/row_item_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<Spinner
android:id="@+id/row_item_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:padding="2dp" />

用于列表视图的适配器:

public class BasicSpinnerAdapter extends BaseAdapter {

private ArrayList<String> mSpinnerItems;
private ArrayList<String> mData;
private Context mContext;

public BasicSpinnerAdapter(ArrayList<String> data, ArrayList<String> 
spinnerItems, Context context) {
mData = data;
mContext = context;
mSpinnerItems = spinnerItems;
}

@Override
public int getCount() {
    return mData.size();
}

@Override
public Object getItem(int position) {
    return mData.get(position);
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;
    if (view == null) {
        LayoutInflater inflater = (LayoutInflater) 
mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.row_item_spinner, null);
    }

    TextView textView = (TextView) view.findViewById(R.id.row_item_textview);
    Spinner spinner = (Spinner) view.findViewById(R.id.row_item_spinner);

    textView.setText(mData.get(position));
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(mContext, 
android.R.layout.simple_spinner_item, mSpinnerItems);
    spinner.setAdapter(adapter);
    return view;
}

活动:

public class SpinnerListviewActivity extends AppCompatActivity {

private ListView lvSpinner;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.spinner_listview_activity);
    lvSpinner = (ListView) findViewById(R.id.listview_spinner);

    ArrayList<String> mData = new ArrayList<>();
    mData.add("Test1");
    mData.add("Test2");
    mData.add("Test3");
    mData.add("Test4");

    ArrayList<String> mSpinnerData = new ArrayList<>();
    mSpinnerData.add("1");
    mSpinnerData.add("2");
    mSpinnerData.add("3");
    mSpinnerData.add("4");

    BasicSpinnerAdapter adapter = new BasicSpinnerAdapter(mData, mSpinnerData, 
this);
    lvSpinner.setAdapter(adapter);
  }
}

活动xml:

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

<ListView
    android:id="@+id/listview_spinner"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

</ListView>

0 个答案:

没有答案