Listview item click issue - android

I have added leadbolt ad(entry ad). The advertising works correctly but listview.click doesn't work when I close ad from close sign. (Listview.click does not do anything, It works when I remove AdController)
public class SoundList extends ListActivity {
int [] soundfile;
MediaPlayer mediaPlayer;
private AdController myController;
final Activity act = this;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myController = new AdController(act, "111111111");
myController.loadAd();
soundfile= new int[] {R.raw.sound1,R.raw.sound2.....};
String[] sounds= getResources().getStringArray(R.array.sounds);
// Binding Array to ListAdapter
this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, R.id.label, sounds));
ListView lv = getListView();
lv.setDescendantFocusability(ListView.FOCUS_BLOCK_DESCENDANTS);
// listening to single list item on click
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = new Intent(getApplicationContext(), SingleListItem.class);
intent.putExtra("position", position);
startActivity(intent);
}
});
}

Having a focusable item in a row of a ListView causes the OnItemClickListener NOT to be invoked.
To fix this issue add following code to row view.
XML:
android:descendantFocusability="blocksDescendants"
Java:
listItem.setDescendantFocusability(FOCUS_BLOCK_DESCENDANTS);

Related

How to show different activities for different ListView items?

I have a ListView with several items and every item has it own Activity class to show details. How can I switch to an appropriate activity when user selects and item in this ListView?
My current code is following:
ListView listView;
ArrayAdapter<String> adapter;
String[] hotel = {"one room", "double room", "suit", "vip"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listView);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, hotel);
listView.setAdapter(adapter);
}
Add setOnItemClickListener to your listView in onCreate under listView.setAdapter(adapter); :
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
if(position == 0){
Intent intent = new Intent(MainActivity.this, OneRoomActivity.class);
startActivity(intent);
}
else if(position == 1){
Intent intent = new Intent(MainActivity.this, DoubleRoomActivity.class);
startActivity(intent);
}
//Do as above for rest of the list items
}
});
Hope this helps.

How to implement onItemClickListener on each item in the ListView to go to another activity?

How to implement onItemClickListener on each item in the ListView to go to another activity / to a new class?
public class MainActivity extends Activity{
ListView list;
String[] itemname ={
"Resturants",
"Coffee Shops",
"Hotels",
"Gas Stations",
"Hospitals",
"Airports",
"ATM",
"Cinemmas",
"Phamacies"
};
Integer[] imgid={
R.drawable.restaurantz,
R.drawable.coffeeshop,
R.drawable.hotel,
R.drawable.gaspump,
R.drawable.hospitalblue,
R.drawable.airporticon,
R.drawable.atm,
R.drawable.cinemma,
R.drawable.hospitalblue,
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
CustomListAdapter adapter = new CustomListAdapter(this, itemname, imgid);
list = (ListView) findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
}
}
inside onItemClick:
startActivity(new Intent(MainActivity.this, NewActivity.class));
if you need to differenciate the clicked item, you can use the position parameter, for example:
if (position == 0)
// do something
else
// do something else

update listView from ActionBarActivity

My MainActivity, which extends ActionBarActivity, has a ListView, which I want to reload using notifyDataSetChanged(), but I do not know how to do this.
My code so far:
public class MainActivity extends ActionBarActivity {
private JournalEntryDataSource datasource;
private ListView listView = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
datasource = new JournalEntryDataSource(this);
datasource.open();
listView = (ListView) findViewById(R.id.list);
List<JournalEntry> values = datasource.getAllJournalEntries();
listView.setAdapter(new ArrayAdapter<JournalEntry>(this, R.layout.list_item, R.id.lugares, values));
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
Log.i("app", "Deleting item number " + position);
datasource.deleteJournalEntry((JournalEntry) listView.getAdapter().getItem(position));
//I would like to do this here:
//ArrayAdapter<JournalEntry> adapter = (ArrayAdapter<JournalEntry>) getListAdapter();
//adapter.notifyDataSetChanged();
}
});
}
But I get an error when calling adapter.notifyDatasetChanged()...
Any tips? I've been looking at similar questions and no luck so far...
--------EDITED CODE---------
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
datasource = new JournalEntryDataSource(this);
datasource.open();
listView = (ListView) findViewById(R.id.list);
values = datasource.getAllJournalEntries();
listView.setAdapter(new ArrayAdapter<JournalEntry>(this, R.layout.list_item, R.id.lugares, values));
ArrayAdapter<JournalEntry> adapter = (ArrayAdapter<JournalEntry>) getListAdapter();
// use the SimpleCursorAdapter to show the
// elements in a ListView
listView.setAdapter(new ArrayAdapter<JournalEntry>(this, R.layout.list_item, R.id.lugares, values));
listView.setLongClickable(true);
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id) {
Log.i("hola","borrando la posición "+position);
JournalEntry removeMe = (JournalEntry)listView.getAdapter().getItem(position);
values.remove(removeMe);
datasource.deleteJournalEntry(removeMe);
//adapter.notifyDataSetChanged();
reloadMe();
return true;
}
});
}
public void reloadMe(){
//need to do this better... but meh.
Intent intent = new Intent(this, MainActivity.class);
this.startActivity(intent);
}
protected ListView getListView() {
if (listView == null) {
listView = (ListView) findViewById(android.R.id.list);
}
return listView;
}
protected void setListAdapter(ListAdapter adapter) {
getListView().setAdapter(adapter);
}
protected ListAdapter getListAdapter() {
ListAdapter adapter = getListView().getAdapter();
if (adapter instanceof HeaderViewListAdapter) {
return ((HeaderViewListAdapter)adapter).getWrappedAdapter();
} else {
return adapter;
}
}
}
Updated Answer for updated code
instead of creating the same activity again, you should choose to set another new adapter object with the new values object.
public void reloadMe(){
//need to do this better... but meh.
//Intent intent = new Intent(this, MainActivity.class);
//this.startActivity(intent);
listView.setAdapter(new ArrayAdapter<JournalEntry>(this, R.layout.list_item, R.id.lugares, values));
}
Perhaps, you are deleting from
datasource.deleteJournalEntry((JournalEntry) listView.getAdapter().getItem(position));
But you did not set "datasource" as the data source of the adapter. If you look carefully you did set the "values" list object-
listView.setAdapter(new ArrayAdapter<JournalEntry>(this, R.layout.list_item, R.id.lugares, values));
Perhaps you may need to also remove from the values list also.

setOnItemClickListener not working

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);
}
}

Trigger ListView's OnClickListener in a TabActivity?

I've searched around for a while now to try to find a solution to this problem, but none of them have worked for me. I have a tab setup which displays tab activities. I'm trying to use a OnClickListener in ListView to catch click events in a list, but it doesn't work. Thanks in advance for helping!
public class Ettan extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.ettan);
ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.nyhetslista,
new String[] { "rubrik", "kategori" },
new int[] { R.id.item_title, R.id.item_subtitle });
setListAdapter(adapter);
final ListView lv = getListView();
lv.setTextFilterEnabled(false);
lv.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
Toast.makeText(getApplicationContext(), "yay", Toast.LENGTH_LONG).show();
}
});
}
}

Categories

Resources