Android how to use adapter for listView without extending listActivity

后端 未结 3 823
情书的邮戳
情书的邮戳 2020-12-19 08:34

I have an application with tabs. In one tab I need to put data (strings) in rows. To do so I chose tableLayout but when I wanted to use a contextmenu

3条回答
  •  心在旅途
    2020-12-19 09:37

    main.xml:

    
    
            
           
    
    
    

    You need to define an xml which will be used to hold data of each row:

    
    
    
       
    
            
            
            
    
        
    
    
    

    In the above xml i have also included the ImageView, it is not really required but this is just to update you that we can include the other controls also.

    & at the last you should have a function in your related class:

    private void LoadData()
    {
    
        DBAdapter db = new DBAdapter(this);
        db.open();
        Cursor cur = db.GetData();
        private ListView lv = (ListView)findViewById(R.id.myLayout);
        lv.setAdapter(null);
        if(cur.moveToFirst())
        {
                String[] from = new String[] {"_id","column1"};
                int[] to = new int[] {R.id.col1, R.id.col2};
                SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,R.layout.grid_item, cur, from, to);
                lv.setAdapter(adapter);  
    
         }
           db.close();
             }
    }
    

提交回复
热议问题