如何在微调器中的选择项上获取Listview

时间:2014-07-14 06:46:40

标签: android android-listview spinner

实际上我正在开发一个将实现所有CURD操作的应用程序。 我的应用程序由微调器,Listview,文本框组成。 现在我想要的是,为了更新操作,我需要通过选择从列表视图中获取值。

我不知道如何在Spinner中显示所选值(来自Listview)。

这是我的微调器编码

sppriority = (Spinner) findViewById(R.id.sp_priority);
public void addNewTask(View view) {
HashMap<String, String> queryValues = new HashMap<String, String>();
queryValues.put("priority", sppriority.getSelectedItem().toString());

sppriority.setOnItemSelectedListener(new OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView<?> adapter, View v,
int position, long id) {
// On selecting a spinner item
Task = adapter.getItemAtPosition(position).toString();
}

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

}
});

这是关于点击代码的Listview:

lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {

lvpriority = (TextView) view.findViewById(R.id.lvpriority);
String vallvpriority = lvpriority.getText().toString();

Intent objIndent5 = new Intent(getApplicationContext(),addinbox.class);
objIndent5.putExtra("lvpriority", vallvpriority);
}

这就是我使用的不起作用的方式:

 **if(animalList.size()!=0) {

    sppriority.setText(animalList.get("priority"));
    }**

        public void editTask(View view) {
        HashMap<String, String> queryValues = new HashMap<String, String>();
        queryValues.put("priority", sppriority.getSelectedItem().toString());
        controller.updateTask(queryValues);
        }

请帮我将listview的选定项目转到微调器。 提前谢谢。

1 个答案:

答案 0 :(得分:0)

以下是一个简单方法的概述:

1) Have a HashSet which is member of your activity, let us call it selectSet.
   //Initialize like this
   HashSet<String>selectSet=new HashSet<String>();

2) onItemClick of ListView, add item to selectSet.
     public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
        lvpriority = (TextView) view.findViewById(R.id.lvpriority);
        String vallvpriority = lvpriority.getText().toString();
        selectSet.add(vallvpriority);
     }

3) Create ArrayAdapter<String> spinnerAdapter=new ArrayAdapter<String>(context, resource);
4) Iterate through the HashSet and add items to the spinnerAdapter
     for(String priority: selectSet){
        spinnerAdapter.add(priority);
     }
5) Set the spinnerAdapter to your spinner:
     spppriority.setAdapter(spinnerAdapter);