Selected item ListView Background change - java

I have a ListView which after clicking on an item I want it to be permanently selected, so some other action can be taken depending on what button is pressed next. A bit like a RadioBox but within the list view. So when pressed, the background stays yellow and I keep a store of which item is selected. At the moment I have it when it is clicked the the background changes, but haveing weird behavior with when selected and I scroll the ListView the selected item changes.
Code I have:
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < titles.size(); i++){
HashMap<String, String> map = new HashMap<String, String>();
map.put("name", titles.get(i));
mylist.add(map);
}
SimpleAdapter mSchedule = new SimpleAdapter(this, mylist, R.layout.listcell,
new String[] {"name"}, new int[] {R.id.txtItemName});
ListView listView1 = (ListView) findViewById(R.id.ListView01);
listView1.setAdapter(mSchedule);
listView1.setTextFilterEnabled(true);
listView1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
Log.v("Test", v.toString());
v.setBackgroundResource(R.drawable.newbackground);
}
});
}

The views in a ListView get recycled so if the selected view goes off the screen it is probably being re-used for one of the current visible items. That might be the weird behavior you are describing? If you want to do multiple potential actions based on a item selection usually that is done either by a long-press + showing context menu.
What is the weird behavior?

Track the selected items in a HashSet. Override the SimpleAdapter.getView() method to assign the background resource based on the selectedItems HashSet.
final HashSet<String> selectedItems = new HashSet<String>();
SimpleAdapter mSchedule = new SimpleAdapter(this, mylist, R.layout.listcell,new String[] {"name"}, new int[] {R.id.txtItemName}) {
#Override
public View getView(int position, View v, ViewGroup parent) {
if(v!= null) {
String title = titles.get((int)this.getItemId(position));
if(selectedItems.contains(title)) {
v.setBackgroundResource(R.drawable.newbackground);
} else {
v.setBackgroundResource(0);
}
}
return super.getView(position, v, parent);
}
};
ListView listView1 = (ListView)findViewById(R.id.listView1);
listView1.setAdapter(mSchedule);
listView1.setTextFilterEnabled(true);
listView1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
String title = titles.get((int)id);
if(selectedItems.contains(title)) {
v.setBackgroundResource(0);
selectedItems.remove(title);
} else {
v.setBackgroundResource(R.drawable.newbackground);
selectedItems.add(title);
}
}
});

Related

Android, how do I dynamically update a ListView item and all subsequent items?

I have a ListView that displays an ArrayList that is dynamically created using an adapter. However, certain elements of each list item view are calculated based on previous item values. I am using Intents to open another activity where the user can edit a selected list item, and the updates are passed back to the main activity. In the main activity I've placed the getIntent, and the associated setters, after the ArrayList is generated and before the adapter. When the main activity is first created the adapter correctly calculates all list view items. But when the user accepts updates in the edit activity and returns to the main activity, only the selected list item is updated. Having the entire list cycle through and update would be fine (it will never be a very long list), but I'm a little surprised that only the selected list item is getting updated. I expected that either the adapter would run as it does when the activity is first created and all items would get updated, or that it wouldn't run at all and none would get updated.
public class MainActivity extends AppCompatActivity {
private final Context thisContext = MainActivity.this;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView planListView = findViewById(R.id.plan_listview);
final ArrayList<ItemProfile> planSteps = BuildPlan();
if(getIntent().getExtras() != null)
{
int stepNumber = getIntent().getIntExtra("stepNumber", 0);
ItemProfile thisStep = (ItemProfile) getIntent().getSerializableExtra("itemProfile");
planSteps.get(stepNumber-1).setDepth(thisStep.getDepth());
planSteps.get(stepNumber-1).setTime(thisStep.getTime());
planSteps.get(stepNumber-1).setInterval(thisStep.getInterval());
}
ItemsListAdapter planAdapter = new ItemsListAdapter(this, planSteps);
planListView.setAdapter(planAdapter);
planListView.setOnItemClickListener(
new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int pos, long l)
{
int index = pos-1;
Intent i = new Intent(thisContext, EditItemActivity.class);
i.putExtra("stepNumber", pos);
i.putExtra("stepProfile", planSteps.get(index));
if (index > 0)
{
i.putExtra("groupStart", planSteps.get(index-1).getGroupEnd());
}
startActivity(i);
}
}
);
}
}
Update... I've added the mainActivity code. It creates an ArrayList plan (I'm using a BuildPlan method to populate a dummy plan while I'm developing) then checks for an intent that is returning an updated plan step. If an intent exists the specified step is updated in the plan. The list adapter is then created and set. Finally the clickListener is created and set.
I've done something relatively similar but I used dynamic spinners and listviews from a database.
Here is the code. Basically you invalidate the list view, reset the data and call notifyDataSetChanged() on the adapter.
public ListView lv;
Spinner suburbSpinner;
ArrayAdapter<String> suburbAdapter;
ArrayList<String> suburbs = new ArrayList<>();
ArrayList<Resource> resources = new ArrayList<>();
ArrayAdapter<Resource> arrayAdapter;
public void updateList(String type, String suburb, String businessType, int suburbPos) {
DatabaseHelper db = new DatabaseHelper(this, "fairCanberraDB", null, 1);
lv = findViewById(R.id.list);
// Reset suburb spinner, get new list view resources.
lv.invalidateViews();
resources = db.resourceQuery(type, suburb, businessType);
System.out.println("resource: " + resources);
ArrayList<Resource> suburbQuery = db.resourceQuery(type, "All", businessType);
Spinner suburbSpinner = findViewById(R.id.suburbSpinner);
suburbs.clear();
suburbs.add("All");
for (int x = 0; x < suburbQuery.size(); x++) {
if (suburbs.contains(suburbQuery.get(x).getSuburb())) {
continue;
} else {
suburbs.add(suburbQuery.get(x).getSuburb());
}
}
db.close();
suburbAdapter.notifyDataSetChanged();
arrayAdapter.notifyDataSetChanged();
ArrayAdapter<Resource> arrayAdapter = new ArrayAdapter<Resource>(
this,
android.R.layout.simple_list_item_1,
resources);
lv.setAdapter(arrayAdapter);
if(suburbPos < suburbSpinner.getCount())
{ suburbSpinner.setSelection(suburbPos);}
setSpinnerListener(suburbSpinner);
}
// Register listener for a spinner
public void setSpinnerListener(Spinner spinner)
{
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
Spinner typeSpinner = (Spinner) findViewById(R.id.typeSpinner);
Spinner businessTypeSpinner = (Spinner) findViewById(R.id.businessTypeSpinner);
Spinner suburbSpinner = (Spinner) findViewById(R.id.suburbSpinner);
String businessType = businessTypeSpinner.getSelectedItem().toString();
if(businessType.contains("Private"))
{
businessType = "private user";
}
updateList(typeSpinner.getSelectedItem().toString(), suburbSpinner.getSelectedItem().toString(),
businessType, suburbSpinner.getSelectedItemPosition());
}

How to get Item from POJO in listview onClick()?

I have a POJO, which describes some model (Item?) and some custom Adapter.
I am setting my adapter for ListView and then, in onItemClick() I want to get value of one of variables which I had added Into the Item.
How I can reach this?
In my code I am doing something like:
private List<SomeItem> items = new ArrayList();
items.add(new SomeItem(firstValueString, secondValueBitmap, thirdValueString));
SomeAdapter adapter = new SomeAdapter(this, R.layout.list_item, items);
listView.setAdapter(adapter);
listView.setOnItemClickListener(
...
#Override
public void onItemClick(){
//How to reach for example firstValueString value of currently clicked item??
}
)
Use android.widget.AdapterView.OnItemClickListener:
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Log.d(LOG_TAG, "itemClick: position = " + position + ", id = "
+ id);
SomeItem item = items.get(position); // specific item
}
});
Based on Android SDK documentation:
Implementers can call getItemAtPosition(position) if they need to access the data associated with the selected item.
If you implement your adapter completely then you can get item like bellow:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Item item = (Item)parent.getItemAtPosition(position);
}
});

How do I know which list item was clicked?

I have a ListView filled with items. I'd like to set an onClickListener to show details about the item that was clicked. How can I determine which item was clicked within the onClickListener? The View, or v has a lot of methods available that I've looked through and don't see anything pertaining to getting the object clicked on.
//populate the activity list
ListView teamsListView = (ListView) activity.findViewById(R.id.teamsListView);
ArrayList<HashMap<String, String>> listData = new ArrayList<HashMap<String, String>>();
HashMap<String, String> listItem;
for (TeamSet teamSet : response.getTeamSetList()) {
listItem = new HashMap<String, String>();
listItem.put("name", teamSet.getName());
//listItem.put("teamCount", Integer.toString(teamSet.getTeams().size()));
listData.add(listItem);
}
teamsListView.setAdapter(
new SimpleAdapter(
context,
listData,
R.layout.teams_list_row,
new String[]{"name"},
new int[]{R.id.teamsTeamListName}
)
);
//show details on a team
TextView team = (TextView) activity.findViewById(R.id.teamsTeamListName);
team.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//how do I know which item was clicked? I want to load more details on the item...
}
});
You use a OnItemClickListener not a onCLickListener
You do something like this:
final ListView lv = (ListView) findViewById(R.id.ListView01);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> myAdapter, View myView, int myItemInt, long mylng) {
String selectedFromList =(String) (lv.getItemAtPosition(myItemInt));
}
});
Hope this helped.

Custom SimpleAdapter only shows Sample Text Android

Before making my own SimpleAdapter object because I wanted to change the color of the rows, I was just using new SimpleAdapter(...). Now that I am using my own custom SimpleAdapter, the row color is changing, but my text is not getting updated. I have called adapter.notifyDataSetChanged(), but it is still showing only the sample text- "TextView". As I said, everything was working fine when I didn't create my own adapter. I suspect it might have something to do with the order I am initializing things:
public class AddScreen extends Activity implements OnClickListener,
OnItemClickListener, OnItemLongClickListener {
SimpleAdapter adapter;
List<HashMap<String, String>> painItems = new ArrayList<HashMap<String, String>>();
ListView listthings;
int[] to;
String[] from;
public void onCreate(Bundle savedInstanceState) {
...
listthings = (ListView) findViewById(R.id.listthings);
from = new String[] { "row_1", "row_2" };
to = new int[] { R.id.row1, R.id.row2 };
adapter = new Adapter(this, painItems, R.layout.mylistlayout,
from, to);
listthings.setAdapter(adapter);
...
}
public class Adapter extends SimpleAdapter{
HashMap<String, String> map = new HashMap<String, String>();
public Adapter(Context context, List<? extends Map<String, String>> data,
int resource, String[] from, int[] to) {
super(context, data, resource, from, to);
}
#Override
public View getView(int position, View convertView, ViewGroup parent){
View row = convertView;
if (row == null) {
LayoutInflater mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = mInflater.inflate(R.layout.mylistlayout, parent, false);
}
row.setBackgroundColor(0xFF0000FF);
TextView rw1 = (TextView)findViewById(R.id.row1);
// TextView rw2 = (TextView)findViewById(R.id.row2);
rw1.setText(map.get(position));
return row;
}
}
// to add the item, put it in the map, and add the map into the list
private void addItem() {
HashMap<String, String> map = new HashMap<String, String>();
map.put("row_1", row1);
map.put("row_2", row2);
map.put("row_3", painLevelString);
map.put("row_4", painLocation);
map.put("row_5", timeOfPainString);
map.put("row_6",textTreatmentString);
painItems.add(map);
adapter.notifyDataSetChanged();
}
EDIT:Added Code
This is how I am getting the data from the intent(onActivityResult()), placed before the addItem Code:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == 1) {
row1 = data.getStringExtra("com.painLogger.row1");
row2 = data.getStringExtra("com.painLogger.row2");
painLevelString = data.getStringExtra("com.painLogger.painLevel");
painLocation = data.getStringExtra("painLocation");
timeOfPainString = data.getStringExtra("com.painLogger.painTime");
textTreatmentString = data
.getStringExtra("com.painLogger.treatment");
addItem();
}
}
*Also, just in case this is relevant the order of placement is this: onCreate() -> custom Adapter class -> onActivityResult() -> addItem()* **
Here is a screenshot of what it looks like. The two TextView fields in each item should be filled with info(which they were, until I did this).
If it worked previously with just using new SimpleAdapter(...) then in your getView(...) implementation change the first line to this:
View row = super.getView(position, convertView, parent);
And see if that is what you're expecting. Take out the LayoutInflater stuff too.
In getView(), about where you are setting the row background, you should also set the text for the TextView.
Calling notifyDataSetChanged(), doesn't automagically set your texts right, it just causes the ListView to redraw the visible rows with the new data...practically calling getView() for each row that needs a refresh.
I also suggest setting the background color from the mylistlayout.xml file, and if the getView() function starts taking on a few findViewByID's, you should also consider using a "view holder" approach like in this sample: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List14.html
You need to set the text in getView(). Like this:
public View getView(int position, View convertView, ViewGroup parent){
TextView text;
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.mylistlayout, parent, false);
text = (TextView) convertView.findViewById(R.id.more_list_text);
}
convertView.setBackgroundColor(0xFF0000FF);
text.setText(map.get(position));
return convertView;
}
Also, and this is VERY important - store you map as a member variable of the SimpleAdapter
ie, put this line at the top of your object definition:
HashMap<String, String> map = new HashMap<String, String>();

How to update expandable list view when adapter's data changes

I have an activity that extends ExpandableListActivity. I use SimpleCursorTreeAdapter to fill ExpandableListView.
My layout contains list view and empty view.
On app start ExpandableListActivity automatically chooses the right view to display.
My steps:
App starts, there is no data. (empty view on the screen)
Insert some data into db.
Call adapter.notifyDataSetChanged(); But empty view is still on the screen and there is no any item in my list view.
Then I restart app:
List view appears. I expand all groups and scroll to the bottom.
I click on the item in the list. New activity appears.
Click back button. All groups are collapsed and we are at the top of the screen. Scroll position and expanded groups are not remembered.
Delete all data from db and call adapter.notifyDataSetChanged();
Child views have disappeared, but top-level groups are still visible.
Questions:
What can I do to replace empty view with list view?
What can I do to do to save state of groups and scroll position of the list view?
Tested on SDKs: 1.5r3, 1.6r1
Code:
public class MainActivity extends ExpandableListActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
dbHelper = new DBOpenHelper(this);
rubricsDbAdapter = new RubricsDBAdapter(dbHelper);
rubricsDbAdapter.open();
itemsDbAdapter = new ItemsDBAdapter(dbHelper);
itemsDbAdapter.open();
rubricsCursor = rubricsDbAdapter.getAllItemsCursor();
startManagingCursor(rubricsCursor);
// Cache the ID column index
rubricIdColumnIndex = rubricsCursor.getColumnIndexOrThrow(RubricsDBAdapter.KEY_ID);
// Set up our adapter
mAdapter = new MyExpandableListAdapter(rubricsCursor,
this,
android.R.layout.simple_expandable_list_item_1,
android.R.layout.simple_expandable_list_item_1,
new String[] {RubricsDBAdapter.KEY_NAME},
new int[] {android.R.id.text1},
new String[] {ItemsDBAdapter.KEY_NAME},
new int[] {android.R.id.text1});
setListAdapter(mAdapter);
}
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
Intent i = new Intent(this, ItemViewActivity.class);
i.putExtra(ItemsDBAdapter.KEY_ID, id);
startActivity(i);
return super.onChildClick(parent, v, groupPosition, childPosition, id);
}
private void updateMyData() {
int i;
int k;
long rubricId;
for (i = 1; i <= 5; i++) {
rubricId = rubricsDbAdapter.insert("rubric " + i);
for (k = 1; k <= 5; k++) {
itemsDbAdapter.insert("item " + i + "-" + k, rubricId);
}
}
mAdapter.notifyDataSetChanged();
}
private void deleteMyData() {
rubricsDbAdapter.deleteAll();
itemsDbAdapter.deleteAll();
mAdapter.notifyDataSetChanged();
}
public class MyExpandableListAdapter extends SimpleCursorTreeAdapter
{
public MyExpandableListAdapter(Cursor cursor, Context context, int groupLayout,
int childLayout, String[] groupFrom, int[] groupTo, String[] childrenFrom,
int[] childrenTo) {
super(context, cursor, groupLayout, groupFrom, groupTo, childLayout, childrenFrom,
childrenTo);
}
#Override
protected Cursor getChildrenCursor(Cursor notebookCursor) {
// Given the group, we return a cursor for all the children within that group
long id = notebookCursor.getLong(rubricIdColumnIndex);
Cursor itemsCursor = itemsDbAdapter.getRubricItemsCursor(id);
startManagingCursor(itemsCursor);
return itemsCursor;
}
}
}
So, the answer is:
Call cursor.requery() before calling adapter's notifyDataSetChanged() method.
Save groups state in activity's onPause() method and restore in onResume() method.
Re: Question # 2...
Why can't you simply call View.invalidateViews() and View.requestLayout() in that order?
If you already have your adapter set to the view, then you shouldn't need to requery, do you? By invalidating the view, and requesting the layout, you won't lose any of the states of your view.
For example, in your "onResume" method, invalidate your list view by calling something like "lv.invalidateViews()". Next, call "lv.requestLayout()".
Your "onCreate" method should still be responsible for setting up the adapters and linking an adapter to the view. To link the adapter, consider using "lv.setListAdapter()" instead of the former so that you aren't messing with the other views on the screen.

Categories