Adding search feature to listview - android

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!

Related

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

hightlight multiple selected item(s)

i am populating my list view with cursor adapter, and i want to allow user to long press on any item(s) and perform available actions using action mode. i am using support library as my minimum sdk version =10
Problem: when i long click on the item action mode is displayed but the item is not highlighted as selected.
here is my activity_layout:
<LinearLayout 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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>
and the layout for list row :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingRight="5dp"
android:paddingLeft="5dp"
android:layout_marginTop="0dp"
android:layout_marginBottom="10dp"
android:orientation="horizontal"
android:background="#drawable/activated_background"
>
<TextView
android:id="#+id/item_id"
android:layout_width="40dp"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/item_title"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingRight="5dp"
/>
<TextView
android:id="#+id/item_amount"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:gravity="right"
/>
</LinearLayout>
i have used android:background="#drawable/activated_background" for row layout and it is defined in drawable folder as:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_activated="true" android:drawable="#drawable/list_activated_holo" />
<item android:drawable="#android:color/transparent" />
</selector>
when i long click on the item, it doesn't get highlighted.
here is my activity class:
public class MainActivity extends ActionBarActivity {
ActionMode _actionMode =null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ListView listView = (ListView)findViewById(R.id.listView1);
SQLiteDatabase db = new ItemDbContract(getBaseContext()).getReadableDatabase();
String selection[] = {ProductTable._ID,ProductTable.TITLE,ProductTable.Price};
Cursor cursor = db.query(ProductTable.TABLE_NAME, selection, null, null, null, null, null);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setAdapter(new ItemsListAdapter(getBaseContext(), cursor));
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
if(_actionMode!=null)
{
listView.setItemChecked(arg2, true);
System.out.println("item position checked="+arg2);
}
}
});
listView.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
System.out.println("long clicked on "+arg2);
listView.setItemChecked(arg2, true);
((ActionBarActivity)MainActivity.this).startSupportActionMode(actionModeCallback);
return true;
}
});
}
ActionMode.Callback actionModeCallback = new ActionMode.Callback() {
#Override
public boolean onPrepareActionMode(ActionMode arg0, Menu arg1) {
// TODO Auto-generated method stub
return false;
}
#Override
public void onDestroyActionMode(ActionMode arg0) {
// TODO Auto-generated method stub
}
#Override
public boolean onCreateActionMode(ActionMode arg0, Menu menu) {
// TODO Auto-generated method stub
_actionMode = arg0;
_actionMode.getMenuInflater().inflate(R.menu.item_journal_context_menu, menu);
return true;
}
#Override
public boolean onActionItemClicked(ActionMode arg0, MenuItem arg1) {
// TODO Auto-generated method stub
return false;
}
};
}
thanks in advance
finally solved my problem by maintaining a list of selected item in custom adapter and setting different background for the selected item.

Android - setOnItemClickListener event of ListView not working in Fragment

Good morning,
I have a problem with the setOnItemClickListener event of a ListView inside a Fragment which is never fired.
Here is the code of the item in listView:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="80dip" >
<ImageView
android:id="#+id/url_foto"
android:layout_width="100dip"
android:layout_height="100dip"
android:src="#drawable/stub"
android:scaleType="centerCrop"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_toRightOf="#id/url_foto"
android:orientation="vertical"
android:paddingLeft="10sp" >
<TextView
android:id="#+id/nome"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textIsSelectable="true" />
<TextView
android:id="#+id/cognome"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textIsSelectable="true" />
Here is the xml code of the list (that also includes a edittext with some listeners):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="#+id/cerca_il_prof_edit_search"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="search" />
<ListView
android:id="#+id/cerca_il_prof_list_result"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#00000000"
android:drawSelectorOnTop="true"
android:focusable="true" />
and finally the fragment that contains the event which is not fired:
public class CercaIlProfFragment extends Fragment {
private DbAdapter dbHelper;
private Cursor cursor;
private Context ctx;
List<Professore> listProf;
private static final String TAG = "CercaIlProf - ";
private EditText string_search;
private ListView listViewProf;
int textlength = 0;
/**
* The fragment argument representing the section number for this fragment.
*/
public static final String ARG_SECTION_NUMBER = "section_number";
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.cerca_il_prof, container, false);
ctx = getActivity();
listProf = new ArrayList<Professore>();
string_search = (EditText) rootView.findViewById(R.id.cerca_il_prof_edit_search);
Date currDate = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(currDate);
calendar.add(Calendar.MONTH, 1);
// get all prof
getAllProf();
listViewProf = (ListView) rootView.findViewById(R.id.cerca_il_prof_list_result);
ProfessoreListAdapterWithCache professoreListAdapterWithCache = new ProfessoreListAdapterWithCache(ctx,
R.layout.cerca_il_prof_list_result_item, listProf, this.getActivity());
listViewProf.setAdapter(professoreListAdapterWithCache);
listViewProf.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getActivity(), "Click ListItem Number " + position, Toast.LENGTH_LONG).show();
}
});
listViewProf.setEnabled(true);
string_search.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
Toast.makeText(getActivity(), "Click ListItem Number ", Toast.LENGTH_LONG).show();
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//do stuff
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
//do stuff
});
return rootView;
}
Can someone help me please??
Thanks in advance.
Regards.
Giuseppe
To expand upon #Ashwin's answer, 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, although you can just disable children that can take focus as he mentioned earlier. (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.)
Just put android:focusable="false" android:clickable="false" in layout. For all textviews,buttons etc.
Works Fine.
Declare your listview and itemclick listener inside onViewCreated Like,
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
final ListView listViewProf=(ListView)getActivity().findViewById(R.id.cerca_il_prof_list_result);
listViewProf.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
}
you will try this it will help you
lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
#Override
public void onItemClick(android.widget.AdapterView<?> arg0,
View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
}
});
I found the solution. If you change the list item's (layout which you are setting in the listview adapter)layout child views to
android:clickable="false"
and
android:focusable="false"
it works.

OnClick ListView

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.

Categories

Resources