将文本和图像添加到ListView?

时间:2019-01-14 15:54:53

标签: android listview android-listview

我想在ListView的每个列表项中添加图像和文本。让我向您展示我的代码:

ArrayAdapter<String> arrayadapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, arrayListString);
listView.setAdapter(arrayAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener){
@override
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
...
}
}

我该如何解决?

感谢您的耐心和考虑。

1 个答案:

答案 0 :(得分:2)

请考虑使用RecyclerView,要使用recyclerview,您需要具有图像(或URL)列表和文本列表,甚至更好的是同时包含两者的对象。

关于您的活动或片段

var data = [
{
    "date": "1459468800000",                            // 1 April 2016 
    "values": [{
        "name": "US",
        "value": 72580613,
        "domainname": "com"
    }, {
        "name": "US",
        "value": 161645,
        "domainname": "nl"
    }]
}, {
    "date": "1467331200000",                            // 1 Juli 2016
    "values": [{
        "name": "US",
        "value": 73129243,
        "domainname": "com"
    }, {
        "name": "US",
        "value": 166152,
        "domainname": "nl"
    }]
}
]

var svg = d3.select("svg"),
    margin = {top: 20, right: 20, bottom: 30, left: 50},
    width = +svg.attr("width") - margin.left - margin.right,
    height = +svg.attr("height") - margin.top - margin.bottom;

var tooltip = d3.select("body").append("div").attr("class");

var x = d3.scaleBand().rangeRound([0, width]).padding(0.1),
    y = d3.scaleLinear().rangeRound([height, 0]);

var colours = d3.scaleOrdinal()
    .range(["#6F257F", "#CA0D59"]);

var g = svg.append("g")
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

    x.domain(data.map(function(d) { return d.values.domainname; }));
    y.domain([0, d3.max(data, function(d) { return d.values.value; })]);

    g.append("g")
        .attr("class", "axis axis--x")
        .attr("transform", "translate(0," + height + ")")
        .call(d3.axisBottom(x));

    g.append("g")
        .attr("class", "axis axis--y")
        .call(d3.axisLeft(y).ticks(25).tickFormat(function(d) { return parseInt(d / 1000) + "K"; }).tickSizeInner([-width]))
      .append("text")
        .attr("transform", "rotate(-90)")
        .attr("y", 6)
        .attr("dy", "0.71em")
        .attr("text-anchor", "end")
        .attr("fill", "#5D6971")
        .text("Aantal domeinen");

    g.selectAll(".bar")
        .data(data)
      .enter().append("rect")
        .attr("x", function(d) { return x(d.values.domainname); })
        .attr("y", function(d) { return y(d.values.value); })
        .attr("width", x.bandwidth())
        .attr("height", function(d) { return height - y(d.values.value); })
        .attr("fill", function(d) { return colours(d.values.domainname); })

其中的适配器是RecyclerviewAdapter和RecyclerViewHolder,因为您要执行的方法仅适用于Strings,而没有自定义视图。 请查看以下官方文档:https://developer.android.com/guide/topics/ui/layout/recyclerview