OnClick ListView - android

I have followed this tutorial
http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/
and like to add an on click for the listview.
now here is my main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:divider="#drawable/list_divider"
android:dividerHeight="1px"
android:cacheColorHint="#00000000"/>
</LinearLayout>
and here is my code:
setContentView(R.layout.main);
steden = new ArrayList<voorDeLijst>();
this.m_adapter = new StedenAdapter(this, R.layout.list_item, steden);
setListAdapter(this.m_adapter);
ListView lv = (ListView)findViewById(R.id.List);
lv.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
AlertDialog.Builder adb = new AlertDialog.Builder(HelloAndroid.this);
adb.setTitle("LVSelectedItemExample");
adb.setMessage("Selected Item is = ");
adb.setPositiveButton("Ok", null);
adb.show();
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
The thing is that I am a beginner and with the code above I get an error because It cannot locate the listview. So I can't attach a OnItemClick Listener to it.
but when I change <ListView android:id="#+id/android:list" to <ListView android:id="#+id/List"
Then I can find the listview. but it gives an exception at the line: setContentView(R.layout.main);
So how do I attach an onClick/onItemClick to a Listview which has a custom adapter to bind objects to the listitems?

Found it, because my class extended the ListActivity I could do this:
#Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
// Get the item that was clicked
Object o = this.getListAdapter().getItem(position);
String keyword = o.toString();
...
}
I found it at http://www.anddev.org/viewtopic.php?t=22

Have you tried findViewById(android.R.id.list)? Really though, if your activity is basically one big list view, then you should be using a ListActivity for your activity, and that has an accessor that gives you direct access to the ListView.

Related

setOnItemClickListener for activity extending ListActivity without adapter

I want to implement a setOnItemClickListener for the below mentioned listview.
The code works fine for me in displaying items of string-array to listview but I want to perform some action when the user clicks and on longclick.
public class MainActivity extends ListActivity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] plainStrings = getResources().getStringArray(R.array.myarray);
Spanned[] htmlStrings = new Spanned[plainStrings.length];
for(int i = 0 ; i < plainStrings.length; i++) {
htmlStrings[i] = Html.fromHtml(plainStrings[i]);
}
setListAdapter(new ArrayAdapter<CharSequence>(this,R.layout.items, htmlStrings));
}
main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:isScrollContainer="true"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:scrollbarStyle="insideOverlay">
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:dividerHeight="3dp"/>
</LinearLayout>
items.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
ListActivity has its own protected method onListItemClick, you can override it to get the row click's event
Normal click:
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
//Do whatever you want
Toast.makeText(getApplicationContext(),"Clicked",Toast.LENGTH_SHORT).show();
}
});
Long click:
listView.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
final int arg2, long arg3) {
//Do whatever you want
Toast.makeText(getApplicationContext(),"Long Clicked",Toast.LENGTH_SHORT).show();
});
}

how to show list view record into second listview on setOnItemClickListener

I have created list view, what I want to do is that when user clicks on first list view the selected record should show in second list, in my code it show on text view, but I want to show that record in second list view so please give me the code/idea how to do this as I am new in android..
public class MainActivity extends Activity {
String item[]=new String[]{"rk","kk","kk","ll","mm","uu"};
TextView tv,tv2,tv3,tv4,tv5;
List<TextView> arrayTV = new ArrayList<TextView>();
ListView li,li2;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
li=(ListView)findViewById(R.id.listView1);
li2=(ListView)findViewById(R.id.listView2);
tv=(TextView)findViewById(R.id.textView1);
tv2=(TextView)findViewById(R.id.textView2);
tv3=(TextView)findViewById(R.id.textView3);
tv4=(TextView)findViewById(R.id.textView4);
tv5=(TextView)findViewById(R.id.textView5);
arrayTV.add(tv);
arrayTV.add(tv2);
arrayTV.add(tv3);
arrayTV.add(tv4);
arrayTV.add(tv5);
ArrayAdapter<String> adapter=new ArrayAdapter<String> (this,android.R.layout.simple_list_item_1,android.R.id.text1, item);
li.setAdapter(adapter);
li.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3) {
// TODO Auto-generated method stub
int itm=position;
Toast.makeText(getApplicationContext(),""+itm+""+li.getItemAtPosition(position),40).show();
arrayTV.get(position).setText(""+li.getItemAtPosition(position));
}
});
}
Here is my XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/rt"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1"
tools:context=".MainActivity" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_weight=".5" >
</ListView>
set the adapter for second listview in onitemclick of first listview
List<String> a = new ArrayList<String>();
li.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3) {
String clickedItem =item[postion];
if(!a.contains(clickedItem))
a.add(clickedItem);
String[] newitem = new String[a.size()];
a.toArray(newitem);
ArrayAdapter<String> adapter=new ArrayAdapter<String> (MainActivity.this,android.R.layout.simple_list_item_1,android.R.id.text1, newitem);
li2.setAdapter(adapter);
}
});

Listview Android onClick defaul event

How could I use onClick event from the base ListView in android without using the Adapter. I what to now witch item is selected and in base of that i want to open another layout.
listview.item="Something"
{
setContentView(R.id.layout.Something);
}
Please help me. I tried with the simple onClick but it doesn't work.
You Need To create layout XML file
main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
android:id="#+id/listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="#+id/view1" >
</ListView>
</RelativeLayout>
In Activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView mListView = (ListView)findViewById(R.id.listview);
mListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
}
});
}

Adding search feature to listview

I have created a listview of the users installed applications in a sliding drawer and I want to add a search function to it. I am following the tutorial here.
So far, this is my coding (that includes the so far implemented search function):
Drag_and_Drop_App.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<SlidingDrawer
android:id="#+id/bottom"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:orientation="horizontal"
android:layout_marginTop="200dp"
android:content="#+id/content"
android:handle="#+id/handle" >
<Button
android:id="#+id/handle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Handle" />
<LinearLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#00FF00">
<!-- Editext for Search -->
<EditText android:id="#+id/inputSearch"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Search applications.."
android:inputType="textVisiblePassword"/>
<ListView
android:id="#+id/lvApps"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
</SlidingDrawer>
</RelativeLayout>
Drag_and_Drop_App.java:
package com.example.awesomefilebuilderwidget;
IMPORTS
public class Drag_and_Drop_App extends Activity {
private ListView mListAppInfo;
// Search EditText
EditText inputSearch;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// set layout for the main screen
setContentView(R.layout.drag_and_drop_app);
// search bar
inputSearch = (EditText) findViewById(R.id.inputSearch);
// load list application
mListAppInfo = (ListView)findViewById(R.id.lvApps);
// create new adapter
AppInfoAdapter adapter = new AppInfoAdapter(this, Utilities.getInstalledApplication(this), getPackageManager());
// set adapter to list view
mListAppInfo.setAdapter(adapter);
// implement event when an item on list view is selected
mListAppInfo.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView parent, View view, int pos, long id) {
// get the list adapter
AppInfoAdapter appInfoAdapter = (AppInfoAdapter)parent.getAdapter();
// get selected item on the list
ApplicationInfo appInfo = (ApplicationInfo)appInfoAdapter.getItem(pos);
// launch the selected application
Utilities.launchApp(parent.getContext(), getPackageManager(), appInfo.packageName);
}
});
}
}
I'm not sure where to put this coding:
inputSearch.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
MainActivity.this.adapter.getFilter().filter(cs);
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
If you need to see any other of my coding (such as any adapters for the app listview) let me know!

how Listview onclick call another activity?

mListUsers = getUsers();
lvUsers = (ListView) findViewById(R.id.lv_user);
lvUsers.setAdapter(new ListAdapter(this, R.id.lv_user, mListUsers));
lvUsers.setClickable(true);
lvUsers.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3)
{
// TODO Auto-generated method stub
Object o = lvUsers.getItemAtPosition(position);
UserBO obj = (UserBO) o;
Toast.makeText(Select.this, "Record Selected= "+obj.getId()+" "+obj.getName()+" "+obj.getEmail()+" "+obj.getContact(), Toast.LENGTH_LONG).show();
Intent intent = new Intent(Select.this,Update.class);
startActivity(intent);
}
});
list_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:padding="6px"
android:orientation="vertical"
android:layout_height="67px" android:id="#+id/rlt_main"
android:background="#E0FFFF" android:clickable="true">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:orientation="horizontal"
android:layout_height="16px" android:id="#+id/rlt_main"
android:background="#E0FFFF">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tv_pid"
android:text="12"
android:layout_centerVertical="true"
android:textColor="#E0FFFF" >
</TextView>
</LinearLayout>
select.xml
<ListView
android:layout_height="wrap_content"
android:id="#+id/lv_user"
android:layout_width="fill_parent"
android:clickable="true"
>
Hi,above code is of onclick listview items selection.but when i click on item from listview nnothing is happening instead i want to call update activity.what is the mistake in my code??if anyone want to see code of XML layout then let me know..
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,int position, long id) {
Toast.makeText(Context,"Selected item is "+ position, Toast.LENGTH_SHORT).show();
Intent i = new Intent(ListActivity.this, SecondActivity.class);
ListActivity.this.startActivity(i);
}
}
Hi I think you should try like this,
Intent intent = new Intent();
intent.setClassName(Select.this, <packagename>.Update.class.getName());
startActivityForResult(intent,0);

Categories

Resources