List View in android not visible - android

I'm facing problem with list view, I wrote following code for list view but when i run that code, list view not visible, the code is as follows:
l1=(ListView)findViewById(R.id.listView1);
String[] a1=new String[]{"abc", "bcd","cde","def"};
ArrayAdapter<String>adapter=new ArrayAdapter<String>(this, android.R.layout.simple_gallery_item, a1);
l1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), ((TextView)arg1).getText(), Toast.LENGTH_LONG).show();
}
});

Here you are missing to set Adapter to listview. Update your code to below code.
l1=(ListView)findViewById(R.id.listView1);
String[] a1=new String[]{"abc", "bcd","cde","def"};
ArrayAdapter<String>adapter=new ArrayAdapter<String>(this, android.R.layout.simple_gallery_item, a1);
l1.setAdapter(adapter);
l1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), ((TextView)arg1).getText(), Toast.LENGTH_LONG).show();
}
});

set listadapter first.
l1.setAdapter(ArrayAdapteradapter);

Is your layout properly written? Means ListView visibility.

Your listview doesn't get visible because you have not set the data in your listview using adapter.
set the adapter in your listview as below:
ArrayAdapter<String>adapter=new ArrayAdapter<String>(this, android.R.layout.simple_gallery_item, a1);
l1.setAdapter(adapter);

Related

In Listview onItemClick() listener is not working?

I have added an onItemClick() listener to a listview. The listener is not working.
Here is the code I am working with:`
MyBaseAdapter ma= new MyBaseAdapter(context, myList);
lvDetail.setAdapter(ma);
lvDetail.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
ListData ld= myList.get(arg2);
String des=ld.getDescription();
Toast.makeText(context,"description"+des, Toast.LENGTH_LONG).show();
}
});
Since you are using custom adapter, please write listener inside getView of adapter on item of list and perform operation.

Android clickable list item in a search interface

In my android app my search function works, the list gets correctly populated, but the items are not clickable. I'm trying to get that to work. Here's my code, can anyone see why the items are not clickable?
private void showResults(String query) {
Cursor cursor = DBHelper.searchDB(query);
startManagingCursor(cursor);
String[] searchFrom = new String[] { DBAdapter.KEY_TITLE,
DBAdapter.KEY_YEAR, DBAdapter.KEY_MAKE,
DBAdapter.KEY_MODEL };
int[] displayHere = new int[] { R.id.rTitleTV, R.id.rYearTV,
R.id.rMakeTV, R.id.rModelTV };
SimpleCursorAdapter records = new SimpleCursorAdapter(this,
R.layout.record_2, cursor, searchFrom, displayHere);
setListAdapter(records);
DBHelper.close();
// --- Click on list item ---
ListView clickList = getListView();
clickList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
}
});
// --- END click on list item ----
}
I just want to get the onClick to work, I don't care that it doesn't do anything at the moment. I'll put that functionality in later.
An example of record_2.xml:
<TextView
android:id="#+id/rMakeTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="Make"
android:textColor="#000000"
android:textSize="16dp"
android:textStyle="bold" >
</TextView>
I've tried this, based on what Sam is pointing out, but the list items are still not clickable.
// --- Click on list item ---
AdapterView<?> clickList = getListView();
clickList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
}
});
// --- END click on list item ----
It seems to me that since there's no reference to the records ListAdapter in the setOnItemClickListener it won't work?
You aren't doing anything in your onItemClick() method, how will you know if it is working (unless you are using the debugger)? Since your code looks fine, try something simple like this:
ListView clickList = getListView();
clickList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Log.v("Test", "A row was clicked with OnItemClick!");
}
});
Watch your logcat for these messages.
Try this. Remove AdapterView before OnItemClickListener in the method setOnItemClickListener(). Or replace AdapterView with simple View

Check selected item of ListView

I have a ListView and need to check, which item has been clicked.
The way I have it, it only shows my the selected item when I use the scrollwheel of my mouse (when testing it in the Eclipse emulator). When I test it on a real mobile device, it doesn't react at all.
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listItems);
listView.setAdapter(adapter);
listView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
myFunction(listView.getSelectedItem());
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Use setOnItemClickListener instead of setOnItemSelectedListener
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
//get value for listItems using position
String str =listItems[position];
myFunction(str);
}
});
I think better you need to use on onListItemClick listener
public void onListItemClick(ListView parent, View v, int position, long id)
{
parent.setItemChecked(position, parent.isItemChecked(position));
Toast.makeText(this, "You have selected" + items[position],30).show();
}

ItemClickListener not working

I have problem with onItemClickListener, its not working for my code
My code is
listView = (ListView)findViewById(R.id.listView1);
CustomAadpter adapter = new CustomAadpter(this,R.layout.file_view_layout,R.id.contact_name,names);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
System.out.println("view clicked is"+arg1);
}
});
Toast.makeText(context,"Position is"+ arg2,Toast.LENGTH_LONG).show();
Can be used if you want to show the postion as well. Hope this helps.
try
Toast.makeText(context,"message",Toast.LENGTH_LONG).show();

I am not able to select a particular row in listview

Hello all
I created a custom listview through my custom adapter..
CustomAdapter adapter = new CustomAdapter(MainPage.this, dbHelper, imgId, title);
adapter.setList(list1);
list1.setAdapter(adapter);
list1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
Log.i("hello",arg2+"item clicked ....");
Toast.makeText(MainPage.this, "Kindly enter search parameter!!", Toast.LENGTH_SHORT).show();
}
});
--
Now i am not able to select any item. I dont know why.....
try setting the inflated view "Clickable" and "able to listen to click events" in your adapter class getView() method.
convertView = mInflater.inflate(R.layout.your_layout, null);
convertView.setClickable(true);

Categories

Resources