创建ListView导致奇怪的行为

时间:2018-01-22 09:56:55

标签: android listview

我正在尝试在我的应用中创建listView。这样只有部分屏幕将用于listView,而另一部分将用作永久信息。我得到奇怪的输出,listView而不是显示所需的字符串,显示永久信息。

这是我的代码:

activity_main.xml中

<!-- Start of the permanent information -->
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="com.example.mti_student.kol_mila.MainActivity">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:id="@+id/header_image_id"
            android:layout_width="100dp"
            android:layout_height="90dp"
            android:layout_marginBottom="0dp"
            android:src="@drawable/logo"
            android:layout_centerHorizontal="true"/>

        <LinearLayout
            android:id="@+id/search_panel_id"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/header_image_id"
            android:orientation="vertical"
            android:layoutDirection="rtl">

            <TextView
                android:id="@+id/text_view_id"
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:textAlignment="center"
                android:text="Title"
                android:layout_marginBottom="5dp"/>


            <SearchView
                android:id="@+id/search_view_id"
                android:layout_width="250dp"
                android:layout_height="wrap_content"
                android:queryHint="Search"
                android:iconifiedByDefault="false"
                android:layout_gravity="center"
                android:queryBackground="@color/colorWhite"
                android:background="@color/colorPrimary" />

        </LinearLayout>
        <!-- End of permanent information -->

        <!-- This where I want my list view to appear --> 
        <FrameLayout
            android:id="@+id/fragment_layout_id"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/search_panel_id"
            android:background="@color/colorBeige"
            android:layout_marginTop="5dp"
            android:orientation="horizontal">

            <ListView android:id="@+id/list_view"
                android:layout_height="fill_parent"
                android:layout_width="fill_parent">
            </ListView>

        </FrameLayout>

    </RelativeLayout>

</android.support.constraint.ConstraintLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private List<String> filePaths = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        copyFromAssets();//Stores the information to filePaths

        ListView lv = findViewById(R.id.list_view);
        ArrayList<HashMap<String,String>> arrayList=new ArrayList<>();
        for (int i=0;i<filePaths.size() ;i++)
        {
            HashMap<String,String> hashMap=new HashMap<>();//create a hashmap to store the data in key value pair
            hashMap.put("file_name",filePaths.get(i));
            arrayList.add(hashMap);//add the hashmap into arrayList
        }

        String[] from={"name"};
        int[] to={R.id.topic_id};
        SimpleAdapter simpleAdapter=new SimpleAdapter(this,arrayList,R.layout.activity_main,from,to);
        lv.setAdapter(simpleAdapter);
    }

2 个答案:

答案 0 :(得分:0)

您正在使用主活动布局作为SimpleAdapter呈现的每个项目的布局 - 请参阅构造函数的第三个参数。相反,在实例化TextView之前,根据您的设置提供包含topic_id ID为SimpleAdapter的布局。

答案 1 :(得分:0)

也许可能是因为您使用与主活动相同的资源来在列表视图中显示信息 活性:

      SetContentView(R.layout.activity_main);

你应该使用你自己的视图来列表视图来膨胀

      SimpleAdapter simpleAdapter=new  simpleAdapter(this,arrayList,*R.layout.row*,from,to)