Android - TextView未显示在ListView

时间:2016-03-17 05:14:04

标签: java android database listview android-fragments

我在ListView中没有显示任何TextView的问题。我不确定它是布局还是java类本身的问题。这里有两个图像,一个是在数据输入数据库后运行的应用程序,另一个是应该看起来的样子。

ListView中每行数据的布局

ListView的片段(显示删除按钮但不显示TextViews)

enter image description here

row_layout.xml

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

    <TextView
        android:layout_width="123dp"
        android:layout_height="wrap_content"
        android:id="@+id/resultTeamNumber"
        android:text="Here Number"
        android:textSize="18dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:id="@+id/textPortcullis"
        android:text="Portcullis:"
        android:layout_above="@+id/textChevalFrise"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/resultPortcullis"
        android:text="Yes/No"
        android:singleLine="false"
        android:layout_below="@+id/resultTeamNumber"
        android:layout_marginLeft="110dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textChevalFrise"
        android:text="Cheval de Frise:"
        android:layout_below="@+id/resultPortcullis"
        android:layout_alignParentStart="true"
        android:singleLine="false" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/resultChevalFrise"
        android:text="Yes/No"
        android:singleLine="false"
        android:layout_below="@+id/resultPortcullis"
        android:layout_alignStart="@+id/resultPortcullis" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textMoat"
        android:singleLine="false"
        android:layout_below="@+id/textChevalFrise"
        android:layout_alignParentStart="true"
        android:text="Moat:" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/resultMoat"
        android:text="Yes/No"
        android:singleLine="false"
        android:layout_below="@+id/resultChevalFrise"
        android:layout_alignStart="@+id/resultChevalFrise" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textRamparts"
        android:text="Ramparts:"
        android:singleLine="false"
        android:layout_below="@+id/textMoat"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/resultRamparts"
        android:text="Yes/No"
        android:singleLine="false"
        android:layout_above="@+id/textDrawbridge"
        android:layout_alignStart="@+id/resultMoat" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textDrawbridge"
        android:text="Drawbridge:"
        android:singleLine="false"
        android:layout_below="@+id/textRamparts"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/resultDrawbridge"
        android:text="Yes/No"
        android:singleLine="false"
        android:layout_below="@+id/resultRamparts"
        android:layout_alignStart="@+id/resultRamparts" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textSallyPort"
        android:text="Sally Port:"
        android:singleLine="false"
        android:layout_below="@+id/textDrawbridge"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/resultSallyPort"
        android:singleLine="false"
        android:layout_below="@+id/resultDrawbridge"
        android:layout_alignStart="@+id/resultDrawbridge"
        android:text="Yes/No" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textRockWall"
        android:text="Rock Wall:"
        android:singleLine="false"
        android:layout_alignTop="@+id/resultPortcullis"
        android:layout_toEndOf="@+id/resultPortcullis"
        android:layout_marginStart="34dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/resultRockWall"
        android:singleLine="false"
        android:text="Yes/No"
        android:layout_marginStart="46dp"
        android:layout_above="@+id/resultRockTerrain"
        android:layout_toEndOf="@+id/textRockTerrain" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textRockTerrain"
        android:text="Rock Terrain:"
        android:singleLine="false"
        android:layout_below="@+id/textRockWall"
        android:layout_alignStart="@+id/textRockWall" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/resultRockTerrain"
        android:singleLine="false"
        android:text="Yes/No"
        android:layout_alignBottom="@+id/textRockTerrain"
        android:layout_alignStart="@+id/resultRockWall" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textLowBar"
        android:text="Low Bar:"
        android:singleLine="false"
        android:layout_above="@+id/resultRamparts"
        android:layout_alignStart="@+id/textRockTerrain" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/resultLowBar"
        android:singleLine="false"
        android:text="Yes/No"
        android:layout_below="@+id/resultRockTerrain"
        android:layout_alignStart="@+id/resultRockTerrain" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#fff"
        android:background="@color/colorPrimary"
        android:text="Delete"
        android:id="@+id/buttonDelete"
        android:layout_below="@+id/textLowBar"
        android:layout_toEndOf="@+id/textLowBar"
        android:layout_above="@+id/resultSallyPort" />
</RelativeLayout>

ScoutFragment.java

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

/**
 * A simple {@link Fragment} subclass.
 */
public class ScoutFragment extends Fragment {

    FloatingActionButton addDataScout;
    ListView eListScoutInfo;
    SQLiteDatabase sqLiteDatabase;
    DatabaseHelper databaseHelper;
    Cursor cursor;
    ListScoutInfoAdapter listScoutInfoAdapter;

    public ScoutFragment() {
        // Required empty public constructor
    } //End of ScoutFragment

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_scout, null, false);
        view.setBackgroundColor(Color.WHITE);
        eListScoutInfo = (ListView) view.findViewById(R.id.listScoutInfo);
        listScoutInfoAdapter = new ListScoutInfoAdapter(getActivity().getApplicationContext(), R.layout.row_layout);
        eListScoutInfo.setAdapter(listScoutInfoAdapter);
        databaseHelper = new DatabaseHelper(getActivity().getApplicationContext());
        sqLiteDatabase = databaseHelper.getReadableDatabase();
        cursor = databaseHelper.getInformation(sqLiteDatabase);

        //Checks if information is available in cursor
        if(cursor.moveToFirst()){
            do {
                //Declare all strings
                String teamNumber, portcullis, chevalFrise, moat, ramparts, drawbridge, sallyPort, rockWall, rockTerrain, lowBar;

                //Get strings from cursor
                teamNumber = cursor.getString(cursor.getColumnIndex(DatabaseContract.NewDataInfo.COL_NUMBER));
                portcullis =  cursor.getString(cursor.getColumnIndex(DatabaseContract.NewDataInfo.COL_PORTCULLIS));
                chevalFrise = cursor.getString(cursor.getColumnIndex(DatabaseContract.NewDataInfo.COL_CHEVAL_FRISE));
                moat = cursor.getString(cursor.getColumnIndex(DatabaseContract.NewDataInfo.COL_MOAT));
                ramparts = cursor.getString(cursor.getColumnIndex(DatabaseContract.NewDataInfo.COL_RAMPARTS));
                drawbridge = cursor.getString(cursor.getColumnIndex(DatabaseContract.NewDataInfo.COL_DRAWBRIDGE));
                sallyPort = cursor.getString(cursor.getColumnIndex(DatabaseContract.NewDataInfo.COL_SALLY_PORT));
                rockWall = cursor.getString(cursor.getColumnIndex(DatabaseContract.NewDataInfo.COL_ROCK_WALL));
                rockTerrain = cursor.getString(cursor.getColumnIndex(DatabaseContract.NewDataInfo.COL_ROCK_TERRAIN));
                lowBar = cursor.getString(cursor.getColumnIndex(DatabaseContract.NewDataInfo.COL_LOW_BAR));

                //Get methods from DatabaseProvider
                DatabaseProvider databaseProvider = new DatabaseProvider(teamNumber, portcullis, chevalFrise, moat, ramparts,
                        drawbridge, sallyPort, rockWall, rockTerrain, lowBar);

                //Pass objects to add method
                listScoutInfoAdapter.add(databaseProvider);
                listScoutInfoAdapter.notifyDataSetChanged();
            } while (cursor.moveToNext());
        } //End of if statement

        //Setups Floating Action Button
        addDataScout = (FloatingActionButton) view.findViewById(R.id.fab);
        addDataScout.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                AddScoutDataFragment fragment = new AddScoutDataFragment();
                FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
                fragmentTransaction.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_left, R.anim.enter_from_left, R.anim.exit_to_right);
                fragmentTransaction.replace(R.id.fragment_container, fragment);
                fragmentTransaction.commit();
            } //End of onClick
        }); //End of setOnClickListener
        return view;
    } //End of onCreateView
} //End of class

我不相信问题在于数据库显示数据,因为ListView中的某些TextView甚至不使用数据库内部的数据。

3 个答案:

答案 0 :(得分:1)

这是主题问题,你的主题中的textcolor设置为白色可能会改变这些

<item name="android:textColorPrimary">@color/white</item>
<item name="android:textColorSecondary">@color/white</item>

将其更改为黑色

答案 1 :(得分:1)

你的row_layout.xml文件textview in set textcolor:

 <TextView
    android:layout_width="123dp"
    android:layout_height="wrap_content"
    android:id="@+id/resultTeamNumber"
    android:text="Here Number"
    android:textSize="18dp"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true" 
    android:textColor="@android:color/black"

/&GT;

答案 2 :(得分:1)

在最后的..............

中将你的适配器设置在listview上

enter image description here