<少女> 2022-11-24 20:12 采纳率: 90.9%
浏览 3
已结题

这里我就是想要用listView写一个微信差不多的那个列表界面,可是为什么我的内容显示不出来,只显示出来我的图片跟标题,为什么这个内容出不来?详解必采纳


package com.example.studytest04;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //创建数组保存图片
        int[] imageId = new int[]{R.drawable.touxiang2,R.drawable.touxiang3,R.drawable.touxiang6,R.drawable.touxiang7};
        //同理创建标题与内容
        String[] title = new String[]{"【欢】","第五开黑交流群","S唯品会女神优惠福利群","微信团队"};
        String[] content = new String[]{"[图片]", "lily:世界杯你们看了吗?","记得早点睡.:没得","你有一笔收款到账"};
        List<Map<String, Object>> listitem = new ArrayList<Map<String, Object>>();
        for (int i=0;i<imageId.length;i++) {
            Map<String, Object> map = new HashMap<String,Object>();
            map.put("image",imageId[i]);
            map.put("name",title[i]);
            map.put("content",content[i]);
            listitem.add(map);
        }
        SimpleAdapter simpleAdapter=new SimpleAdapter(this,listitem,R.layout.main,new String[]{"name","image"},
                new int[]{R.id.title,R.id.image});
        ListView listView=findViewById(R.id.listview);
        listView.setAdapter(simpleAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Map<String, Object> map=(Map<String, Object>)parent.getItemAtPosition(position);
                Toast.makeText(MainActivity.this,map.get("name").toString(),Toast.LENGTH_SHORT).show();
            }
        });
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </ListView>
</LinearLayout>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:id="@+id/image"
        android:layout_width="100dp"
        android:layout_height="100dp" />
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="100dp">
        <TextView
            android:id="@+id/title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    <TextView
        android:id="@+id/content1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    </LinearLayout>
</LinearLayout>

这里我就是想要用listView写一个微信差不多的那个列表界面,可是为什么我的内容显示不出来,只显示出来我的图片跟标题,为什么这个内容出不来?详解必采纳

img

  • 写回答

1条回答 默认 最新

  • 码上就疯 2022-11-25 08:14
    关注

    这个地方写错了,漏写了,应该这样写

    SimpleAdapter simpleAdapter=new SimpleAdapter(this,listitem,R.layout.main,new String[]{"name","image","content"},
                    new int[]{R.id.title,R.id.image,R.id.content1});
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 12月3日
  • 已采纳回答 11月25日
  • 创建了问题 11月24日

悬赏问题

  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan