android list inside layout

后端 未结 2 878
执念已碎
执念已碎 2021-01-29 07:35

i am working on an Android app that needs showing a list[table], inside the layout[view]

I come from iPhone dev objC land, and i have an app that shows a table[list] ins

2条回答
  •  -上瘾入骨i
    2021-01-29 08:05

    Yes, of course, you can do that

    1) you need to have listholder.xml here, you can scratch anything in you layout view, either imageview, textview..etc. just don't forget to add ListView inside it. for example:

    
    
    
        
    
    
    
        
    
    

    2) For custom your own list item, you have to create listitem.xml i.e.

    
    
        
                
        
        
                
                
          
    
    

    3) create customAdapter in your activity, it would look like this;

    public class MyListActivity extends ListActivity {
    
    private ArrayList yourdata = new ArrayList();
            @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.listholder);    
                   // yourdata might be array, arraylist etc. 
    
                   MyCustomAdapter listadapter = new MyCustomAdapter(this, R.layout.listitem, yourdata);
    
              setListAdapter(listadapter);
    }
    
    private class MyCustomAdapter extends ArrayAdapter{
                    //this case, i use Yourdata as type
            private ArrayList items;
    
            public PreviousAdapter(Context context, int textViewResourceId,
                    ArrayList items) {
                super(context, textViewResourceId, items);
                this.items = items;
            }
    
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View v = convertView;
                if(v == null) {
                    LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    v = vi.inflate(R.layout.listitem, null);
                }
                Yourdata yt = items.get(position);
                if(yt != null){
                 // Don't forget to use v.findView...., otherwise, it might force close when run app.
                    TextView dateStr = (TextView)v.findViewById(R.id.date_value);
    
                        dateStr.setText(yt.getDate());
                }   
                return v;
            }
    
    
    
          }
    
    
    }
    

    P.S. the above code might not exactly right... just give you an idea :) Here is a source about custom list (you might have seen it) hope it useful

    http://www.vogella.de/articles/AndroidListView/article.html

提交回复
热议问题