How to dynamically make TextView's setVisibility(View.GONE)? - android

I am displaying a List that contains 3 Textview for name, start_date and day.Now I want that if any start_date value is empty, Listview must not display that TextView. I know the functionality of "GONE". But In my case I am using hashmap. I don't know where to apply "GONE".
Here is my code:
ListAdapter adapter = new SimpleAdapter(this, hashList,
R.layout.list_item, new String[] { TAG_NAME,
TAG_START_DATE, TAG_DAYS }, new int[] {
R.id.name, R.id.startdate, R.id.days });
Here R.id.name, R.id.startdate, R.id.days are Textviews.
Here, start_date may and may not be available, so R.id.startdate must be applied Visibility Gone if hashmap don't has value for that.

You have to create your own adapter, extending BaseAdapter for instance. Then, when the getView() callback is called you can check your condition and show the info accordingly

Related

CursorAdapter adapts to the wrong fields

I subclassed SimpleCursorAdapter for displaying a list. In my row layout file, I have 3 textView, but I just want to map the data to 2 of them. I explicitly left out txt2, but it keeps being displayed with the data on it. I can fix this by manually removing txt2 from the layout file, then all good. But just out of curiosity, what was wrong with the adapter in the fist place?
Tks.
String []from = new String[] {
DbField.FIELD1,
//DbField.FIELD2,
DbField.FIELD3
};
// Fields on the UI to which we map
int[] to = new int[] { R.id.txt1, /*R.id.txt2,*/ R.id.txt3 };
adapter = new CustomCursorAdapter(this.getActivity()
, R.layout.row_entry, null, from,
to, 0);
setListAdapter(adapter);

set textview color programaically for the item under section header Listview

I have implemented a Section header Listview where i inflate a layout which have three textviews in it as item.i have referred this example here to achieve it.
My code is
adapter.addSection(monthitems2[l], new SimpleAdapter(this, security, R.layout.phone_row,
new String[] {"Title", "Caption","val1" }, new int[] { R.id.tvContact, R.id.tvMobile,R.id.tvMail }));
I have a condition where i need to change text color of ID "R.id.tvContact".I havent defined the textview anywhere in thelayout.i have just referred it as simpleadapter parameter.How to change the textview color the default is white.Any suggestions answers are highly appreciated.
From the reference tutorial, you have
SeparatedListAdapter adapter = new SeparatedListAdapter(this);
adapter.addSection("Array test", new ArrayAdapter<String>(this,
R.layout.list_item, new String[] { "First item", "Item two" }));
adapter.addSection("Security", new SimpleAdapter(this, security, R.layout.list_complex,
new String[] { ITEM_TITLE, ITEM_CAPTION }, new int[] { R.id.list_complex_title, R.id.list_complex_caption }));
ListView list = new ListView(this);
list.setAdapter(adapter);
this.setContentView(list);
Get a handle to your TextView, then set its color from that handle. You need to Post the
commands to the message queue, in a runnable, b/c at this point in the code the ListView hasn't yet rendered the TextView's that the adapter points to (that will happen sometime after OnCreate exits).
list.Post(() =>
{
TextView tv = list.FindViewById<TextView>(R.id.tvContact);
tv.SetTextColor(Color.Red);
});
I don't think you need to qualify FindViewById with list. b/c you set the content view with
this.setContentView(list);
(above code in C# for MonoDroid, easily convertable to Java).
Posting runnables in Java/Android: How to run a Runnable thread in Android?
Posting in runnables puts the code in the message queue, a key, undocumented construct in Android. Anytime you get a null reference from an activity when modifying Views, try this as a first step.

Make list within another app

I did listview tutorial. I can't see how to integrate the list making program into another program I have.
I want to fill an array with sensor input values in one method of my public class, and then display the array as a list after that.
Is it possible to call the list making code as a function, in response to some user activity within the 'sensor' method? How do I do this?
Excuse me if this is stupid, I am a beginner to Java.
Any advice appreciated.
AFAIK, the way you construct a listview is via List<? extends Map<String, ?>>.
if you do want to construct it this way, here's how to do it.
fill the data inside a List<Map<String,?>>, then use a SimpleAdapter (or other adapter) to concatenate the list to ListView. suppose you have a List<Map<String,?>> named mList,
ListView mListView = (ListView)findViewById( --listView id-- );
String[] mFrom = { -key1-, -key2- };
int[] mTo = {android.R.id.text1, android.R.id.text2 };
SimpleAdapter mAdapter = new SimpleAdapter(getApplicationContext(), mList, android.R.layout.simple_list_item_2, mFrom, mTo);
mListView.setAdapter(mAdapter);
listView id is the id of the listview on the xml, key1 and key2 respectively are the data you want to input inside the listview. however this one used android's default feature of list.
if you want to use your own listview template (i.e. you have more than 2 data inside),
define the xml of your custom listview (customlist.xml) then change the mTo variable to match your textviews
int[] mTo = {R.id.-listTextView1-,R.id.-listTextView2-,R.id.-listTextView3-};
and point to that xml on this line
SimpleAdapter mAdapter = new SimpleAdapter(getApplicationContext(), mList, R.layout.--customlistname--, mFrom, mTo);

ListView toggle choice mode

I have a ListView (technically its a ListFragment, but I dont think that changes the question) which displays its results uisng a SimpleCursorAdapter.
adapter = new SimpleCursorAdapter(
getActivity(),
R.layout.item,
null,
new String[] {"Name"},
new int[] {R.id.Name}
);
setListAdapter(adapter);
(The Cursor is set/swapped in onStart).
I want to give the user the ability to toggle choice mode (which will than be used to display only the checked items). A bit like an SMS application when you put messages in "batch mode" which allows you to select multiple conversations which can the be deleted, saved etc. Do I set a new adapter or just change the existing ones properties? the ListView will need re-drawing anyway to reflect changes.
You have to do two things.
Use the ListView's setChoiceMode method to enable MULTI_SELECT on the ListView.
Create a 'Selector' to define a 'selected' or 'checked' state and set this as a style element on your R.layout.item.
Like Vikram Bodicherla answer.
setChoiceMode to enable MULTI_SELECT on the ListView.
Set in your custom item layout a background that show the selected state like:
android:background="?android:attr/activatedBackgroundIndicator"
This is a suprisingly simple answer. The problem was I was calling the method in onCreate before onCreateView had inflated the List. Stupdily this was just to test the function. Once I moved it to onStart and later into its intended location in the FragmentActivity the following worked fine:
public void toggleAdapter()
{
ListView listView = getListView();
if (listView.getChoiceMode() == ListView.CHOICE_MODE_MULTIPLE)
{
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
adapter = new SimpleCursorAdapter(
getActivity(),
R.layout.item,
dashboardCursor,
new String[] {"Name"},
new int[] {R.id.Name}
);
}
else
{
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
adapter = new SimpleCursorAdapter(
getActivity(),
R.layout.dashboard_list_item_multiple_choice,
dashboardCursor,
new String[] {"Name"},
new int[] {R.id.Name}
);
}
listView.setAdapter(adapter);
}

How to update SimpleAdapter in Android

Is it possible to update a SimpleAdapter? I have a list of data and a footer that says "See Next Results" When that list item is clicked I capture the event and get new data. I then want to replace the data in the ListView with this new data but I can't figure out how to do it. Any Ideas? I don't want to use an ArrayAdapter, cause as far as I can see the items can only hold one string where I need it to hold multiple strings and ints.
Update: According to del116, you can indeed give SimpleAdapter a mutable map and then manually call the adapter's notifyDataSetChanged method when you need the list to update. However, my point below stands about the documentation of SimpleAdapter specifying that it is for static data; using it for mutable data is going counter to its design, so if you use this technique I would be sure to check on whether it continues to work in new Android releases as they emerge.
(Original commentary follows:)
If you look at the SimpleAdapter description it says it is "An easy adapter to map static data to views defined in an XML file." I've added the emphasis -- put simply, SimpleAdapater isn't built for use with data that changes; it handles static data only. If you can't use an ArrayAdapter because your data has more than a single bit of text, then you will either have to build your own custom ListAdapter, or put your data in a DB and use one of the CursorAdapters.
As a last resort, if you don't need much performance, you could update a ListView backed by a SimpleAdapter by building a whole new SimpleAdapter instance any time your data changes and telling the list view to use it via setListAdapter.
ListView lv= (ListView) findViewById(R.id.loglist);
ArrayList<Map<String, String>> list = buildData();
String[] from = { "time", "message" };
int[] to = { R.id.logtime, R.id.logmessage };
adapter = new SimpleAdapter(getApplicationContext(), list,R.layout.log_list_row, from,to);
lv.setAdapter(adapter);
Call this function each time to update the ListView. Keep in Mind that You have to update the list variable with new Data..
Hope this Helps..:)
SimpleAdapter is meant for static data, so your performance may vary. The best solution is probably to switch to a different type of adapter, such as ArrayAdapter, or make a new SimpleAdapter every time you change the dataset.
I was not able to get notifyDataSetChanged() to work on updating my SimpleAdapter, so instead I tried first removing all views that were attached to the parent layout using removeAllViews(), then adding the ListView, and that worked, allowing me to update the UI:
LinearLayout results = (LinearLayout)findViewById(R.id.results);
ListView lv = new ListView(this);
ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
SimpleAdapter adapter = new SimpleAdapter( this, list, R.layout.directory_row,
new String[] { "name", "dept" }, new int[] { R.id.name, R.id.dept } );
for (...) {
HashMap<String, String> map = new HashMap<String, String>();
map.put("name", name);
map.put("dept", dept);
list.add(map);
}
lv.setAdapter(adapter);
results.removeAllViews();
results.addView(lv);

Categories

Resources