onListItemClick callback isn't activated - android

public class List_Items extends ListActivity{
private ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_item);
List <ImageAndText> total_list=new ArrayList<ImageAndText>();
ListView lv = (ListView) this.findViewById(android.R.id.list);
lv.setAdapter((ListAdapter) new ImageAndTextListAdapter(this, total_list));
getListView().setTextFilterEnabled(true);
//////////////////////////////////////////////////////
Button btn=(Button) findViewById(R.id.button_sync);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast message=Toast.makeText(getApplicationContext(), "click the list_item", Toast.LENGTH_LONG);
message.show();}
});
}
public void onListItemClick(ListView parent, View v, int position, long id)
{
Toast.makeText(getApplicationContext(), "You have selected " +(position+1)+"th item",
Toast.LENGTH_SHORT).show();}
}
I have this list_item which is extends the listactivity. However, when I click on one of the row, the callback onListItemClick didn't get activated. Why is that? I don't need to anything with the adapter on this right?
}

You have to use the following in all the widgets of xml which you are inflating for your custom ListView.
android:focusable="false"
android:focusableInTouchMode="false"
Hope it helps

u havent register ur onItemClick with Listview.
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
Log.i("List Clicked ....", "List Clicked...");
}
});
or
listView.setOnItemClickListener(listener);
public OnItemClickListener listener = new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Log.i("List Clicked ....", "List Clicked...");
}
};

Can you try this
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Log.i("You clicked the list");
}

Related

listView filter search

I was looking to filter a listView by an editText.
My ListView is populate by an ArrayString, and when I filter it everything goes right. But I have a problem to recognize the element clicked. When I filter the listView and I click on the item filtered I want to have the number of the row in the original arrayString in order to make an Intent. Whit my code when I click the item in the filtered arrayString the row is 0 and it's not correct. If I search "Anche Se Pesa" I want to return the row 2 like the position in the original ArrayString.
<string-array name="titoli">
<item>An Val Dondona</item>
<item>Anche Mio Padre</item>
<item>Anche Se Pesa</item>
<item>Andremo In Francia</item>
<item>Aprite Le Porte</item>
<item>Arda Berghem</item>
<item>Ascoltate Amici Cari</item>
<item>Ave, O Vergine</item>
<item>Aver na Figlia Sola Soletta</item>
<item>Ancora sti quatro</item>
<item>Andouma Prou</item>
</string-array>
Here Is the code
public class activity_titoli extends AppCompatActivity {
Button btt_backHome;
ListView lV_titoli;
EditText eT_search;
private ArrayAdapter<CharSequence> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_titoli);
btt_backHome = (Button) findViewById(R.id.btt_backHome);
lV_titoli = (ListView) findViewById(R.id.lV_titoli);
eT_search = (EditText) findViewById(R.id.eT_search);
adapter = ArrayAdapter.createFromResource(this, R.array.titoli, android.R.layout.simple_list_item_1);
lV_titoli.setAdapter(adapter);
final Intent to_Home = new Intent (this , Activity_Main.class);
eT_search.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
// TODO Auto-generated method stub
}
#Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
(activity_titoli.this).adapter.getFilter().filter(arg0);
}
});
btt_backHome.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(to_Home);
}
});
lV_titoli.setOnItemClickListener(new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Log.e("Element: " , Integer.toString(i));
}
});
}
}
Try this code :
lV_titoli.setOnItemClickListener(new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Resources res = getResources();
List<CharSequence> list = Arrays.asList(res.getStringArray(R.array.titoli));
Log.e("Element: " , Integer.toString(list.indexOf(adapter.filgetItem(i))));
}
});
Hope this helps

OnItemSelectedListener in a Fragment that uses a custom Adapter not responding

I have a list in a fragment with custom cells adapter,
The problem is that the onItemSelected is not responding,
how to fix this please?
public class PhoneMenuList extends SherlockFragment implements OnItemSelectedListener {
Fragment newContent = null;
ListView productList;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View mView = inflater.inflate(R.layout.list, container, false);
return mView;
}
#Override
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
//SET THE LIST ADAPTER!
Hamburger hambu_data[] = new Hamburger[] {
new Hamburger(R.drawable.icon_hambu_folder, "My Documents"),
new Hamburger(R.drawable.icon_hambu_favs, "Top 10 viewed"),
new Hamburger(R.drawable.icon_hambu_validate, "Validate Document"),
new Hamburger(R.drawable.icon_hambu_how, "How to use"),
new Hamburger(R.drawable.icon_hambu_about, "About")
};
productList= (ListView) getActivity().findViewById(R.id.listView1);
HamburgerAdapter adapter = new HamburgerAdapter(getActivity(), R.layout.hamburger_item_row, hambu_data);
productList= (ListView) getActivity().findViewById(R.id.listView1);
View header = (View)getLayoutInflater(savedInstanceState).inflate(R.layout.hamburger_item_row, null);
productList.addHeaderView(header);
productList.setAdapter(adapter);
//listener
productList.setOnItemSelectedListener(this);
}
//#Override
public void onListItemClick(ListView l, View v, int position, long id) {
Log.d("mensa", "chapuzea");
}
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Log.d("mensa", "abacus");
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
Log.d("mensa", "semper");
}
}
why are you initializing listview twice ?
productList= (ListView) getActivity().findViewById(R.id.listView1);
and use setOnItemClick() instead of setOnItemSelected()
remove productList.setOnItemSelectedListener(this);
use productList.setOnItemClickListener(this);
You have to set a choice mode to your ListView. setChoiceMode
You can override onListItemClick method of SherlockListFragment.
Try this Code.
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
System.out.println("onListItemClick");
}
or instead of using OnItemSelectedListener use OnItemClickedListener.

How to implement onClick in a Listview

I guess this is simple.
I want to toast the text in the rows when I click on it. And here is my code:
public class AndroidSQLite extends Activity { private SQLiteAdapter
mySQLiteAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView listContent = (ListView)findViewById(R.id.contentlist);
mySQLiteAdapter = new SQLiteAdapter(this);
mySQLiteAdapter = new SQLiteAdapter(this);
mySQLiteAdapter.openToRead();
Cursor cursor = mySQLiteAdapter.queueAll();
startManagingCursor(cursor);
String[] from = new String[]{SQLiteAdapter.KEY_NOME};
int[] to = new int[]{R.id.text};
SimpleCursorAdapter cursorAdapter =
new SimpleCursorAdapter(this, R.layout.row, cursor, from, to);
listContent.setAdapter(cursorAdapter);
mySQLiteAdapter.close();
}
}
Thank You!
just write down
listContent.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter, View arg1,
int position, long id) {
Log.i("get ItemIDPosition",
"" + adapter.getItemIdAtPosition(position));
Log.i("get ItemATPosition", "" + adapter.getItemAtPosition(position));
}
});
You can use
listContent.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
}
});
Like this:
listContent.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String item = ...;
Toast.makeText(getBaseContext(), item, Toast.LENGTH_LONG).show();
}
});
Try this code for Item click
listContent.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String text = <Get Your from database is here in this string>;
Toast.makeText(getBaseContext(), text, Toast.LENGTH_LONG).show();
}
});
Use onitemclick evetn and not onclick.
listview.setOnItemClickListener(...);
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
arg2 is the index in the list.

How to get the string value of selected value item in the ListView

I have a Listview in my project for which i want to find out the string value of the selected item in the listview.
Here is a simple Examlple.
#Override
protected void onListItemClick(ListView l, View v, int position, long thisID)
{
super.onListItemClick(l, v, position, thisID);
// Get the item that was clicked
Object o = this.getListAdapter().getItem(position);
String keyword = o.toString();
}
From your Activity
public class ExampleActivity extends Activity {
String str_arr[] = {"A", "B", "C"};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView listView = (ListView) findViewById(R.id.listView1);
listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, str_arr));
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter, View view, int position,
long id) {
// TODO Auto-generated method stub
Object o = adapter.getItemAtPosition(position);
String str_text = o.toString();
Intent intent = new Intent(ExampleActivity.this, NewExample.class);
intent.putExtra("StrValue", str_text);
startActivity(intent);
}
});
}
}

Adding an onclicklistener to listview (android)

I've managed to implement a great listview that I found here http://www.learn-android.com/2011/11/22/lots-of-lists-custom-adapter/comment-page-1/
but I can't seem to add an onclicklistener
I just want to be able to do an action when I click on the row, with the data that the row contains of course
any ideas?
thanks
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.liste);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
// Setup the list view
final ListView prestListView = (ListView) findViewById(R.id.list);
final prestationAdapterEco prestationAdapterEco = new prestationAdapterEco(this, R.layout.prestation);
prestListView.setAdapter(prestationAdapterEco);
// Populate the list, through the adapter
for(final prestationEco entry : getPrestations()) {
prestationAdapterEco.add(entry);
}
prestListView.setClickable(true);
prestListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
Object o = prestListView.getItemAtPosition(position);
String str=(String)o;//As you are using Default String Adapter
Toast.makeText(getApplicationContext(),str,Toast.LENGTH_SHORT).show();
}
});
}
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Object o = prestListView.getItemAtPosition(position);
prestationEco str = (prestationEco)o; //As you are using Default String Adapter
Toast.makeText(getBaseContext(),str.getTitle(),Toast.LENGTH_SHORT).show();
}
});
If your Activity extends ListActivity, you can simply override the OnListItemClick() method like so:
/** {#inheritDoc} */
#Override
protected void onListItemClick(ListView l, View v, int pos, long id) {
super.onListItemClick(l, v, pos, id);
// TODO : Logic
}
The prestListView.getItemAtPosition(position); returns the UI widget: Text, Icon, ...
Try this instead:
Object o = prestationAdapterEco.getItemAtPosition(position);
or
Object o = arg0.getItemAtPosition(position);
Get the object from the adapter. Not from the list-view.
2.
Object o is a prestationEco object. Not a String.
list.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
You are doing
Object o = prestListView.getItemAtPosition(position);
String str=(String)o;//As you are using Default String Adapter
The o that you get back is not a String, but a prestationEco so you get a CCE when doing the (String)o
Try this:
list.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3)
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});

Categories

Resources