Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to style individual listView items in android?
    primarykey
    data
    text
    <p>Sorry if this is a bit of a noobish question. I am used to writing windows 8 apps in JS and CSS, and i am still not very good with java.</p> <p>I am in the process of making my first android app (note taker). I have a standard ListView defined in my xml like this:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" &gt; &lt;ListView android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/notesListView" android:fastScrollEnabled="true" android:dividerHeight="2dp" android:layout_alignParentStart="true" android:divider="#b5b5ae" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" /&gt; &lt;/RelativeLayout&gt; </code></pre> <p>I have my note taker app working, but currently the UI is s**t.</p> <p>i am trying to apply styles to each individual listview items, but i cant find a noobs guide to doing that anywhere.</p> <p>I want it so that each new note that is added to the list, automatically gets a predefined style (like margins).</p> <p>Can anyone please tell me the best way of applying styles to each individual listview item from the XML (like setting margins, etc).</p> <p>And also does anyone know how to dynamically change the individual listview items (like i want to be able to make it so if a user selects a certain color from a dialog, that individual listview item changes BG color).</p> <p>EDIT 1:</p> <p>Hi, Here is the code for my listNotes Activity:</p> <pre><code> package com.fishingfon.notetakerui; public class ListNotesActivity extends Activity { @Override public boolean onContextItemSelected(MenuItem item) { AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo(); notes.remove(info.position); populateList(); //populateLateCustomAdapter(); return true; } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.context_menu, menu); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == RESULT_CANCELED){ return; } Serializable extra = data.getSerializableExtra("Note"); if (extra != null){ Note newNote = (Note)extra; if (editingNoteId &gt; -1){ notes.set(editingNoteId, newNote); editingNoteId = -1; } else { notes.add(newNote); }; populateList(); //populateLateCustomAdapter(); } } private List&lt;Note&gt; notes = new ArrayList&lt;Note&gt;(); private ListView notesListView; private int editingNoteId = -1; @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_list_notes); ListView notesListView = (ListView)findViewById(R.id.notesListView); notesListView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView&lt;?&gt; adapter, View view, int itemNumber, long id) { Intent editNoteIntent = new Intent(view.getContext(), EditNotesActivity.class); editNoteIntent.putExtra("Note", notes.get(itemNumber)); editingNoteId = itemNumber; startActivityForResult(editNoteIntent, 1); } }); registerForContextMenu(notesListView); notes.add(new Note("1 Note", "blah blah", new Date())); notes.add(new Note("2 Note", "blah blah", new Date())); notes.add(new Note("3 Note", "blah blah", new Date())); notes.add(new Note("4 Note", "blah blah", new Date())); notes.add(new Note("5 Note", "blah blah", new Date())); notes.add(new Note("6 Note", "blah blah", new Date())); notes.add(new Note("7 Note", "blah blah", new Date())); notes.add(new Note("8 Note", "blah blah", new Date())); populateList(); //populateLateCustomAdapter(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.list_notes, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { //notes.add(new Note("Added note", "blah", new Date())); //populateList(); Intent editNoteIntent = new Intent (this, EditNotesActivity.class); startActivityForResult(editNoteIntent, 1); return true; } // Populate Method private void populateList() { List&lt;String&gt; values = new ArrayList&lt;String&gt;(); for(Note note : notes) { values.add(note.getTitle()); } CustomListAdapter CustomAdapter = new CustomListAdapter(); notesListView.setAdapter(CustomAdapter); } </code></pre> <p>And Here is the apadter Class you gave me, but with my Variable names, etc:</p> <pre><code>class CustomListAdapter extends BaseAdapter { Context mContext; List&lt;String&gt; mList; public CustomListAdapter (Context context, List&lt;String&gt; values) { mList = values; mContext = context; } @Override public int getCount() { return mList.size(); } @Override public String getItem(int position) { return mList.get(position); } @Override public long getItemId(int position) { return 0; } // This method is called to draw each row of the list @Override public View getView(int position, View convertView, ViewGroup parent) { // here you inflate the layout you want for the row final View view = View.inflate(mContext, R.layout.item_list, null); return view; }} </code></pre> <p>My problem is in the populateList method. i have made the line bold above. I am not sure what code and parameters to put in my populateList method to create the new adapter.</p> <p>i was just wondering if you know what code i would use for the: CustomListAdapter CustomAdapter = new CustomListAdapter();</p> <p>and what parameters to pass in?.</p> <p>Thanks Heaps</p> <p>Thanks in advance </p> <p>Cheers Corey B</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload