Setup Listview to strikethrough item after click - android

good day, i have a problem here with my listview
i have a listview with custom arrayadapter which is have 12 item
i have already set the listview when click it's get strikethrough on the selected item
UPDATE
public class MainActivity extends AppCompatActivity {
private ListView mainList;
private final String[] listContent = {
"1. Get Ready",
"2. Second ",
"3. Third",
"4. Fourth",
"5. Fifth",
"6. Sixth",
"7. Seventh",
"8. Eight",
"9. Nineth",
"10. Ten",
"11. Eleven",
"12. Twelve",
"13. Look up number twelve",
"14. Its become strikethrough too",
"15. how to fix it?",
"16. Please help",
"17. Thanks",
"18. End" };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainList = (ListView) findViewById(R.id.listview2);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, listContent);
mainList.setAdapter(adapter);
mainList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view,
int position, long id) {
TextView text = (TextView) view;
text.setPaintFlags(text.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
}
});
}
}
my problem is, when i click the first item (number one) and then it become strikethrough but the 12 item (number twelve) it's also strikethrough to.
see my picture
clicking first item:
https://i.stack.imgur.com/M0nvm.jpg
number twelve also get strikethrough
https://i.stack.imgur.com/1hTPM.jpg

This is because it is the same View instance that is being used now that the first item is out of view.
For this purpose you will need to implement your own (Base)Adapter and store the information there as to which items should be strike through.
Then set the correct paint flags in the getView method as the views move in and out of view.

This is happening because of a view is reused. You need to store the position OR id of that object and provide stroke through effect accordingly. Hope it will help you out. For code reference or help please refer below link.It will definitely help you out
http://lalit3686.blogspot.com/2012/06/today-i-am-going-to-show-how-to-deal.html
https://www.codeproject.com/Questions/896462/Android-Need-to-save-checkbox-state-in-a-custom-ad

Related

When ListView is ready?

I want to change the background of a TextView, which is inserted in a ListView. The ListView contains a series of TextView, and the value of each one is retrieved from a simple List. Now I show you my snippet:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.town_selection);
list = (ListView) findViewById(R.id.list);
adapter = new ArrayAdapter<>(this, R.layout.string_item, App.towns);
list.setAdapter(adapter);
list.setOnItemClickListener(this);
adapter.notifyDataSetChanged();
list.invalidate();
// list.getCount() returns zero!!
// and so, list.getChildAt(...) returns null!!
}
App.towns is the list of String I was talking, and its length is greater than 0. When the app is running it seems ok, because the ListView is showing the items, but at the end of the onCreate I need to change the background color of some items, but the ListView seems not ready as I commented.
notifyDataSetChanged() and invalidate() doesn't help.
I'm not claiming that my answer is the best, but I have some idea and I'd like to share it.
If your string list of towns, as I understood, is unchangable let's do the following:
...
final Context context = this;
String [] towns = new String [] { "town_1", "town_2", "town_3" };
ListView list;
ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.town_selection);
list = (ListView) findViewById(R.id.list);
adapter = new ArrayAdapter<String>(this, R.layout.string_item, R.id.string_item_text, towns)
//R.id.string_item_text - id your TextView in item of your list
{
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
TextView txtView = (TextView) v.findViewById(R.id.string_item_text);
String sText = txtView.getText().toString();
if (sText.equals("town_1")) {
txtView.setBackgroundColor(Color.parseColor("#93C47D")); //#93C47D - just example
} else if (sText.equals("town_2")){
txtView.setBackgroundColor(Color.parseColor("#6FA8DC")); //#6FA8DC - just example
} else if (sText.equals("town_3")){
txtView.setBackgroundColor(Color.parseColor("#F6B26B"));//#F6B26B - just example
}
return v;
}
};
list.setAdapter(adapter);
...
It helped me in one of Android projects. If it will help you it'll be good.
In any way, leave your feedback.
You will need to write a custom adapter for your list view and change the color when creating each item in the list view.
This SO question - Custom Adapter for List View has great answers which should help you with how to create this custom adapter. You will not be able to do it with the ArrayAdapter as-is.
The best solution is to use a custom adapter instead of ArrayAdapter , then in the getView() method of custom adapter you can change the color of the textview
If, for some reason, you need to change your background is TextViev, the information about it should be somewhere to store and change the background is using methods in your custom adapter. Create your own custom adapter.

How to get the id resource id of a view in a List view

I have Listview which is showing different images of animals,birds,reptiles. The list view is working fine. Now I want when user click to any picture it should appear in the ImageView.The ImageView is just below the listview. so when ever user will click any image in the listview it should appear in the Imageview.
Also there is a button. Now I want to achieve that when user select any image and press Ok button that paticular image should show on the image view of other activity also. I Know I can send the Id through intent.putextra(), but the problem is first place how to get the id of particular picture.
Source code
public class MainActivity2 extends ActionBarActivity {
private TypedArray ListIcons;
private HorizontalListView listView;
private ArrayList<AnimalsListItems> SuitItems;
private AnimalsListAdapter adapter1 = null;
/** An array of strings to populate dropdown list */
String[] actions = new String[] {
"Bookmark",
"Subscribe",
"Share"
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity2);
listView = (HorizontalListView) findViewById(R.id.suits_list);
AnimalsItems = new ArrayList<AnimalsListItems>();
/** Create an array adapter to populate dropdownlist */
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_spinner_dropdown_item, actions);
/** Enabling dropdown list navigation for the action bar */
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
getSupportActionBar().setDisplayShowTitleEnabled(false)
;
getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle(" "+" "+" ");
/** Defining Navigation listener */
android.support.v7.app.ActionBar.OnNavigationListener navigationListener = new android.support.v7.app.ActionBar.OnNavigationListener() {
#Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
Toast.makeText(getBaseContext(), "You selected : " + actions[itemPosition], Toast.LENGTH_SHORT).show();
ListIcons = getResources()
.obtainTypedArray(R.array.ic_launcher);// load icons from
// strings.xml
for (int i = 0; i<=ListIcons.length(); i++) {
AnimalsItems.add(new AnimalsListItems(ListIcons.getResourceId(i,-1)));
}
adapter1 = new SuitsListAdapter(getApplicationContext(),SuitItems);
Log.v(adapter1.getCount()+"");
listView.setAdapter(adapter1);
return false;
}
};
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//With the position parameter, you can fetch the item at the clicked position like below. Cast it to whatever type your ListView is.
Object yourItem = (Object) listView.getItemAtPosition(position);
//Now you can assign the image to an imageview
}
});
/** Setting dropdown items and item navigation listener for the actionbar */
getSupportActionBar().setListNavigationCallbacks(adapter, navigationListener);
this.overridePendingTransition(R.anim.anim_slide_in_left,
R.anim.anim_slide_out_left);
}
}
For your ListView, you can set an onItemClickListener as below
ListView listView1 = (ListView) findViewById(R.id.listView1);
listView1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//With the position parameter, you can fetch the item at the clicked position like below.
AnimalsListItems yourItem = (AnimalsListItems) listView1.getItemAtPosition(position);
//Now you can assign the image to an imageview
}
}
Not sure what you mean by "id", if you're talking about drawable id's, path to image files, or just any way to identify your image. It depends a lot on how you populate your list. You didn't supply any code at all.
But since you have a ListView I suppose you have an Adapter, and that you're backing that adapter with some data, such as an array, database etc. When you click on an item, you get an onItemClick callback with the index of the item you clicked on. You should be able to use this index to find which image you clicked on from your data source.
Then when it comes to passing that to another activity, well, again it depends on how you represent your images but you should be able to pass whatever data you need to represent the image in an intent extra to the second activity.
You can use the setOnItemClickListener method of ListView to get witch item clicked.Because one item one image, you can get the target image.
1.Step
Rather getting resource id you can create one integer array containing resource id's like,
int resourceID[] = [R.drawable.image1, R.drawable.image2, ...]
Assign this array to your ListView image item
2.Step
Now can easily get the postion of selected list item or image using Lists on item click method
listView.setOnItemClickListener(new OnItemClickListener() {} )
3.Step
Just pass your ResourceID array & tapped position to next/target activity & use it, rather passing resourceID
or
Otherwise make your ResourceID as static, pass only tapped position to next Activity & use this static ResourceID from previous Activity
i.e -> yourImageView.setImageResource(FirstActivity.ResourceID[position])

ListView Selected position with setTextFilterEnabled

I have the following code that starts a listview with 100 options. Also it activates the software keyboard, for easy-searching of the options. If i choose an option, the position of the selected option stores in the string array items[]. The problem is that when i search an option by keyword, select it and then go back, it points at the current position not at the current value.
How to prevent this?
public class MyListActivity extends ListActivity {
String[] items;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
ListView lView = getListView();
lView.setChoiceMode(2);
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
lView.setTextFilterEnabled(true);
items = getResources().getStringArray(R.array.items1);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_checked, items));
}
public void onListItemClick(ListView lv, View v, int position, long id) {
lv.setItemChecked(position, lv.isItemChecked(position));
String selectedValue = (String) getListAdapter().getItem(position);
Toast.makeText(this, selectedValue, Toast.LENGTH_SHORT).show();
}
}
I think that you should create your own custom Adapter , and then in that adapter just simply override the getItem(int position) function to return the value that you need. If u haven't done it before then check out for instance this tutorial
As it turns out, having a filterable adapter doesn't play well with any choice mode (single or multiple). The only solution to this is to manually maintain the state of the selected items (a set or a list of strings in your case), and then simply loop through it every time you change the filter, in order to select the items you need. This has already been described in great detail here: Multiple Choice Searchable ListView
You obviously can't use the setTextFilterEnabled() method for this, you simply need to add an EditText in your layout and then set a TextWatcher on that in order to get all the necessary callbacks.

display only selected number of items in the list view in android

I am trying to display only selected items in list view.
public class Mact extends ListActivity {
TextView selection;
String[] items = { "this", "is", "a", "really", "record", "list",
"contains", "some", "data","sample" };
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainser);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_expandable_list_item_1, items));
selection = (TextView) findViewById(R.id.selection);
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
String text = " position:" + position + " " + items[position];
selection.setText(text);
}
}
this is my activity class which display a list view. items contains set of records in list view . items contains 10 records . I want to get list view only for starting 5 records. how can I implement it . help me out. thanks in advance.
we can restrict listview items from adapter that are using for listview
Only put 5 items in the adapter. Or set getCount() return value 5.
Do you really want only 5 to show? Or do you want the list to show in a smaller space? It's possible to show all ten in a small ListView simply by setting the height value in your layout.
And if you really only want five, could you not simply create a subset of your larger strings
and pass that into the adapter?

Stuck... need help.. Listview w/ Array Adapter

Ok, so I have this application that takes a adress strings (from values/xml) and parses it your current position) retuning a name, address and distance away. This is then picked up by an arrayadapter and put up on a listview. I cannot for the life of me get the list view to accept an onitemclick to start another activity, where I can launch a different view. I did have it where I was getting the row, name and address to show through to an alert dialog, but in my efforts to get it to launch an activity, I lost that.
So does anyone have any thoughts? I am using the following call to make my list and and arrays. This is stripped down, so assume I have all the imports and proper formatting. I know I am just missing something simple here...
public class Wf extends ListActivity {
private ArrayList<String> DistanceList;
private ArrayAdapter<String> aa;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
// Bind the ListView to an ArrayList of strings.
DistanceList = new ArrayList<String>();
ListView lv = (ListView)findViewById(R.id.ListView01);
aa = new ArrayAdapter<String>(getApplicationContext(),
R.layout.listbox_layout,
DistanceList);
lv.setAdapter(aa);
//Call to get distance... here
}
public void onListItemClick(ListView parent, View v,int position, long id) {
ListView lv = (ListView)findViewById(R.id.ListView01);
Toast.makeText(this, "You clicked", Toast.LENGTH_LONG).show();
}
From the ListActivity docs:
ListActivity has a default layout that
consists of a single, full-screen list
in the center of the screen. However,
if you desire, you can customize the
screen layout by setting your own view
layout with setContentView() in
onCreate(). To do this, your own view
MUST contain a ListView object with
the id "#android:id/list" (or list if
it's in code)
Your ListView does not have the correct ID. Your code is incomplete but I suspect the listener is not being registered with the ListView.

Categories

Resources