Android - 以编程方式创建2列ListView

时间:2015-10-18 18:04:33

标签: android android-listview

下面的代码将创建一个2列的ListView。 是否可以在没有xml的情况下以编程方式创建它。

代码:

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));
        }

    }
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/tv1"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:text="text1"/>

    <TextView
        android:id="@+id/tv2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="text2"/>

</LinearLayout>

2 个答案:

答案 0 :(得分:0)

将GridView与android:numColumns="2"一起使用示例:

在GridView布局中显示从A到Z的字符。很简单,它应该是解释性的。

1.1 Android布局文件 - res / layout / main.xml

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gridView1"
    android:numColumns="2"
    android:gravity="center"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

</GridView>

1.2活动

package com.mkyong.android;

import android.app.Activity;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View;
import android.widget.AdapterView.OnItemClickListener;

public class GridViewActivity extends Activity {

    GridView gridView;

    static final String[] numbers = new String[] { 
            "A", "B", "C", "D", "E",
            "F", "G", "H", "I", "J",
            "K", "L", "M", "N", "O",
            "P", "Q", "R", "S", "T",
            "U", "V", "W", "X", "Y", "Z"};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        gridView = (GridView) findViewById(R.id.gridView1);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, numbers);

        gridView.setAdapter(adapter);

        gridView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v,
                int position, long id) {
               Toast.makeText(getApplicationContext(),
                ((TextView) v).getText(), Toast.LENGTH_SHORT).show();
            }
        });

    }

}

您还可以通过以下方式在GridView中以编程方式设置列数:

 gridview.setNumColumns(n);

答案 1 :(得分:0)

可以创建布局而不使用xml,但不应该。我将指出您的另一个问题。 Android - Create app with pure Java and no XML?是否有一个特定的原因可以避免XML布局以及Android的约定?