简体   繁体   中英

Android Studio ListView Custom

I recently started developing for android and now im stuck! I created a listview but the standard colour is black, now i want to be able to show the text in whatever colour i want. This is the activity.

public class DisplayMalePage extends ActionBarActivity {

String[] maleArray = { "a","b","c"};

   private ListView maleListView;
   private ArrayAdapter maleArrayAdapter;



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

    maleListView = (ListView) findViewById(R.id.maleList);
    maleArrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, maleArray);
    maleListView.setAdapter(maleArrayAdapter);
  }

But this (i think due to simple_list_item_1 ) gives me a black colour. Also i would like the input of my array to be strings so it wil be easier to change language.

Eventually i would like to have a list with 2 top texts, a dividing bar and then the rest of the list (they will all be clickable).

I hope someone understands what i mean haha.

You cannot change any part of the listviews layout using the default ArrayAdapter. You need to define your own CustomArrayAdapter.

http://www.vogella.com/tutorials/AndroidListView/article.html#adapterown

This link is a very useful guide on how to do that.

Create your own layout which you will use in your list item. Make sure it contains a Textview with id text1 . Something like this:

layout/my_list_item.xml

<TextView 
    android:id-"@+id/text1"
    android:textColor="@android:color/white"
    ...... />

Then use this layout in your ArrayAdapter intialization instead of android.R.layout.simple_list_item_1 .

maleArrayAdapter = new ArrayAdapter(this, R.layout.my_list_item, maleArray);

Note: If you want something more complex other than showing a simple text, you should use custom ArrayAdapter , as @user3567040 have pointed out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM