title: Android - Create 2 columns ListView programmatically
body: <p>Below code will create a 2 columns ListView.
  Is it possible to create it programmatically without xml.</p>
  
  <p>Code:</p>
  
  <pre><code>package com.example.listviewexam;
  
  import android.app.Activity;
  import android.content.Context;
  import android.database.Cursor;
  import android.os.Bundle;
  import android.provider.MediaStore;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.ListAdapter;
  import android.widget.ListView;
  import android.widget.SimpleCursorAdapter;
  import android.widget.TextView;
  
  public class MainActivity extends Activity {
      Cursor mCursor;
      @Override
      protected void onDestroy() {
          super.onDestroy();
          if (mCursor != null) {
              mCursor.close();
          }
      }
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          //setContentView(R.layout.activity_main);
          ListView list = new ListView(this);
  
          mCursor = getContentResolver().query(MediaStore.Files.getContentUri("external"),
                  FILES_PROJECTION,
                  MediaStore.Files.FileColumns.DISPLAY_NAME + "!=?",
                  new String[] {""},
                  null);
  
          ListAdapter adapter;
          String[] from = new String[] {MediaStore.Files.FileColumns._ID,
                  MediaStore.Files.FileColumns.DATA};
          int[] to = new int[] {R.id.tv1,R.id.tv2};
          adapter = new Custom_Adapter(
                  this,R.layout.activity_main, mCursor,
                  from,
                  to,
                  0);
          list.setAdapter(adapter);
          setContentView(list);
      }
  
      private static final String[] FILES_PROJECTION = new String[] {
              MediaStore.Files.FileColumns._ID,
              MediaStore.Files.FileColumns.DISPLAY_NAME,
              MediaStore.Files.FileColumns.DATA
      };
  
      public class Custom_Adapter extends SimpleCursorAdapter {
  
          private Context mContext;
          private Context appContext;
          private int layout;
          private Cursor cr;
          private final LayoutInflater inflater;
  
          public Custom_Adapter(Context context,int layout, Cursor c,String[] from,int[] to,int flags) {
              super(context,layout,c,from,to,flags);
              this.layout=layout;
              this.mContext = context;
              this.inflater=LayoutInflater.from(context);
              this.cr=c;
          }
  
          @Override
          public View newView (Context context, Cursor cursor, ViewGroup parent) {
              return inflater.inflate(layout, null);
          }
  
          @Override
          public void bindView(View view, Context context, Cursor cursor) {
              super.bindView(view, context, cursor);
              TextView tv1 = (TextView)view.findViewById(R.id.tv1);
              TextView tv2 = (TextView)view.findViewById(R.id.tv2);
  
              int index1 = cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns._ID);
              int index2 = cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.DATA);
  
              tv1.setText(cursor.getString(index1));
              tv2.setText(cursor.getString(index2));
          }
  
      }
  }
  </code></pre>
  
  <p>XML:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:orientation="horizontal" &gt;
  
      &lt;TextView
          android:id="@+id/tv1"
          android:layout_width="50dp"
          android:layout_height="wrap_content"
          android:text="text1"/&gt;
  
      &lt;TextView
          android:id="@+id/tv2"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="text2"/&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>