I am implementing ListView in my code. But When I click on the items, it does not respond respond to the click in any way. Could someone help me please? Thanks in advance .
Here is the code .
public class ListaActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.provacomunicazione.MESSAGE";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lsta);
Resources res = getResources();
String[] Authors = res.getStringArray(R.array.Lista_Nomi_E_Cognomi_Autori);
ArrayList<String> Autori = new ArrayList<String>();
for (String temp:Authors) {
Autori.add(temp);
}
Collections.sort(Autori);
ArrayList<String> AutoriLetteraSelezionata = new ArrayList<String>();
for (String temp:Autori) {
if (temp.charAt(0)=='A') {
AutoriLetteraSelezionata.add(temp);
}
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.textviewitem, AutoriLetteraSelezionata);
ListView listView = (ListView) findViewById(R.id.listView1);
listView.setAdapter(adapter);
listView.setClickable(true);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
CharSequence autore = "("+((TextView)view).getText()+")";
Intent i = new Intent(ListaActivity.this, SecondaryActivity.class);
i.putExtra(EXTRA_MESSAGE, autore);
startActivity(i);
});
}
}
You should define on all of the child objects in the item listview (TextView, ImageView etc.):
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
And for the root item RelativeLayout / LinearLayout and so, define:
android:clickable="false"
android:descendantFocusability="blocksDescendants"
android:focusable="false"
android:focusableInTouchMode="false"
If you won't define those, they will "catch" the click event.
And if you have a Custom listView adapter, just check that you override:
#Override
public boolean isEnabled(int position)
{
return true;
}
In my case a problem was in fact that a ListView contained HorizontalScrollViews.
HSV consumes clicks over items and doesn't return OnItemClick to the ListView.
I solved the problem when wrote an OnClickListener inside an adapter that returns a callback to the ListView. See here: https://stackoverflow.com/a/43653085/2914140.
In the customer Item,
set every element
android:clickable="true"
android:focusable="false"
works for me
Related
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 4 years ago.
I am new to Android programming. I intend to make a shopping List using a ListView such that the user can strike through items on the list that he has bought. it should be such that
List is made.
He can strike through items bought.
He can add items using the EditText and the Add button at the bottom of the layout.
List is rendered after adding items like on point 3 with the stroke through items maintained.
On starting I click for many of the initial items I added in my program to have a stroke through and then I start adding items, I get an unexpected behaviour in the List. Some of the newly added items get a stroke through after I click ADD right from the outset when I havent even clicked for them to have a stroke through. Some of them dont. I dont understand this behaviour. Is it because of some property of ListView?
ArrayList<String> shoppingList = new ArrayList<>();;
ArrayList<String> strikeThroughList = null;
ArrayList<String> strikeThroughText = null;
ArrayAdapter<String> adapter = null;
ListView lv = null;
Button addBtn=null;
EditText addEdtTxt=null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addBtn=findViewById(R.id.addBtn);
addBtn.setOnClickListener(this::btnClick);
//shoppingList = new ArrayList<>();
strikeThroughList = new ArrayList<>();
strikeThroughText= new ArrayList<>();
//One way to populate List
Collections.addAll(shoppingList, "Eggs", "Yogurt", "Milk", "Bananas", "Apples", "Tide with bleach", "Cascade");
adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, shoppingList);
lv = (ListView) findViewById(R.id.listView);
lv.setAdapter(adapter);
lv.setOnItemClickListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.mnu,menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id=item.getItemId();
if(id==R.id.clrList)
{
shoppingList.clear();
lv.setAdapter(adapter);
}
return super.onOptionsItemSelected(item);
}
public void btnClick(View view) {
if(view.getId()==R.id.addBtn){ addEdtTxt=findViewById(R.id.addEdtTxt);
adapter.add(addEdtTxt.getText().toString());
adapter.notifyDataSetChanged();
}
}
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
TextView text = (TextView) view;
text.setPaintFlags(text.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
}
Try to with below code change while adding Views to strikeThroughView list.
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
//String selectedFromList = (String)(lv.getItemAtPosition(position));
TextView text = (TextView) view;
text.setPaintFlags(text.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
text.setId(2);
strikeThroughView.add(text);
}
I have generated a list using array adapters and listview. Now i want a XML layout to open up each time i click on a list item. The layout format of all XMLs should be same,but the data in each XML should be different(for different list items).
How do i go about it?
Here is the code
public class handgunsActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_handguns);
ArrayList<String> hg=new ArrayList<String>();
hg.add("M1911");
hg.add("Desert Eagle .50");
hg.add("Glock 17");
hg.add("Sig P226");
hg.add("Browning High-Power");
ArrayAdapter<String> item=new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,hg);
final ListView list=(ListView)findViewById(R.id.root);
list.setAdapter(item);
Add item click listener to your list, then get the selected item and pass it where ever you want to
list.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
// Here is the data, pass it any where
String item = (String) list.getItemAtPosition(position);
}
});
I am not able get onItemCickListener to work. All I am doing is opening up a fragment using intents. Please do let me know if I am doing anything fishy here ?
public class MyListFragment extends ListFragment {
private ArrayList<String> myList = null;
private ArrayAdapter<String> myAdapter;
#Override
public void onActivityCreated(Bundle savedInstanceState){
Log.d("Example:", "In Fragement Calss");
super.onActivityCreated(savedInstanceState);
Resources myResources = getResources();
myList = new ArrayList<String>(Arrays.asList(myResources.getStringArray(R.array.myExamArray)));
myAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,myList);
setListAdapter(myAdapter);
ListView lv = (ListView) getActivity().findViewById(R.id.listView);
lv.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent myIntent = new Intent(view.getContext(), DetailsFragment.class);
startActivity(myIntent);
}
});
}
}
Note that the ListView blocks clicks of an item that contains at least one focusable
descendant but it doesn’t make the content focus-reachable calling
setItemsCanFocus(true). One workaround is by disabling focusability of descendants, using
android:descendantFocusability="blocksDescendants"
in the layout defining your list item. (First learned of this myself from http://cyrilmottier.com/2011/11/23/listview-tips-tricks-4-add-several-clickable-areas/, but a few other SO posts also point this out.) Does your layout contain buttons? If so, you'd fall into this case.
I don't see where you are inflating a specific XML for this Fragment... but it is possible you are referencing the wrong ListView.
Regardless you should use the existing onListItemClick method:
public class MyListFragment extends ListFragment {
private ArrayList<String> myList = null;
private ArrayAdapter<String> myAdapter;
#Override
public void onActivityCreated(Bundle savedInstanceState){
// Remove the OnItemClickListener, but keep everything else
}
#Override
public void onListItemClick (ListView l, View v, int position, long id) {
Intent myIntent = new Intent(this, DetailsFragment.class);
startActivity(myIntent);
}
}
I have two activities with ListView. This two activitises have individual layout-file. First have myListView01, second have myListView02. How to update listView's adapters?
I do so:
ListView list = (ListView)findViewById(R.id.myListView01);
myAdapter = new ArrayAdapter<String>(this, R.layout.item01, R.id.label, array01);
list.setAdapter(myAdapter);
I remove some items from the array01 and I need to update my listView without this elements.
But this solution to ListActivity does not work:
myAdapter.notifyDataSetChanged();
my code:
package ...;
import ...;
public class Class01 extends Activity
{
ArrayAdapter<String> mAdapter;
ArrayList<String> array01 = new ArrayList<String>();
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.layout01);
ListView list = (ListView)findViewById(R.id.my_list01);
mAdapter = new ArrayAdapter<String>(this, R.layout.item01, R.id.label, array01);
list.setAdapter(mAdapter);
}
public void onListItemClick (ListView parent, View v, int position, long id)
{
array01 = deleteElementByPosition(array01, position);
mAdapter.notifyDataSetChanged(); //does not work
}
public void deleteElementByPosition()
{
...
}
}
You have not set onItemClickListener for your ListView.
Extend your Class01 from ListActivity. Activity class doesn't have onListItemClick() method so it is never invoked. Or set your listener with setOnItemClickListener() for your ListView.
I have a CHOICE_MODE_SINGLE list with 3 rows. How do I place an onClick event that changes the value of an arbitrary variable, so that if Row 1 is clicked, say, the value of X = 1, and when Row 2 is clicked, the value of X = 2, etc.?
public class List extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice, GENRES));
final ListView listView = getListView();
listView.setItemsCanFocus(false);
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
}
private static final String[] GENRES = new String[] {"Barre", "Buffumville","Hodges"};
}
Its quite simple..rather than extending ListActivity extend it by Activity and just declare listview in your XML file as below:
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
initialize listview in your activity:
ListView listView=(ListView) findViewById(R.id.list);
And after setting adapter as you did,set the item click listener of the listview as below in your code:
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position,
long id) {
Toast.makeText(getBaseContext(), GENRES[position], Toast.LENGTH_SHORT).show();
}
});
By doing this you will get the desired value of that position of listview.
You will surely get the result you want.
You want to set the onItemClickListener on the list view.
listView.setOnItemClickListener(
new OnItemClickListener(
#Override
onItemClick(AdapterView<?> listView, View cell, int position, long id) {
// Code to change variables
}
});
You can implement protected void onListItemClick (ListView l, View v, int position, long id) in your ListActivity, and the position variable lets you know which item was clicked.