CustomListView and CursorAdapter Searching issue
I have a custom ListView and I can access and show data from database. The
problem is When I don't perform any search, onItemClick is working fine.
But, when I try to Search any ListView item and want to access that item.
It shows this
09-14 06:17:50.280: E/AndroidRuntime(1091): FATAL EXCEPTION: main
09-14 06:17:50.280: E/AndroidRuntime(1091):
java.lang.IllegalArgumentException: column 'AF_NAME' does not exist
09-14 06:17:50.280: E/AndroidRuntime(1091): at
android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:303)
09-14 06:17:50.280: E/AndroidRuntime(1091): at
com.yasiradnan.abstracts.AbstractActivity$1.onItemClick(AbstractActivity.java:111)
09-14 06:17:50.280: E/AndroidRuntime(1091): at
android.widget.AdapterView.performItemClick(AdapterView.java:298)
09-14 06:17:50.280: E/AndroidRuntime(1091): at
android.widget.AbsListView.performItemClick(AbsListView.java:1100)
09-14 06:17:50.280: E/AndroidRuntime(1091): at
android.widget.AbsListView$PerformClick.run(AbsListView.java:2749)
09-14 06:17:50.280: E/AndroidRuntime(1091): at
android.widget.AbsListView$1.run(AbsListView.java:3423)
09-14 06:17:50.280: E/AndroidRuntime(1091): at
android.os.Handler.handleCallback(Handler.java:725)
09-14 06:17:50.280: E/AndroidRuntime(1091): at
android.os.Handler.dispatchMessage(Handler.java:92)
09-14 06:17:50.280: E/AndroidRuntime(1091): at
android.os.Looper.loop(Looper.java:137)
09-14 06:17:50.280: E/AndroidRuntime(1091): at
android.app.ActivityThread.main(ActivityThread.java:5041)
09-14 06:17:50.280: E/AndroidRuntime(1091): at
java.lang.reflect.Method.invokeNative(Native Method)
09-14 06:17:50.280: E/AndroidRuntime(1091): at
java.lang.reflect.Method.invoke(Method.java:511)
09-14 06:17:50.280: E/AndroidRuntime(1091): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-14 06:17:50.280: E/AndroidRuntime(1091): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-14 06:17:50.280: E/AndroidRuntime(1091): at
dalvik.system.NativeStart.main(Native Method)
here is my Activity.java code
AbstractCursorAdapter cursorAdapter;
ListView listView;
EditText searchOption;
Cursor cursor;
ListView lv;
String authorNames;
String is_Corrospondence;
String getAfNumber;
String getAuthorID;
DatabaseHelper dbHelper = new DatabaseHelper(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.abstract_general);
listView = (ListView)findViewById(R.id.list);
searchOption = (EditText)findViewById(R.id.abSearch);
dbHelper.open();
String query = "select
abstracts_item._id,CORRESPONDENCE,title, type, topic,
text,af_name,REFS,ACKNOWLEDGEMENTS from
abs_affiliation_name,abstract_affiliation,abstracts_item,abstract_author,authors_abstract
where abstracts_item._id = authors_abstract.abstractsitem_id
and abstract_author._id = authors_abstract.abstractauthor_id
and abstract_affiliation._id = abstract_author._id and
abs_affiliation_name._id = abstracts_item._id GROUP By
abstracts_item._id";
cursor = DatabaseHelper.database.rawQuery(query, null);
Log.e("Cursor Count", String.valueOf(cursor.getCount()));
/*
* Check If Database is empty
*/
if (cursor.getCount() <= 0) {
datainList();
cursor = DatabaseHelper.database.rawQuery(query, null);
}
cursorAdapter = new AbstractCursorAdapter(this, cursor);
listView.setAdapter(cursorAdapter);
listView.setTextFilterEnabled(true);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view,
int position, long arg3) {
// TODO Auto-generated method stub
cursor = (Cursor)cursorAdapter.getCursor();
String Text =
cursor.getString(cursor.getColumnIndexOrThrow("TEXT"));
String Title =
cursor.getString(cursor.getColumnIndexOrThrow("TITLE"));
String Topic =
cursor.getString(cursor.getColumnIndexOrThrow("TOPIC"));
String value =
cursor.getString(cursor.getColumnIndexOrThrow("_id"));
String afName =
cursor.getString(cursor.getColumnIndexOrThrow("AF_NAME"));
String email =
cursor.getString(cursor.getColumnIndexOrThrow("CORRESPONDENCE"));
String refs =
cursor.getString(cursor.getColumnIndexOrThrow("REFS"));
String acknowledgements =
cursor.getString(cursor.getColumnIndexOrThrow("ACKNOWLEDGEMENTS"));
Log.e("Position", String.valueOf(position));
int itemNumber = cursor.getCount();
Intent in = new Intent(getApplicationContext(),
AbstractContent.class);
in.putExtra("abstracts", Text);
in.putExtra("Title", Title);
in.putExtra("Topic", Topic);
in.putExtra("value", value);
in.putExtra("afName", afName);
in.putExtra("email", email);
in.putExtra("refs", refs);
in.putExtra("itemNumber", itemNumber);
in.putExtra("acknowledgements",acknowledgements);
startActivity(in);
}
});
/*
* Serach Filter
*/
cursorAdapter.setFilterQueryProvider(new FilterQueryProvider() {
public Cursor runQuery(CharSequence constraint) {
//return dbHelper.fetchDataByName(constraint.toString());
cursor = DatabaseHelper.database.rawQuery("select
ABSTRACTS_ITEM._id, "
+
"GROUP_CONCAT(ABSTRACT_AUTHOR.NAME),ABSTRACTS_ITEM.TITLE,
ABSTRACTS_ITEM.TOPIC, "
+ "ABSTRACTS_ITEM.TYPE, ABSTRACTS_ITEM.TEXT, "
+ "ABSTRACT_KEY_WORDS.KEYWORDS "
+ "from ABSTRACTS_ITEM , ABSTRACT_AUTHOR ,
AUTHORS_ABSTRACT, ABSTRACT_KEY_WORDS "
+ "where ABSTRACTS_ITEM._id =
ABSTRACT_KEY_WORDS._id "
+ "and ABSTRACTS_ITEM._id =
AUTHORS_ABSTRACT.ABSTRACTSITEM_ID "
+ "and ABSTRACT_AUTHOR._id =
AUTHORS_ABSTRACT.ABSTRACTAUTHOR_ID "
+ "and (ABSTRACT_KEY_WORDS.KEYWORDS like '%" +
constraint.toString() + "%' "
+ "or ABSTRACTS_ITEM.TITLE like '%" +
constraint.toString() + "%' or
ABSTRACT_AUTHOR.NAME like '%" +
constraint.toString() + "%')GROUP BY
ABSTRACTS_ITEM._id", null);
return cursor;
}
});
searchOption.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int start, int
before, int count) {
// TODO Auto-generated method stub
AbstractActivity.this.cursorAdapter.getFilter().filter(cs);
}
@Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
}
private void datainList() {
try {
InputStream inStream =
this.getResources().openRawResource(R.raw.abstracts);
JSONArray jsonArray = JSONReader.parseStream(inStream);
for (int index = 0; index < jsonArray.length(); index++) {
JSONObject jsonObject = jsonArray.getJSONObject(index);
String topic = jsonObject.getString("topic");
String correspondence =
jsonObject.getString("correspondence");
String url = jsonObject.getString("url");
String coi = jsonObject.getString("coi");
String cite = jsonObject.getString("cite");
String type = jsonObject.getString("type");
String title = jsonObject.getString("title");
String refs = "";
if (jsonObject.has("refs")) {
refs = jsonObject.getString("refs");
}
String acknowledgements = "";
if(jsonObject.has("acknowledgements")){
acknowledgements =
jsonObject.getString("acknowledgements");
}
String text = jsonObject.getString("abstract");
dbHelper.addItems(null, text, topic, correspondence,
url, coi, cite, type, title, refs, acknowledgements);
JSONObject abAfData =
jsonArray.getJSONObject(index).getJSONObject("affiliations");
String af_name = abAfData.toString().replaceAll("\\{",
"").replaceAll("\\}", "");
dbHelper.addAbsAffiliation(af_name, null);
JSONArray getKeywords = new
JSONArray(jsonObject.getString("keywords"));
String keywordsData =
String.valueOf(getKeywords).replaceAll("\\[", "")
.replaceAll("\\]",
"").toString().replace("\"", "");
dbHelper.addKeyWord(keywordsData, null);
JSONArray getAuthorsArray = new
JSONArray(jsonObject.getString("authors"));
for (int counter = 0; counter <
getAuthorsArray.length(); counter++) {
JSONObject authjsonObJecthor =
getAuthorsArray.getJSONObject(counter);
JSONArray getNumbers = new JSONArray(
authjsonObJecthor.getString("affiliations"));
authorNames = authjsonObJecthor.getString("name");
String is_Corrospondence = "";
if (authjsonObJecthor.has("corresponding")) {
is_Corrospondence =
authjsonObJecthor.getString("corresponding");
}
getAfNumber =
getNumbers.toString().replaceAll("\\[",
"").replaceAll("\\]", "");
dbHelper.addAbstractAffiliation(null, getAfNumber);
if (!dbHelper.Exists(authorNames)) {
dbHelper.addAuthors(null, authorNames,
is_Corrospondence);
dbHelper.addAuthorsAbstractItems(dbHelper.items_id,
dbHelper.authors_id,
dbHelper.abstract_affiliation_id);
dbHelper.authorsAffiliation(dbHelper.abstract_affiliation_id,
dbHelper.authors_id);
if (is_Corrospondence.equalsIgnoreCase("True")) {
dbHelper.addCorrespondingAuthor(dbHelper.items_id,
dbHelper.authors_id);
}
} else {
cursor = dbHelper.database.rawQuery(
"select _id from abstract_author where
NAME like '%" + authorNames
+ "%'", null);
try {
if (cursor.moveToFirst()) {
getAuthorID = cursor.getString(0);
}
} finally {
cursor.close();
}
if (is_Corrospondence.equalsIgnoreCase("True")) {
dbHelper.addCorrespondingAuthor(dbHelper.items_id,
Integer.parseInt(getAuthorID));
}
dbHelper.addAuthorsAbstractItems(dbHelper.items_id,
Integer.parseInt(getAuthorID),
dbHelper.abstract_affiliation_id);
dbHelper.authorsAffiliation(dbHelper.abstract_affiliation_id,
Integer.parseInt(getAuthorID));
}
}
}
} catch (FileNotFoundException e) {
Log.e("jsonFile", "file not found");
} catch (IOException e) {
Log.e("jsonFile", "ioerror");
} catch (JSONException e) {
e.printStackTrace();
}
}
}
Here is my Adapter code
Cursor cursorOne;
String getName;
@SuppressWarnings("deprecation")
public AbstractCursorAdapter(Context context, Cursor c) {
super(context, c);
// TODO Auto-generated constructor stub
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
// TODO Auto-generated method stub
TextView title = (TextView)view.findViewById(R.id.abTitle);
title.setText(cursor.getString(cursor.getColumnIndexOrThrow("TITLE")));
TextView topic = (TextView)view.findViewById(R.id.abTopic);
topic.setText(cursor.getString(cursor.getColumnIndexOrThrow("TOPIC")));
TextView type = (TextView)view.findViewById(R.id.abType);
type.setText(cursor.getString(cursor.getColumnIndexOrThrow("TYPE")));
String value = cursor.getString(cursor.getColumnIndexOrThrow("_id"));
String sqlQuery = "select abstracts_item._id AS
ID,abstract_author.NAME AS NAME from
abstracts_item,abstract_author,authors_abstract where
abstracts_item._id = authors_abstract.abstractsitem_id and
abstract_author._id = authors_abstract.abstractauthor_id and ID =
"
+ value;
cursorOne = DatabaseHelper.database.rawQuery(sqlQuery, null);
if (cursorOne != null) {
cursorOne.moveToFirst();
do {
if (cursorOne.getPosition() == 0) {
getName =
cursorOne.getString(cursorOne.getColumnIndexOrThrow("NAME"));
} else if (cursorOne.isLast()) {
getName = getName + " & "
+
cursorOne.getString(cursorOne.getColumnIndexOrThrow("NAME"));
} else {
getName = getName + " , "
+
cursorOne.getString(cursorOne.getColumnIndexOrThrow("NAME"));
}
} while (cursorOne.moveToNext());
}
TextView authorNames = (TextView)view.findViewById(R.id.SubTitle);
/*
* Get Width
*/
WindowManager WinMgr =
(WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
int displayWidth = WinMgr.getDefaultDisplay().getWidth();
Paint paint = new Paint();
Rect bounds = new Rect();
int text_height = 0;
int text_width = 0;
paint.getTextBounds(getName, 0, getName.length(), bounds);
text_height = bounds.height();
text_width = bounds.width();
if (text_width > displayWidth) {
//Log.e("Width inside",
String.valueOf(text_width)+"--------------"+String.valueOf(displayWidth));
String output = getName.split(",")[0] + " et al. ";
authorNames.setText(output);
} else {
//Log.e("Width inside",
String.valueOf(text_width)+"--------------"+String.valueOf(displayWidth));
authorNames
.setText(getName.replaceAll("((?:^|[^A-Z.])[A-Z])[a-z]*\\s(?=[A-Z])",
"$1."));
}
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup
viewgroup) {
// TODO Auto-generated method stub
LayoutInflater inflater =
LayoutInflater.from(viewgroup.getContext());
View returnView = inflater.inflate(R.layout.abstract_content,
viewgroup, false);
return returnView;
}
}
Now, My question why this is happening? How can I solve that ? Can you
give me a tutorial project link similar search function and
customListview, So that I can see what they're doing. I tried to find it
on google,but didn't found. Thanks
nice post
ReplyDeleteoffice furniture in chennai