Alphabet Index in ListView with SimpleCursorAdapter very slow - android

I am trying to implement the Alphabet-Index in ListView with SimpleCursorAdapter.
It is working but it is very slow, when i am trying to scroll the list up and down the phone is getting strucked. I think its is happening because of my way of coding due to lack of knowledge. so that I need to know what is the mistake or what is the wrong of this code. And also if there is any other way than this way, please let me know.
This is set of code of fragment which has the above problem.
public class EnFragment extends Fragment {
private TestAdapter dbHelper;
private myCursorAdapter dataAdapter;
public EnFragment() {
// Required empty public constructor
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dbHelper = new TestAdapter(getContext());
dbHelper.createDatabase();
dbHelper.open();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_en, container, false);
setListView(view);
return view;
}
private void setListView(View view) {
Cursor cursor = dbHelper.getAllWord("enDic");
String[] columns = new String[]{
"word",
"definition",
"_id",
"favourite"
};
int[] to = new int[]{
R.id.txt_word,
R.id.txt_def,
R.id.txt_id
};
dataAdapter = new myCursorAdapter(
getContext(), en_word_row,
cursor,
columns,
to,
0);
Button btnSubmitEng = (Button) view.findViewById(R.id.btnSubmitEng);
final ListView listView = (ListView) view.findViewById(R.id.listView);
listView.setAdapter(dataAdapter);
listView.setEmptyView(btnSubmitEng);
listView.setFastScrollEnabled(true);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Cursor cursor = (Cursor) listView.getItemAtPosition(position);
final String word = cursor.getString(cursor.getColumnIndexOrThrow("word"));
Toast.makeText(getContext(), word, Toast.LENGTH_SHORT).show();
}
});
}
public void textSearch(String searchTxt){
String newStr = searchTxt.replace(" ","");
dataAdapter.getFilter().filter(newStr);
dataAdapter.setFilterQueryProvider(new FilterQueryProvider() {
public Cursor runQuery(CharSequence constraint) {
return dbHelper.getWordByName("enDic",constraint.toString());
}
});
}
private class myCursorAdapter extends SimpleCursorAdapter implements SectionIndexer {
private final AlphabetIndexer mAlphabetIndexer;
public myCursorAdapter(Context context, int layout,
Cursor c, String[] from, int[] to, int flags) {
super(context, layout, c, from, to, flags);
mAlphabetIndexer = new AlphabetIndexer(c, 1, "අBCDEFGHIJKLMNOPQRSTUVWXYZ");
}
public View newView(Context _context, Cursor _cursor, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) _context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
return inflater.inflate(en_word_row, parent, false);
}
#Override
public void bindView(View view, Context context, final Cursor cursor) {
super.bindView(view, context, cursor);
final ImageButton imgFav = (ImageButton) view.findViewById(R.id.img_fav);
final String _id = cursor.getString(cursor.getColumnIndex("_id"));
final Integer fav = Integer
.valueOf(cursor.getString(cursor.getColumnIndex("favourite")));
if (fav == 1)
imgFav.setImageResource(R.drawable.fav_on);
else
imgFav.setImageResource(R.drawable.fav_off);
imgFav.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (dbHelper.isFavourite("enDic", _id)){
imgFav.setImageResource(R.drawable.fav_off);
dbHelper.favUpdate("enDic", _id,0);
}
else{
imgFav.setImageResource(R.drawable.fav_on);
dbHelper.favUpdate("enDic", _id,1);
}
}
});
}
#Override
public Object[] getSections() {
return mAlphabetIndexer.getSections();
}
#Override
public int getPositionForSection(int sectionIndex) {
return mAlphabetIndexer.getPositionForSection(sectionIndex);
}
#Override
public int getSectionForPosition(int position) {
return mAlphabetIndexer.getSectionForPosition(position);
}
}
}
fragment_en.xml (layout of the above fragment)
<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"
tools:context="com.sldroid.mecdic_v21.fragment.EnFragment"
android:orientation="vertical">
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#android:color/transparent"
android:listSelector="#android:color/transparent"
android:scrollbarStyle="outsideOverlay"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Submit new word"
android:layout_marginTop="55dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:id="#+id/btnSubmitEng"/>
</LinearLayout>
en_word_row.xml
<LinearLayout
android:id="#+id/cardView"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:descendantFocusability="blocksDescendants">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:baselineAligned="false"
android:paddingLeft="20dp"
android:paddingRight="20dp">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="3"
android:paddingTop="10dp"
android:paddingBottom="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="25dp"
android:orientation="horizontal">
<TextView
android:id="#+id/txt_word"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Word"
android:textSize="18dp"
android:textColor="#424242"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/txt_def"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="definition"
android:textSize="10dp"
android:paddingTop="5dp"
android:paddingLeft="10dp"
android:textColor="#FF757575"
android:lines="1"/>
</LinearLayout>
<TextView
android:id="#+id/txt_id"
android:layout_width="0dp"
android:layout_height="0dp"
android:visibility="invisible"/>
</LinearLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="0.5">
<ImageButton
android:id="#+id/img_fav"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/fav_off"
android:scaleType="centerInside"
android:layout_gravity="center"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
style="#style/Base.Widget.AppCompat.Button.Borderless"/>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
method of TestAdapter
public Cursor getAllWord(String table)
{
Cursor mCursor = mDb.query(table, new String[] {"_id","word","definition","favourite"},
null, null, null,null, " word COLLATE NOCASE");
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
public Cursor getWordByName(String table, String inputText) throws SQLException {
Cursor mCursor = null;
if (inputText == null || inputText.length () == 0) {
mCursor = mDb.query(table, new String[] {"_id","word","definition","favourite"},
null, null, null,null, " word COLLATE NOCASE");
}
else {
mCursor = mDb.query(true, table, new String[] {"_id","word","definition","favourite"},
"word" + " like '" + inputText + "%'", null,
null, null, " word COLLATE NOCASE",null);
}
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}

GetAllWord and getWordByName should be called in the doInBackground method and in the postExecute you shoul update your adapter.
private class GetAllWord extends AsyncTask<Object, Object, Cursor> {
#Override
protected Cursor doInBackground(Object... params) {
return GetAllWord("your text");
}
#Override
protected void onPostExecute(Cursor result) {
if (result != null) {
//update the adapter with the result
}
}
}

Related

CursorAdapter not displaying elements in ListView

I am trying to get a column of SQLITE database to a listview using a SimpleCursorColumn. I am getting the accurate number of rows in the list view, but the text is not appearing in the list. I printed out the content of the cursor and it contains the correct elements.
Why is the text not showing up in the list? Surely it must be easy that I am being stupid about.
public class Tab3Fragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {
private static final int LOADER_ID = 42;
private CursorAdapter _adapter;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tab3_frag, container, false);
ListView lv = view.findViewById(R.id.student_view);
_adapter = new SimpleCursorAdapter(getContext(),
R.layout.container_list_item_view, null,
new String[] { Contract.Student_Table.STUDENTS_COLUMN_NAME},
new int[] { R.id.list_item });
lv.setAdapter(_adapter);
getLoaderManager().initLoader(LOADER_ID, null, this);
return view;
}
#Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
if (id != LOADER_ID) {
return null;
}
return new CursorLoader(getContext(),
Contract.Student_Table.CONTENT_URI,
new String[] { Contract.Student_Table.STUDENTS_COLUMN_ID, Contract.Student_Table.STUDENTS_COLUMN_NAME}, null, null,
null);
}
#Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
_adapter.swapCursor(data);
}
#Override
public void onLoaderReset(Loader<Cursor> loader) {
_adapter.swapCursor(null);
}
}
tab3_frag.xml
<?xml version="1.0" encoding="utf-8"?>
<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:background="#c2f6ff"
android:orientation="vertical">
<ListView
android:id="#+id/simple_list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:id="#+id/student_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:textColor="#android:color/black" />
</LinearLayout>
Better use loader. Try below example:
public class LanguageListActivity extends ListActivity implements
LoaderCallbacks<Cursor> {
private static final int LOADER_ID = 42;
private CursorAdapter _adapter;
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.container_list);
_adapter = new SimpleCursorAdapter(this,
R.layout.container_list_item_view, null,
new String[] { DatabaseConstants.COL_LANG_NAME },
new int[] { R.id.list_item });
setListAdapter(_adapter);
getLoaderManager().initLoader(LOADER_ID, null, this);
}
#Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
if (id != LOADER_ID) {
return null;
}
return new CursorLoader(LanguageListActivity.this,
LanguageContentProvider.CONTENT_URI,
new String[] { DatabaseConstants.COL_LANG_ID, DatabaseConstants.COL_LANG_NAME }, null, null,
null);
}
#Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
_adapter.swapCursor(data);
}
#Override
public void onLoaderReset(Loader<Cursor> loader) {
_adapter.swapCursor(null);
}
}
container_list.xml:
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
container_list_item_view.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:gravity="center_vertical"
android:orientation="horizontal"
android:padding="2dip" >
<TextView
android:id="#+id/list_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="4dip" />
</LinearLayout>

How to get proper item in listview?

I writing test application and I get a strange behavior of mainActivity.listItem, when I click on item yet another item can get visible,it can get dissapear by scrolling.I'm confused.Thanks!
MainActivity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
stethoInit();
mainActivity = DataBindingUtil.setContentView(this, R.layout.activity_main);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
cursor = getContentResolver().query(MyProvider.CONTENT_URI, null, null, null, null, null);
}
String[] from = new String[]{MyProvider.ID, MyProvider.TEXT};
int[] to = new int[]{R.id.t_id, R.id.t_text};
scAdapter = new SimpleCursorAdapter(this, R.layout.item, cursor, from, to, 0);
mainActivity.listItem.setAdapter(scAdapter);
scAdapter.notifyDataSetChanged();
mainActivity.listItem.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
FrameLayout v = (FrameLayout)view.findViewById(R.id.updateLayout);
Log.d("!!!",v.toString());
v.setVisibility(View.VISIBLE);
}
});
mainActivity.insert.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Fragment insertFragment = new Insert();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.container, insertFragment);
ft.addToBackStack(null);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
}
});
}
item.xml
<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:orientation="vertical" >
<FrameLayout
android:id="#+id/updateLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone">
<!-- TODO: Update blank fragment layout -->
<EditText
android:id="#+id/updateTextField"
android:layout_width="match_parent"
android:layout_height="50dp"
android:ems="10"
android:hint="#string/insertTextFieldHint"
android:focusable="false"
android:inputType="textPersonName" />
<Button
android:id="#+id/uodateRecButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="125dp"
android:layout_marginTop="65dp"
android:focusable="false"
android:text="#string/updateRecButton" />
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="#+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"/>
<TextView
android:id="#+id/t_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:id="#+id/t_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
UPDATE
I solved this problem by extending SimpleCursorAdapter ,maybe someone share with another solution?
private class MyAdapter extends SimpleCursorAdapter{
private int layout;
public MyAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags) {
super(context, layout, c, from, to, flags);
this.layout = layout;
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
String id = cursor.getString(colId);
String title = cursor.getString(colText);
TextView idView = (TextView)view.findViewById(R.id.t_id);
idView.setText(id);
TextView textView = (TextView)view.findViewById(R.id.t_text);
textView.setText(title);
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View view = getLayoutInflater().inflate(layout,parent,false);
return view;
}
#Override
public int getItemViewType(int position) {
return position;
}
#Override
public int getViewTypeCount() {
return getCount();
}
}

NullPointerException error in my ListView

I have a fragment which I want to display a ListView
here's a portion of my code
public class ExpensesDaily extends Fragment{
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
lvExpenses = (ListView) getActivity().findViewById(android.R.id.list);
Cursor expensesListCursor;
DbControl dbc = new DbControl(getActivity());
try {
dbc.open();
expensesListCursor = dbc.listExpenses();
String[] from = new String[] {"description", "cost" };
ListAdapter adapter = new SimpleCursorAdapter(getActivity(), R.layout.list_expenses, expensesListCursor, from, new int[] { R.id.tvDescription, R.id.tvCost },0);
lvExpenses.setAdapter(adapter);
}catch (SQLException e){
}
dbc.close();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.expenses_daily, container, false);
return rootView;
}
}
** DbControl class**
public class DbControl {
private SQLiteDatabase database;
private MySQLiteHelper dbHelper;
public DbControl(Context context) {
dbHelper = new MySQLiteHelper(context);
}
public void open() throws SQLException {
database = dbHelper.getWritableDatabase();
}
public void close() {
dbHelper.close();
}
public Cursor listExpenses(){
Cursor cursor;
cursor = database.query(MySQLiteHelper.TABLE_EXPENSES, new String[] {"id _id", "description, cost"}, "forDate='" + date + "'", null, null, null, "id asc");
if (cursor != null) {
cursor.moveToFirst();
}
return cursor;
}
expenses_daily.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/ll_expensesDaily"
android:baselineAligned="false">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
>
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
</LinearLayout>
list_expenses.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="7dip">
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.6"
android:id="#+id/tvDescription" />
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:id="#+id/tvCost" />
</LinearLayout>
</LinearLayout>
but everytime I load the fragment, it will display this error
FATAL EXCEPTION: main java.lang.NullPointerException at com.code.imin.mymoneymanaged.ExpensesDaily.onCreate(ExpensesDaily.java:60)
which points to the line
lvExpenses.setAdapter(adapter) in class ExpensesDaily.
lvExpenses = (ListView) getActivity().findViewById(android.R.id.list);
You can't call getActivity().findViewById on onCreate because the view was not created yet. Change that logic to onActivityCreated

populate ListFragment with Cursor items without ContentProvider

I'd like to use my own CursorAdapter class to populate a ListFragment. The cursor is returned from my DB API class. I don't use any ContentProvider.
The cursor is loaded properly, and ListFragment.getListAdapter().getCount() returns number of items from the cursor.
There is just no list showing on the UI ...
This is my ListFragment class:
public class ArticlesListFragment extends ListFragmen
public ArticlesListFragment(int userId, ArticleType type, DBAdapter db) {
ArticlesListFragment.userId = userId;
this.type = type;
this.dbAdapter = db;
Log.d(TAG, "Constructor: type=" + type);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setHasOptionsMenu(true);
mInflater = LayoutInflater.from(getActivity());
cursor = dbAdapter.getArticles(userId, type);
mAdapter = new ListAdapter(getActivity(), cursor, true);
setListAdapter(mAdapter);
//-----> When I call getListAdapter().getCount() here I get a proper number of articles.
}
}
And this is the ListAdapter::
public class ListAdapter extends CursorAdapter {
private Context mContext;
private final LayoutInflater mInflater;
public ListAdapter(Context context, Cursor c, boolean autoRequery) {
super(context, c, autoRequery);
mInflater = LayoutInflater.from(context);
mContext = context;
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
TextView title = (TextView) view.findViewById(R.id.article_title);
title.setText(cursor.getString(cursor
.getColumnIndex(Column._TITLE.name)));
TextView text = (TextView) view.findViewById(R.id.article_text);
text.setText(cursor.getString(cursor
.getColumnIndex(Column._TEXT.name)));
TextView date = (TextView) view.findViewById(R.id.article_date);
date.setText(cursor.getString(cursor
.getColumnIndex(Column._CREATED_DATE.name)));
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
final View view = mInflater.inflate(R.layout.articles_list_item,
parent, false);
return view;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (!mDataValid) {
throw new IllegalStateException(
"this should only be called when the cursor is valid");
}
if (!mCursor.moveToPosition(position)) {
throw new IllegalStateException(
"couldn't move cursor to position " + position);
}
View v;
if (convertView == null) {
v = newView(mContext, getCursor(), parent);
} else {
v = convertView;
}
bindView(v, mContext, getCursor());
return v;
}
}
Thank you
EDIT:
public class ArticlesFragment extends Fragment implements OnTabChangeListener {
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
dbAdapter = new DBAdapter(activity);
dbAdapter.open();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mRoot = inflater.inflate(R.layout.articles_tabs_fragment, null);
return mRoot;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setRetainInstance(true);
FragmentManager fm = getFragmentManager();
if (fm.findFragmentByTag(tabId) == null) {
fm.beginTransaction()
.replace(placeholder,
new ArticlesListFragment(userId, ArticleType
.valueOf(tabId), dbAdapter)).commit();
}
}
This is the R.layout.articles_tabs_fragment complete layout file.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="horizontal" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0" >
<FrameLayout
android:id="#+id/starredArticles"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<FrameLayout
android:id="#+id/newArticles"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<FrameLayout
android:id="#+id/readArticles"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</FrameLayout>
</LinearLayout>
This is my list item layout. What I notice while debugging is that CursorAdapter's getView() method is never called..
<LinearLayout
android:id="#+id/first_row"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="3dp" >
<TextView
android:id="#+id/article_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ellipsize="middle"
android:paddingRight="5dp"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/article_date"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:textSize="10dp" />
</LinearLayout>
<TextView
android:id="#+id/article_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/first_row"
android:layout_span="2"
android:textAppearance="?android:attr/textAppearanceSmall" />

autocompletetextview not working

I have a contact list and when I want to make a search in my list I want to use an autocompletetext view but it is not working.
When I start typing in autocomplete text view nothing appears, but when I click on the search button it finds me the contact I want.
here is my .java:
public class Search extends ListActivity {
private static int[] TO = {R.id.rowid,R.id.name, R.id.mobilephone, R.id.email };
private static String[] FROM = {_ID,DbConstants.NAME, DbConstants.PHONE, DbConstants.EMAIL, };
private Button sButton;
private ListView lv1;
private static SQLiteDatabase db;
private DbCreate contacts;
private Cursor cursor;
private EditText searchText;
protected SimpleCursorAdapter adapter;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.search);
searchText=(EditText)findViewById(R.id.searchtext);
sButton=(Button)findViewById(R.id.searchButton);
sButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
showDatabaseContent();
lv1 = getListView();
lv1.setTextFilterEnabled(true);
}
});
}
private Cursor getContacts() {
db = contacts.getReadableDatabase();
cursor = db.rawQuery("SELECT _id,name, phone, email FROM contactTest1 WHERE name LIKE ?",
new String[]{searchText.getText().toString()+"%"});
startManagingCursor(cursor);
return cursor;
}
public void showDatabaseContent(){
contacts = new DbCreate(this);
try {
cursor = getContacts();
showContacts(cursor);
} finally {
contacts.close();
db.close();
}
}
private void showContacts(Cursor cursor) {
//set up data binding
adapter = new SimpleCursorAdapter(this, R.layout.item, cursor, FROM, TO);
setListAdapter(adapter);
}
public void onListItemClick(ListView parent, View v, int position, long id) {
Intent abaintent = new Intent(this,Detalii.class);
Cursor cursor = (Cursor) adapter.getItem(position);
abaintent.putExtra("Contact_ID", cursor.getInt(cursor.getColumnIndex("_id")));
startActivity(abaintent);
}
}
here is my search.xml:
<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk//android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<AutoCompleteTextView
android:id="#+id/searchtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="searchDefault" >
<requestFocus />
</AutoCompleteTextView>
<Button android:id="#+id/searchButton"
android:text="Search"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Why do you have there
android:layout_weight="1"
?
I would remove it.

Categories

Resources