通过微调器setOnItemSelectedListener将项添加到自定义列表视图

时间:2014-10-22 16:59:45

标签: android listview spinner

我正在尝试使用以下代码选择微调器时将项目添加到自定义列表视图,但这显然不起作用。我不知道怎么做。请帮我下面是我的小代码

Spnchapter.setOnItemSelectedListener(new OnItemSelectedListener() 
    {
        @Override
        public void onItemSelected(AdapterView<?> parent,
                View view, int pos, long id) 
        {                
            Toast.makeText(getApplicationContext(), Spnchapter.getSelectedItem().toString(),
                       Toast.LENGTH_LONG).show();
            generateData();             
        }

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

        }
    });
    // Spinner

private ArrayList<Item> generateData()
{
    ArrayList<Item> items = new ArrayList<Item>();

    int verseNumber = 1;
    ExternalDbOpenHelper dbOpenHelper = new ExternalDbOpenHelper(this, DB_NAME);
    database = dbOpenHelper.openDataBase();

    Cursor c = database.rawQuery("select * from friends where chapter = 1 ", null);
    c.moveToFirst();
    if(!c.isAfterLast()) 
    {
        do 
        {
            String name = c.getString(3);
            if(c.getString(2).length() == 0)
            {                   
                items.add(new Item("", " " + name + " " + c.getString(5)));
            }
            if(!c.getString(2).equals(""))
            {
                items.add(new Item(" " + Integer.toString(verseNumber++), "    " + name));                  
            }       
        } 
        while (c.moveToNext());
    }
    c.close();
    return items;
}

4 个答案:

答案 0 :(得分:2)

我对spinner onItemSelected函数进行了一些更改。在下面的代码中,替换&#34; yourListView&#34;使用您正在使用的列表视图。

Spnchapter.setOnItemSelectedListener(new OnItemSelectedListener() 
{
    @Override
    public void onItemSelected(AdapterView<?> parent,
            View view, int pos, long id) 
    {                
        Toast.makeText(getApplicationContext(), Spnchapter.getSelectedItem().toString(),
                   Toast.LENGTH_LONG).show();
        ArrayList<Item> items=generateData(); 
        MyAdapter adapter=new MyAdapter(getApplicationContext(),items);
        yourListView.setAdapter(adapter);

    }

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

    }
});

上述代码应该可以解决您遇到的问题。如果您遇到任何其他问题,请告诉我。

答案 1 :(得分:0)

当在微调器中选择一个项目时,您正在调用函数“generateData()”。 但是在这个函数中你没有做任何事情来添加项目到自定义列表视图。您所做的只是从数据库中获取内容并将其存储在arrayList“items”中。

如果要将项目添加到列表,那么您已将数据添加到适配器并调用yourAdapter.notifyDataSetChanged()。如果您发布listview适配器的代码,我可以更好地指导您解决问题。

答案 2 :(得分:0)

public class MyAdapter extends ArrayAdapter<Item> {

    private final Context context;
    private final ArrayList<Item> itemsArrayList;

    public MyAdapter(Context context, ArrayList<Item> itemsArrayList) {

        super(context, R.layout.row, itemsArrayList);

        this.context = context;
        this.itemsArrayList = itemsArrayList;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        // 1. Create inflater 
        LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        // 2. Get rowView from inflater
        View rowView = inflater.inflate(R.layout.row, parent, false);

        // 3. Get the two text view from the rowView
        TextView labelView = (TextView) rowView.findViewById(R.id.label);
        TextView valueView = (TextView) rowView.findViewById(R.id.value);

        // 4. Set the text for textView 
        labelView.setText(itemsArrayList.get(position).getTitle());
        valueView.setText(itemsArrayList.get(position).getDescription());

        // 5. retrn rowView
        return rowView;
    }

}

答案 3 :(得分:0)

此代码显示了如何使用简单的列表视图:

 private String[] mListStr = {"name1","name2","name3","name4"};  
 ListView mListView = (ListView)findViewById(R.id.listview1);  
 mListView.setListAdapter(new ArrayAdapter<String>(this,  
                android.R.layout.simple_list_item_1, mListStr));

,结果是带有字符串数组的简单列表视图。

在您的代码中,您似乎需要使用listview显示项目列表。 所以你需要编写自己的列表视图。

public class ListViewAdapter extends BaseAdapter { 
     public final class ListItemView{                //asume your item has 
                                                     //an image'a string and a button   
                public ImageView image;     
                public TextView title;        
                public Button detail;          
         }
    private List<Item> listItems;                  //your data
    private LayoutInflater listContainer;  
    public ListViewAdapter(Context context, List<Map<Item> listItems) {   
                this.context = context;            
                listContainer = LayoutInflater.from(context);   //create view   
                this.listItems = listItems;     
           }  
    public int getCount() {   
        // TODO Auto-generated method stub   
        return listItems.size();   
    }   

    public Object getItem(int arg0) {   
        // TODO Auto-generated method stub   
        return null;   
    }   

    public long getItemId(int arg0) {   
        // TODO Auto-generated method stub   
        return 0;   
    }
      public View getView(int position, View convertView, ViewGroup parent) {   
    // TODO Auto-generated method stub   

    ListItemView  listItemView = null;   
    if (convertView == null) {   
        listItemView = new ListItemView();    
        //get list_item layout   
        convertView = listContainer.inflate(R.layout.list_item, null);   
        //get elements   
        listItemView.image = (ImageView)convertView.findViewById(R.id.imageItem);   
        listItemView.title = (TextView)convertView.findViewById(R.id.titleItem);     
        listItemView.detail= (Button)convertView.findViewById(R.id.detailItem);   
        //set listitem to convertView   
        convertView.setTag(listItemView);   
    }else {   
        listItemView = (ListItemView)convertView.getTag();   
    }   

    //set text and image
    listItemView.image.setBackgroundResource((Integer) listItems.get(   
            position).get("image"));   
    listItemView.title.setText((String) listItems.get(position)   
            .get("title"));   

    listItemView.detail.setText("click");   
    //set click listener   
    listItemView.detail.setOnClickListener(new View.OnClickListener() {   
        @Override  
        public void onClick(View v) {   
            //do something
        }   
    });   

    return convertView;   
}     
}