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写一个微信差不多的那个列表界面,可是为什么我的内容显示不出来,只显示出来我的图片跟标题,为什么这个内容出不来?详解必采纳