Android Studio ListView Custom - java

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.

Related

When press back button listview going top. How to fix that?

In my codes there is two fragment. List page and detail page. There is lot of alphabetic item on list page. And when i clicked some item opening the detail page. But when i press back button, the list doesn't stay same position. It goes to the top. I searched some solution but i think that's not common issue. I couldn't find different answers. Is there anyone to help me?
At first glance, it looks like it's one of your calls to: dictionaryFragment.resetDatasource(source);
Which is resetting the Adapter on your ListView to a brand new ArrayAdapter. This will reset the ListView to the top because the adapter is an entirely new object. The ListView no longer has any idea about the old ArrayAdapter and thus resets to the top.
public void resetDatasource (ArrayList<String> source){
mSource=source;
adapter = new ArrayAdapter<String>(getContext(),R.layout.kelimelistesi, mSource);
dicList.setAdapter(adapter);
}
Rather than calling
adapter = new ArrayAdapter<String>(getContext(),R.layout.kelimelistesi, mSource);
dicList.setAdapter(adapter);
You should do more like this:
public class DictionaryFragment ... {
private Adapter mAdapter;
public View onCreateView(...) {
// Init the layout here
// Put some other ArrayList here if you have one initially, else an empty one to start
mAdapter = new ArrayAdapter<String>(getContext(), R.layout.kelimelistesi, new ArrayList<String>());
dicList.setAdapter(mAdapter);
}
public void resetDatasource(ArrayList<String> source) {
mAdapter.clear();
mAdapter.addAll(source);
mAdapter.notifyDataSetChanged();
}
...
}
This difference now is that the same mAdapter instance is bound to dicList the whole time, but only the underlying data to display changed. Think of a ListView/RecyclerView's Adapter as a class that takes some input data and binds it to some UI elements. In this case, each String in the ArrayAdapter is displayed in some UI element like a TextView or what not.
ALSO important: note that we call notifyDataSetChanged() AFTER we have cleared the old data and added all the new data. This will help avoid flickering and scrolling issues from trying to elements one at a time.

How to change style of specific rows in android ListView

I have a listview in my app, which is declared in XML, then the content is passed to it in code based on a string array which I have declared in XML. I'm new to android programming, but I was wondering how I could make the text in certain listview elements bold and darken the listview background behind it.
I have my set names as an array in my strings.xml:
<string-array name="setsArray">
<item>set1</item>
<item>set2</item>
<item>set3</item>
<item>set4</item>
<item>set5</item>
<item>set6</item>
</string-array>
I have my listview declared in the activity's layout.xml
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView"
android:layout_below="#+id/app_bar"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
And in the code of the activity I have this:
final ListView setList = (ListView) findViewById(R.id.listView);
setList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.setsArray)));
setList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int itemPosition = position;
String itemValue = (String) setList.getItemAtPosition(itemPosition);
if (!itemValue.equals("SPANISH")){
Intent intent = new Intent(view.getContext(), SetViewActivity.class);
intent.putExtra("value", itemValue);
startActivity(intent);
}
}
});
Note that this code takes the name of the set that was clicked on, and passes it onto the next activity with the intent so it knows what to display. If possible I need to keep this intact or replace it with another system which sends something which I can use to distinguish the sets in the next activity.
because you don't use a custom adapter (I guess), try this please:
<string-array name="setsArray">
<item>set1</item>
<item>set2</item>
<item>set3</item>
<item>set4</item>
<item> <![CDATA[ <b>set5</b> ]]> </item>
<item>set6</item>
</string-array>
I hope android using Html.fromHtml() by default :)
Android:Make characters bold in xml
Please watch The World of ListView. It explains the important aspects of ListViews and how to make an adapters for your ListView. Pay particular attention to the part about using getItemViewType() and getViewTypeCount().
I think you'll have to implement custom ListAdapter. In getView() method you'll be able to change style of item based on index.
As a side note, I don't see a reason why you wouldn't use RecyclerView. It's a more flexible and up to date choice for creating lists.
With RecyclerView it could be done like this:
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
holder.mTextView.setText(mDataset[position]);
if(position == 5) holder.mTextView.setTypeface(null, Typeface.BOLD);
}

Having an issue with a NullPointer and ListView

This is the code which is causing the error. Sorry I changed so much from my original post, but I have placed all the code in one place now. Tell me if this is a terrible idea.
I get my error on the line that sets the myArrayAdapter for my ListView
public class DisplayCaf extends Activity implements OnNavigationListener {
private static final String STATE_SELECTED_NAVIGATION_ITEM = null;
//Holds items.
ArrayList<String> menuArray;
//String adapter for ListView
ArrayAdapter<String> myArrayAdapter;
//set listView
ListView listView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pager_caf);
listView = (ListView)findViewById(R.id.listView);
menuArray = new ArrayList<String>();
//set up the adapter for listView
myArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, menuArray);
//connect adapter to feed info to listView
listView.setAdapter(myArrayAdapter);
}
}
It's hard to tell what the problem is with the limited information you provided: the fact you have posted a lot of code, as you said, doesn't mean you have provided the right one (for instance you pasted that big switch that probably doesn't matter, and you didn't pasted the layout...).
My best guess is that the call to
public Object instantiateItem(View collection, int position);
happens right after the call:
setContentView(R.layout.pager_caf);
and therefore the adapter never gets a chance of being initialized.
If you want to have a clearer picture of what's happening, help yourself first: place a couple of Log.d here and there (right at the start of instantiateItem and before and after setContentView might be a good start...), reduce your problem to the essential parts (bisecting it until only the bug remains) and post the code that remains (as well as the logs).

Android: Dealing with CheckBox in a ListView

I'm using a SimpleAdapter to fill my ListView with below code.
SimpleAdapter adapter = new SimpleAdapter(this,
saleDriver.getOutstandings(clientId),
R.layout.outstanding_list_row, new String[] { "sale_id",
"sale_date", "invoice_number", "sale_total", },
new int[] { R.id.tt_check_box, R.id.tt_invoice_date,
R.id.tt_invoice_no, R.id.tt_invoice_tot });
setListAdapter(adapter);
According to above code, i have bind sale_id with CheckBox (R.id.tt_check_box) in the listview. When i run the program, value of checkboxes displayed right of the CheckBox as text. but i don't want to display them.
My actual need is, when user checked checkboxes, i need to get sale_ids bind with them.
How could i access sale_ids bind with checked checkboxes in my java programe ?
use
android.R.layout.simple_list_item_multiple_choice
I've attached code to obtain listview that works with multichoice checkboxes..
public class MultiChoiceActivity extends Activity {
String[] choice = { "Choice A",
"Choice B", "Choice C", "Choice D", "Choice E"};
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
choiceList = (ListView)findViewById(R.id.list);
ArrayAdapter<String> adapter
= new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice,
choice);
choiceList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
choiceList.setAdapter(adapter);
}
Refernce Link
Override the getView() function in the SimpleAdapter and use the value of sale_id to check/uncheck the checkbox. Then use this custom adapter for your list.
EDIT:
In looking at another answer, my refined guess is that you need the Multiple Choice solution rather than this (since you need to find out the selections). This solution is more to display the checkbox based on existing data. If you still need this, let me know and I will post the sample code once I have access to my code.

Dynamic Spinners in Android (General Workflow Question)

Like this one, I've seen a few how-to's on this site, but truthfully, I'm not really getting it.
I want one spinner's content to be based upon a previous spinner selection, like in a States and Cities scenario. In general terms, what is the workflow? Are the results of the second spinner filtered based on the first spinner, or is the second spinner pointing to a completely different list based on the first spinner?
For my own simple learning project, I've built several string-arrays in the strings.xml (AL-Cities, AK-Cities, AR-Cities, etc). I'd like the city spinner to populate from the correct array based on a choice from the state spinner. But I'm wondering if instead I should just have one large multidimensional array of "Cities" that have a state abbreviation as an additional identifier, and then point the second spinner at that using the state abbreviation as a filter. It would seem like the former would provide better performance.
Any help (and code examples) would be greatly appreciated. I'm not new to programming (php mostly, so I guess scripting is more accurate), but I am new to java. My code so far with the spinners not linked is below with the second spinner pointing to an undifferentiated city_array.
Thank you!
public class Example1 extends Activity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.example1);
Spinner spinState = (Spinner) findViewById(R.id.spin_state);
ArrayAdapter<CharSequence> adapter3 = ArrayAdapter.createFromResource(
this, R.array.state_array, android.R.layout.simple_spinner_item);
adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinState.setAdapter(adapter3);
Spinner spinCity = (Spinner) findViewById(R.id.spin_city);
ArrayAdapter<CharSequence> adapter4 = ArrayAdapter.createFromResource(
this, R.array.city_array, android.R.layout.simple_spinner_item);
adapter4.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinCity.setAdapter(adapter4);
}
}
You could try and get the position from your first spinner, the one you've selected and then populate your second spinner after retrieving the proper array based on that position.
You have to listen for your first adapter being changed:
spinner. setOnItemSelectedListener(new MyOnItemSelectedListener());
class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
String choice = parent.getItemAtPosition(pos).toString();
populateSecondSpinnerMethod(choice)
}
}
public void onNothingSelected(AdapterView parent) { // Do nothing.
}
}

Categories

Resources