Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/191.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 带有ListView的ConstraintLayout不';不显示任何数据_Java_Android_Xml_Listview_Android Constraintlayout - Fatal编程技术网

Java 带有ListView的ConstraintLayout不';不显示任何数据

Java 带有ListView的ConstraintLayout不';不显示任何数据,java,android,xml,listview,android-constraintlayout,Java,Android,Xml,Listview,Android Constraintlayout,我构建了一个简单的应用程序,它由一个FloatingActionButton和一个Snackbar组成。 当用户点击晶圆厂时,实际日期应显示在应用程序的主要内容中 不幸的是,当我点击晶圆厂时,只有Snackbar在工作列表项不显示任何内容 我的代码怎么了 MainActivity.java: public class MainActivity extends AppCompatActivity { ArrayList<String> listItems = new Array

我构建了一个简单的应用程序,它由一个
FloatingActionButton
和一个
Snackbar
组成。 当用户点击晶圆厂时,实际日期应显示在应用程序的主要内容中

不幸的是,当我点击晶圆厂时,只有Snackbar在工作<代码>列表项不显示任何内容

我的代码怎么了

MainActivity.java:

public class MainActivity extends AppCompatActivity {
    ArrayList<String> listItems = new ArrayList<String>();
    ArrayAdapter<String> adapter;
    private ListView myListView;

    @Override
    protected  void onStart() {
        super.onStart();
        myListView = findViewById(R.id.listView);
        adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, listItems);
        myListView.setAdapter(adapter);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                addListItem();
                Snackbar.make(view, "Item added to list", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private void addListItem(){
        SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss MM/dd/yyyy", Locale.US);
        listItems.add(dateFormat.format(new Date()));
        adapter.notifyDataSetChanged();
    }
}
片段的XML:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
    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=".FirstFragment">

    <ListView
        android:id="@+id/listView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        tools:layout_editor_absoluteX="117dp"
        tools:layout_editor_absoluteY="332dp" />

</androidx.constraintlayout.widget.ConstraintLayout>

我没有运行您的代码,但总体上看起来不错。正在将项添加到列表和
adapter.notifyDataSetChanged()之后将被调用。但是有一个小项目,您可以直接在适配器上调用
add
,它会通知您有关更改的信息

ArrayAdapter的平台源代码:

public void add(@Nullable T object) {
    synchronized (mLock) {
        if (mOriginalValues != null) {
            mOriginalValues.add(object);
        } else {
            mObjects.add(object);
        }
        mObjectsFromResources = false;
    }
    if (mNotifyOnChange) notifyDataSetChanged();
}
突出的一点是,
ListView
布局看起来不正确:

<androidx.constraintlayout.widget.ConstraintLayout 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=".FirstFragment">    
    <ListView
        android:id="@+id/listView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        tools:layout_editor_absoluteX="117dp"
        tools:layout_editor_absoluteY="332dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
[可能
根据您的要求包装内容
。]

还要确保在
ConstraintLayout
中添加一些约束。如果不确定,请按设计编辑器顶部的“推断约束”,或使用更简单的
ViewGroup
容器。但是现在出现在
工具中的那些工具:“
名称空间仅由Android Studio的“设计”选项卡使用。它们不适用于实际设备

例如:

app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"

因为有你们这样的人,编程世界变得更好;))非常感谢你的订婚:)祝你一切顺利
<androidx.constraintlayout.widget.ConstraintLayout 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=".FirstFragment">    
    <ListView
        android:id="@+id/listView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        tools:layout_editor_absoluteX="117dp"
        tools:layout_editor_absoluteY="332dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"