同一屏幕android上的文本列表和图像网格

时间:2017-04-04 23:55:27

标签: android android-layout

我正在关注以下教程http://www.devexchanges.info/2015/03/combining-gridview-and-listview-in-one.html

我正在尝试更改列表视图以使用字符串数组,并在此应用程序的底部显示文本而不是图像。

我不完全确定我应该在这里使用我的ListView ID。看起来我只能使用findViewbyID,但我宁愿给它一个字符串数组来填充该列表视图。我在这里错过了一些简单的东西吗?

ArrayAdapter<String> adapter =new ArrayAdapter<String>(this, R.layout.item_grid, CORPS);
    ListView lv = (ListView)findViewById(????);
    lv.setAdapter(adapter);

这是代码

public class ListViewActivity extends Activity {

private ExpandableHeightGridView gridView;
private ListView listView;

//drawables array for listview
private static final int[] corpImage = { R.drawable.apple, R.drawable.blackberry_logo,
        R.drawable.google, R.drawable.microsoft, R.drawable.mozilla, R.drawable.oracle };

static final String[] CORPS = new String[] { "Microsoft", "Google", "Apple" };

//drawables array for gridview
private static final int[] osImage = { R.drawable.bbos, R.drawable.firefox_os,
        R.drawable.ic_launcher, R.drawable.ios, R.drawable.tizen, R.drawable.ubuntu_touch,
        R.drawable.wpos, R.drawable.symbian };

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    listView = (ListView) findViewById(R.id.list);
    addGridViewAsListViewHeader();

    setAdapters();
}

/**
 * set header for listview (it's a gridview)
 */
@SuppressLint("InflateParams")
private void addGridViewAsListViewHeader() {
    View header = (View) getLayoutInflater().inflate(R.layout.layout_gridview, null);
    listView.addHeaderView(header);

    gridView = (ExpandableHeightGridView) header.findViewById(R.id.gridview);

    // set expand to show all gridview rows
    gridView.setExpanded(true);
}


/**
 * set adapters for list view and gridview
 */
private void setAdapters() {
    // convert int array to Integer array by apache common lang library
    Integer[] osImageList = ArrayUtils.toObject(osImage);
    Integer[] corpImageList = ArrayUtils.toObject(corpImage);

    //Added by me but not working correctly
    //ArrayAdapter<String> adapter =new ArrayAdapter<String>(this, R.layout.item_grid, CORPS);
    //ListView lv = (ListView)findViewById(R.id.image);
   // lv.setAdapter(adapter);

    // set listview and gridview adapters
    CustomAdapter gridViewAdapter = new CustomAdapter(this, R.layout.item_grid, R.id.image, osImageList);
    CustomAdapter listViewAdapter = new CustomAdapter(this, R.layout.item_grid, R.id.image, corpImageList);

    gridView.setAdapter(gridViewAdapter);
    listView.setAdapter(listViewAdapter);

}

}

1 个答案:

答案 0 :(得分:0)

如果您的目标是仅在每行中使用一个字符串,并将其显示为项目列表,则可以使用数组适配器执行此操作,如下所示。

ArrayList<String> companies = new ArrayList<>();
companies.add("Google");
companies.add("Microsoft");
// Find a reference to the {@link ListView} in the layout
ListView companiesListView = (ListView) findViewById(R.id.list);

// Create a new {@link ArrayAdapter} of earthquakes
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
        this, android.R.layout.simple_list_item_1, earthquakes);

// Set the adapter on the {@link ListView}
// so the list can be populated in the user interface
companiesListView.setAdapter(adapter);

在布局文件中,您应声明要显示此项列表的列表视图。在这种情况下,该列表视图的id将是“list”