八月没有晚山茶 2022-01-08 23:05 采纳率: 61.1%
浏览 33
已结题

使用SimpleAdapter制作列表时列表显示不出来,但是感觉数据没问题

这是activity中的代码:
public class News extends AppCompatActivity implements View.OnClickListener {

    private ListView listView;
    private List<Map<String, Object>> listitem = new ArrayList<Map<String, Object>>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news);
        ImageButton sendRequest = (ImageButton) findViewById(R.id.buttonwddt);
        //创建一个simpleAdapter
        SimpleAdapter myAdapter = new SimpleAdapter(this, listitem, R.layout.new2, new String[]{"uname", "content"}, new int[]{R.id.item_name, R.id.item_content});
        listView = (ListView) findViewById(R.id.list_wddy);
        listView.setAdapter(myAdapter);
        sendRequest.setOnClickListener(this);
    }

    //@Override
    public void onClick(View v) {
        if(v.getId() == R.id.buttonwddt) {
            sendRequestWithOkHttp();
        }
    }

    private void sendRequestWithOkHttp() {
        //开启线程来发起网络请求
        new Thread(new Runnable() {
            @Override
            public void run() {
                //String uid = "1001";
                try {
                    OkHttpClient client = new OkHttpClient();
                    RequestBody requestBody = new FormBody.Builder()
                            .add("action","dt")
                            .add("uid","1001")
                            .build();
                    Request request = new Request.Builder()
                            .url("http://192.168.102.186:8080/ateeth/newsServlet")
                            .post(requestBody)
                            .build();
                    Call call = client.newCall(request);
                    call.enqueue(new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {

                        }

                        @Override
                        public void onResponse(Call call, Response response) throws IOException {
                            String responseData = response.body().string();
                            System.out.println("1");
                            System.out.println(responseData);
                            showResponse(responseData);
                            /*Gson gson = new Gson();
                            List<Dynamic> dynamicList = gson.fromJson(responseData, new TypeToken<List<Dynamic>>(){}.getType());
                            System.out.println(dynamicList);
                            showResponse(dynamicList);*/
                        }
                    });

                } catch (Exception e){
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private void showResponse(final String response) {
    //private void showResponse(final List<Dynamic> dynamicList) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                //在这里进行UI操作
                Gson gson = new Gson();
                List<Dynamic> dynamicList = gson.fromJson(response, new TypeToken<List<Dynamic>>(){}.getType());
                System.out.println(dynamicList);
                for (int i = 0; i < dynamicList.size(); i++){
                    HashMap<String, Object> showitem = new HashMap<String, Object>();
                    showitem.put("uname", dynamicList.get(i).getUname());
                    showitem.put("content", dynamicList.get(i).getContent());
                    listitem.add(showitem);
                }
                System.out.println(listitem);
                //创建一个simpleAdapter
                SimpleAdapter myAdapter = new SimpleAdapter(getApplicationContext(), listitem, R.layout.new2, new String[]{"uname", "content"}, new int[]{R.id.item_name, R.id.item_content});
                listView.setAdapter(myAdapter);
                Log.d("刷新","刷新成功");

            }
        });

    }


}

相关的xml文件:
activity_news.xml
<?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"
    android:orientation="vertical"
    tools:context=".News">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="#49B0EE">

        <ImageButton
            android:id="@+id/buttonfh"
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:layout_marginTop="50dp"
            android:layout_marginLeft="20dp"
            android:background="@drawable/fanhui" />

        <TextView
            android:id="@+id/woxx"
            android:textColor="#333333"
            android:textStyle="bold"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="115dp"
            android:layout_marginTop="50dp"
            android:text="我的消息"
            android:textSize="20sp"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="125dp"
        android:background="#ffffff"
        android:orientation="horizontal">


        <ImageButton
            android:id="@+id/buttonwddt"
            android:layout_width="60dp"
            android:layout_height="80dp"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_marginTop="15dp"
            android:background="@drawable/wddt" />

        <ImageButton
            android:id="@+id/buttongz"
            android:layout_width="60dp"
            android:layout_height="80dp"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_marginTop="15dp"
            android:background="@drawable/guanzhu" />

        <ImageButton
            android:id="@+id/buttonz"
            android:layout_width="60dp"
            android:layout_height="80dp"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_marginTop="15dp"
            android:background="@drawable/zan" />

        <ImageButton
            android:id="@+id/buttonpl"
            android:layout_width="60dp"
            android:layout_height="80dp"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_marginTop="15dp"
            android:background="@drawable/pinglun" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#E6E6E6"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="380dp"
            android:layout_height="match_parent"
            android:background="#ffffff"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="10dp"
            android:orientation="vertical">

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

        </LinearLayout>

    </LinearLayout>

</LinearLayout>

news2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <!--用于显示每一条自己的动态消息-->
    <TextView
        android:id="@+id/item_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <TextView
        android:id="@+id/item_content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>

这是android studio运行的截图:

img

在代码中把相关数据打印了,感觉数据没啥问题。

但是列表就是一点都显示不出来。

纠结了一天,不知道该怎样改。
  • 写回答

1条回答 默认 最新

  • pumpkin的玄学 2022-01-08 23:43
    关注

    adapter抽出来 在获取到数据之后 不要重新创建adapter只需要改变原来adapter里面的数据即可 然后调用adapter的notify刷新数据

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 3月9日
  • 已采纳回答 3月1日
  • 创建了问题 1月8日