为页面渲染上的listitem设置背景颜色

时间:2018-07-08 10:15:45

标签: android listview 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吗?我正在尝试在页面加载时将颜色设置为一项,但无法访问列表项。让我知道我是否完全偏离轨道或错过了一些小问题。

1 个答案:

答案 0 :(得分:0)

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

要使用externalclass属性和方法,您需要在内部匿名类内部的syntex下面。

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);
    }
});