質問

Having a problem with my program and list view. I have a simple list view with 2 items on it

Joe Bloggs


David Jones

I am trying to set the background color on the first element to green, however I keep getting a null pointer exception when I am using

namesList.getChildAt(0).setBackgroundColor(Color.GREEN);

also

 namesList.getChildCount(); 

is always returning 0 even though there are 2 items on the list.

I really dont understand why this is not working, is there some step I am missing.

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class LecturerLogsView extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lecturer_logs_view);
        ListView namesList = (ListView)findViewById(R.id.namesList);
        String names[]={"Joe Bloggs", "David Jones"};

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                this, android.R.layout.simple_list_item_1,
                names);
        namesList.setAdapter(adapter);

        namesList.getChildAt(0).setBackgroundColor(Color.GREEN);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.lecturer_logs_view, menu);
        return true;
    }

}

xml file

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    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=".LecturerLogsView" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <ListView
        android:id="@+id/namesList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1" >
    </ListView>

</RelativeLayout>
役に立ちましたか?

解決

setAdapter is an asynchronous call. After you call it, it will take some time to execute, that's why after assigning it you are getting values consistent with the "no data" state.

You need to modify your Adapter getView , that is the function called by the ListView everytime it needs a view. You need to read about it, it's the key to understand how Android widgets really work.

In your example, this will do it:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, listItems) {
       @Override
       public View getView(int position, View convertView, ViewGroup parent) {
              View view=super.getView(position, convertView, parent); // we get the original view
              if (position==0) view.setBackgroundColor(Color.GREEN);  // and change color if position==0
              return view;
       }
 };

... but again I recommend you to read carefully how this stuff works, find out a good tutorial on Adapters!

他のヒント

This is what I would try:

public class LecturerLogsView extends Activity {
    ArrayList<String> listItems;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);            
        listItems = new ArrayList<String>();
        listItems.add("Joe Bloggs \n");
        listItems.add("David Jones \n");
        setContentView(R.layout.activity_lecturer_logs_view);
        (ListView) getView().findViewById(R.id.namesList)

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                getActivity(), android.R.layout.simple_list_item_1,
                listItems);
        namesList.setAdapter(adapter);

        namesList.getChildAt(0).setBackgroundColor(Color.GREEN);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.lecturer_logs_view, menu);
        return true;
    }

}

remember to import ArrayList.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top