【问题标题】:list view not working properly in android列表视图在android中无法正常工作
【发布时间】:2017-06-27 20:52:30
【问题描述】:

当我没有在代码中添加任何延迟时,列表视图没有显示任何内容,但是当我添加大约 1 秒的延迟时,列表视图的结果会显示在屏幕上。它以这种方式在 android 版本 5.1 中工作,但是当我运行时android 7.1 nougat 中的代码在从服务器接收数据时,它不会在屏幕上显示任何内容,因为我可以通过敬酒看到它。

Entry1.xml(自定义适配器)

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="13dp"

    android:focusable="false"
    android:focusableInTouchMode="false"


    >

<ImageView
    android:layout_width="90dp"
    android:layout_height="50dp"
    android:src="@drawable/image"
    android:layout_gravity="center"

    android:focusable="false"
    android:focusableInTouchMode="false"
    />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"

    android:focusable="false"
    android:focusableInTouchMode="false"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"

        android:focusable="false"
        android:focusableInTouchMode="false">


        <TextView
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:text="Name"
            android:textAlignment="center"
            android:textColor="@color/black"
            android:textSize="14dp"

            android:focusable="false"
            android:focusableInTouchMode="false"/>


    <TextView
        android:id="@+id/name2"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:textAlignment="center"
        android:layout_marginLeft="15dp"
        android:background="#00000000"
        android:letterSpacing="0.1"
        android:textSize="16dp"
        android:inputType="text"
        android:textColor="@color/black"

        android:focusable="false"
        android:focusableInTouchMode="false"
        />




    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"

        android:focusable="false"
        android:focusableInTouchMode="false">

        <TextView
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:text="Company Name"
            android:textColor="@color/black"
            android:textSize="14dp"
            android:textAlignment="center"

            android:focusable="false"
            android:focusableInTouchMode="false"/>

    <TextView
        android:id="@+id/cname2"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:textAlignment="center"
        android:layout_marginLeft="15dp"
        android:background="#00000000"
        android:letterSpacing="0.1"
        android:textSize="16dp"
        android:inputType="text"
        android:textColor="@color/black"

        android:focusable="false"
        android:focusableInTouchMode="false"
        />

        </LinearLayout>



    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"

        android:focusable="false"
        android:focusableInTouchMode="false">


        <TextView
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:text="Position"
            android:textColor="@color/black"
            android:textSize="14dp"
            android:textAlignment="center"

            android:focusable="false"
            android:focusableInTouchMode="false"/>



    <TextView
        android:id="@+id/pos2"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:textAlignment="center"

        android:layout_marginLeft="15dp"
        android:background="#00000000"
        android:letterSpacing="0.1"
        android:textSize="16dp"
        android:inputType="text"
        android:textColor="@color/black"

        android:focusable="false"
        android:focusableInTouchMode="false"
        />

        </LinearLayout>

</LinearLayout>

Card.xml(主要活动)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/rlLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.hppc.business.Card">

<ListView
    android:id="@+id/lvUsers"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:clickable="true"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:descendantFocusability="blocksDescendants">

</ListView>

mainactivity.java

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    employee_id = getIntent().getStringExtra("employee_id");
    //Toast.makeText(getApplicationContext(),answer,Toast.LENGTH_SHORT).show();
    requestQueue1 = Volley.newRequestQueue(this);

    doit_function();

    setContentView(R.layout.activity_card);
    listView = (ListView) findViewById(R.id.lvUsers);
    listView.setClickable(true);



    populateUsersList();
}

【问题讨论】:

    标签: android listview android-database android-studio-2.3


    【解决方案1】:

    使用 RecyclerView 而不是 listView。

     recyclerView = (RecyclerView) findViewById(R.id.ur_view);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setHasFixedSize(true);
    

    将您的适配器设置为具有您想要的条件的recyclerview,无论是本地数据库还是服务器。

    【讨论】:

    • 我也试过 Recycler 视图,它也有同样的问题。
    • 先尝试添加静态值,看看结果,成功与否? @himanshu
    • 对于静态数据,它可以在列表视图和回收器视图中使用,但是当我尝试从服务器获取数据时,它们都不起作用。我观察到的另一个奇怪的事情是,如果我打开相机 Intent 并关闭它,数据就会出现在屏幕上。
    • 我认为您的回收站视图或列表视图没有问题,从服务器@himanshu 获取数据存在问题
    • 非常感谢解决它只需要在解析 JSON 后调用填充列表视图函数,而不是在 onCreate 中调用它。感谢您的所有帮助!
    猜你喜欢
    • 1970-01-01
    • 2019-10-24
    • 2015-01-09
    • 1970-01-01
    • 2017-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-01
    相关资源
    最近更新 更多