首页 > 解决方案 > 在页面渲染上为 listitem 设置背景颜色

问题描述

我有以下布局xml:

<?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=".ShowMessageList">

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fabMsgDel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="30dp"
        android:layout_marginEnd="32dp"
        android:clickable="true"
        app:backgroundTint="@color/colorPrimary"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="@+id/lstMessages"
        app:layout_constraintTop_toBottomOf="@+id/lstMessages"
        app:srcCompat="@android:drawable/ic_delete" />
    <ListView
        android:id="@+id/lstMessages"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toTopOf="@+id/fabMsgDel"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

另外,后面的代码是

package com.example.hp_pc.qc;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Canvas;
import android.graphics.Color;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckedTextView;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

    public class ShowMessageList extends AppCompatActivity {
        ListView lstMsgs;
        Cursor cursor, cursorDel;
        String selectedTxt;
        SQLiteDatabase database;
        String[] MessageLists;
        private int selected;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_show_message_list);
            lstMsgs = findViewById(R.id.lstMessages);

            database = new SampleSQLiteDBHelper(this).getReadableDatabase();
            MessageLists = new String[3];
            MessageLists[0] = SampleSQLiteDBHelper.MESSAGE_COLUMN_ID;
            MessageLists[1] = SampleSQLiteDBHelper.MESSAGE_COLUMN_NAME;
            MessageLists[2] = SampleSQLiteDBHelper.MESSAGE_IS_DEFAULT;

            readFromDB();
            //lstMsgs.getChildAt(selected).setBackgroundColor(Color.BLUE);
            lstMsgs.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    selected = position;
                    selectedTxt = (String) lstMsgs.getItemAtPosition(selected);
                    View lstMsgsChildAt = lstMsgs.getChildAt(selected);
                    lstMsgsChildAt.setBackgroundColor(Color.BLUE);
                }
            });

            setMessageLst();

        }

        private void setMessageLst() {
            String[] values = new String[cursor.getCount()];
            int cnt = 0;
            try {
                while (cursor.moveToNext()) {
                    values[cnt] = cursor.getString(cursor.getColumnIndex(SampleSQLiteDBHelper.MESSAGE_COLUMN_NAME));
                    if (cursor.getInt(cursor.getColumnIndex(SampleSQLiteDBHelper.MESSAGE_IS_DEFAULT)) == 1)
                    {
                        selected = cnt;
                //        lstMsgs.getChildAt(cnt).setBackgroundColor(Color.BLUE);
                    }
                    cnt = cnt + 1;
                }
            }finally {
                cursor.close();
            }

            ArrayAdapter<String> msgLSt = new ArrayAdapter<String (this,android.R.layout.simple_list_item_1, values);
            lstMsgs.setAdapter(msgLSt);
            lstMsgs.performItemClick(lstMsgs, selected, selected);
          }

        }

当我到达时lstMsgs.setOnItemClickListener,我正在null到达lstMsgs.getChildAt(selected)。这是在 之后调用的setMessageLst

你能帮我为什么我得到null。我试图在页面加载时将颜色设置为一项,但我无法访问列表项。让我知道我是否完全偏离轨道或遗漏了一些小点。

标签: androidlistviewlistitem

解决方案


如我所见,您在“ShowMessageList”类中​​创建了内部匿名“AdapterView.OnItemClickListener”。所以你不能在“AdapterView.OnItemClickListener”中使用“ShowMessageList”的任何属性或方法。

要使用外部类属性和方法,您需要在内部匿名类中使用以下语法。

OuterClassName.this.propertyormethod

请看下面更新的代码

lstMsgs.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        ShowMessageList.this.selected = position;
        selectedTxt = (String) ShowMessageList.this.lstMsgs.getItemAtPosition(ShowMessageList.this.selected);
        View lstMsgsChildAt = ShowMessageList.this.lstMsgs.getChildAt(ShowMessageList.this.selected);
        lstMsgsChildAt.setBackgroundColor(Color.BLUE);
    }
});

推荐阅读