Creating Listview dynamically in Android

前端 未结 1 501
挽巷
挽巷 2020-12-22 01:47

I Have a two dimensional string array,

I want to create a 3 column list-view, that display data from the string array, it should have the same number o

相关标签:
1条回答
  • 2020-12-22 02:17

    for that you need a customize list view first you should add a listview in your main.xml(just an eg) file then create a class like this

    public class MySimpleArrayAdapter extends ArrayAdapter<String> {
          private final Context context;
          private final String[] values;
          DataHelper dh;
    
          public MySimpleArrayAdapter(Context context, int textViewResourceId, String[] values) {
            super(context, textViewResourceId, values);
            this.context = context;
            this.values = values;
    
            dh=new DataHelper(getApplicationContext());
          }
    
          @Override
          public View getView(final int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View rowView = inflater.inflate(R.layout.list_name, parent, false);
           textView = (TextView) rowView.findViewById(R.id.textname);
    
            textView.setText(values[position]);
            // Change the icon for Windows and iPhone
            textView.setOnClickListener(new View.OnClickListener() {
                   public void onClick(View v) 
                   {
                       Toast.makeText(this,""+values[position],10000).show();
                   }
                   });
    
    
            return rowView;
    } 
    

    R.layout.list_name this will be the new xml file which will load the contents to the list view

    and the the final step just in your on create method do this

    con = (ListView) findViewById(R.id.main_listView); 
     MySimpleArrayAdapter adapter = new MySimpleArrayAdapter(MainActivity.this,  R.id.main_listView ,data);// data is String array valu to be added in list view
        //setting the adapter
        con.setAdapter(adapter);
    
    0 讨论(0)
提交回复
热议问题