【问题标题】:Blank list activity, ListView object is not null空白列表活动,ListView 对象不为空
【发布时间】:2017-04-03 07:32:01
【问题描述】:

我的 ListedActivity 是空白的,不会引发错误或异常。它只是坐在那里,什么都不做。

我的数据库不应该是空的,有一些条目。

public class ListedActivity extends Activity {

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

        LayoutInflater inflater = (LayoutInflater)   getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        LinearLayout mContainer = (LinearLayout) inflater.inflate(R.layout.activity_list, null);
        ListView listView = (ListView) mContainer.findViewById(R.id.listedactivity_list_view);
        DBHelper dbHelper = new DBHelper(getApplicationContext());
        ArrayList<MyLayoutObject> b = dbHelper.getAllResults();

        MyAdapter a = new MyAdapter(this, b);

        listView.setAdapter(a);

        listView.setOnItemClickListener(
            new AdapterView.OnItemClickListener(){
                @Override
                public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
                    Intent intent = new Intent(ListedActivity.this, ResultsActivity.class);
                    intent.putExtra("position", position);
                    intent.putExtra("id", id);
                    startActivity(intent);
                }
            }
        );
    }
}

活动列表应该写出我数据库中的所有结果。这是 .xml。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="smart.tuke.sk.makac.ListedActivity">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#eeeeee"
        android:id="@+id/listedactivity_list_view" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@android:id/empty"
        android:text="@string/no_data"
        android:visibility="gone" />
</LinearLayout>

这是我的行布局。每一行都应该是这样的。它的 .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="match_parent"
    >

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="61dp"
        android:orientation="vertical">

    <TextView
        android:id="@+id/date_recorded"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:textSize="18sp"
        android:textStyle="bold"
        android:textColor="#111111" />

    </LinearLayout>

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

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/duration_recorded" />

        <TextView
            android:layout_width="345dp"
            android:layout_height="wrap_content"
            android:id="@+id/distance_recorded" />

        <TextView
            android:layout_width="345dp"
            android:layout_height="wrap_content"
            android:id="@+id/pace_recorded" />

        <TextView
            android:layout_width="345dp"
            android:layout_height="wrap_content"
            android:id="@+id/calories_recorded" />
    </LinearLayout>

</LinearLayout>

MyLayoutObject 是 5 个带有 getter 的字符串,仅此而已

帮帮我,Overflow Kenobi,你是我唯一的希望!

编辑:适配器代码

class MyAdapter extends BaseAdapter{

    private LayoutInflater inflater;
    private ArrayList<MyLayoutObject> objects;

    private class ViewHolder {
        TextView date;
        TextView duration;
        TextView distance;
        TextView pace;
        TextView calories;
    }

    MyAdapter(Context context, ArrayList<MyLayoutObject> objects) {
        inflater = LayoutInflater.from(context);
        this.objects = objects;
    }

    public int getCount() {
        return objects.size();
    }

    public MyLayoutObject getItem(int position) {
        return objects.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if(convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.row_layout, null);
            holder.date = (TextView) convertView.findViewById(R.id.date_recorded);
            holder.distance = (TextView) convertView.findViewById(R.id.distance_recorded);
            holder.duration = (TextView) convertView.findViewById(R.id.duration_recorded);
            holder.pace = (TextView) convertView.findViewById(R.id.pace_recorded);
            holder.calories = (TextView) convertView.findViewById(R.id.calories_recorded);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.date.setText(objects.get(position).getDate());
        holder.duration.setText(objects.get(position).getDuration());
        holder.distance.setText(objects.get(position).getDistance());
        holder.pace.setText(objects.get(position).getPace());
        holder.calories.setText(objects.get(position).getCalories());
        return convertView;
    }
}

还有适配器的.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="match_parent"
    >

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="61dp"
        android:orientation="vertical">

    <TextView
        android:id="@+id/date_recorded"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:textSize="18sp"
        android:textStyle="bold"
        android:textColor="#111111" />

    </LinearLayout>

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

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/duration_recorded" />

        <TextView
            android:layout_width="345dp"
            android:layout_height="wrap_content"
            android:id="@+id/distance_recorded" />

        <TextView
            android:layout_width="345dp"
            android:layout_height="wrap_content"
            android:id="@+id/pace_recorded" />

        <TextView
            android:layout_width="345dp"
            android:layout_height="wrap_content"
            android:id="@+id/calories_recorded" />
    </LinearLayout>

</LinearLayout>

【问题讨论】:

  • 尝试在 setContentView(R.layout.activity_list) 之后移动 listview.setAdapter
  • 我转移了它们,活动仍然是空白

标签: android listview layout


【解决方案1】:

您正试图在膨胀的自定义视图中“找到”android.R 视图。您正在膨胀的布局不包含此视图(此类视图仅出现在预定义布局内,例如android.R.layout.simple_list_item

另外,请在活动的 XML 中考虑这一行:android:id="@android:id/list"

您并没有真正为元素分配 ID。这样做:android:id="@+id/listedactivity_list_view"(重要的是@+id)。

现在,在您的活动中,执行以下操作:ListView listView = (ListView) mContainer.findViewById(R.id.listedactivity_list_view);

通过这样做,您实际上是在引用视图中的实际元素,而不是引用布局中不存在的某些“幽灵元素”。

编辑:更仔细地重新阅读您的代码后,我终于找到了问题......看看这些行:

setContentView(R.layout.activity_list);

LayoutInflater inflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout mContainer = (LinearLayout) inflater.inflate(R.layout.activity_list, null);
ListView listView = (ListView) mContainer.findViewById(R.id.listedactivity_list_view);

在第 1 行(sn-p)中,您将内容视图设置为特定的布局资源。但是,在第 3 行 (mContainer) 中,您再次膨胀此布局 - 因此,当您在第 4 行执行 findViewById 时,您实际上是在膨胀视图中寻找某些东西,而不是实际显示的视图。因此,您的问题的解决方案是删除 inflater 和 mContainer 变量:

setContentView(R.layout.activity_list);

ListView listView = (ListView) findViewById(R.id.listedactivity_list_view);

【讨论】:

  • 它仍然是空白的,即使我做了你提到的更改。
  • 您确定您的适配器按预期工作吗?能否请您也给我们适配器代码?
  • 好的,我不确定确切的问题...但在尝试其他任何操作之前,请在您的 ID 中添加一个“+”(您错过了一些,例如这个:@ 987654328@,你需要到处都有@+id)。
  • 我在所有 id 中添加了“+”。奇怪的是,Android Studio 之前没有警告我这些。不过,它仍然是空白的。
  • 将代码编辑为最​​新的,还有什么我可以尝试的吗?
猜你喜欢
  • 2014-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-08
  • 2013-12-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多