首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >同屏文字和网格图片列表

同屏文字和网格图片列表
EN

Stack Overflow用户
提问于 2017-04-05 07:55:27
回答 1查看 39关注 0票数 0

我正在遵循下面的教程http://www.devexchanges.info/2015/03/combining-gridview-and-listview-in-one.html

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

我不完全确定我应该在这里使用什么作为我的ListView ID。看起来我只能使用findViewbyID,但我更愿意给它一个字符串数组来填充列表视图。我是不是漏掉了什么简单的东西?

代码语言:javascript
复制
ArrayAdapter<String> adapter =new ArrayAdapter<String>(this, R.layout.item_grid, CORPS);
    ListView lv = (ListView)findViewById(????);
    lv.setAdapter(adapter);

以下是代码

代码语言:javascript
复制
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);

}

}

EN

回答 1

Stack Overflow用户

发布于 2017-04-05 09:16:58

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

代码语言:javascript
复制
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将是“列表”

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43219656

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档