Android Search is not accurate when passing another activity .
This is my app
This the main page
[Main] https://drive.google.com/open?id=0B2SE2JZYuezja2JqRHcycWdhWTA
When I search of a word the first array description will be display and it is not accurate description .
This is the same output to the one that I click
[Same output]https://drive.google.com/open?id=0B2SE2JZYuezjQmNFSm45RHloT3c
This is my codes
MainActivity.java
package com.skholingua.android.searchview;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater.Filter;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SearchView;
public class MainActivity extends Activity implements
SearchView.OnQueryTextListener {
String[] stateList;
String[] anotherStringArray;
private SearchView searchView;
private ListView listView;
private ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
searchView = (SearchView) findViewById(R.id.searchView1);
listView = (ListView) findViewById(R.id.listView1);
stateList = getResources().getStringArray(R.array.stateList);
anotherStringArray = getResources().getStringArray(R.array.anotherStringArray);
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, stateList);
listView.setAdapter(adapter);
listView.setTextFilterEnabled(true);
// Sets the default or resting state of the search field.
// If true, a single search icon is shown by default and
// expands to show the text field and other buttons when pressed
//searchView.setIconifiedByDefault(false);
searchView.setOnQueryTextListener(this);
// Sets the hint text to display in the query text field
//searchView.setQueryHint("State Name");
int searchPlateId = searchView.getContext().getResources()
.getIdentifier("android:id/search_plate", null, null);
View searchPlateView = searchView.findViewById(searchPlateId);
if (searchPlateView != null) {
searchPlateView.setBackgroundColor(Color.TRANSPARENT);
}
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String stateName = listView.getAdapter().getItem(position).toString();
// Put selected state to intent.
Intent intent = new Intent(MainActivity.this, NextActivity.class);
intent.putExtra("selectedState", stateName);
intent.putExtra("anotherStringArray",anotherStringArray[position]);
startActivity(intent);
}
});
}
@Override
public boolean onQueryTextChange(String newText) {
android.widget.Filter filter = adapter.getFilter();
if (TextUtils.isEmpty(newText)) {
filter.filter("");
} else {
filter.filter(newText);
}
return true;
}
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
activity.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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:focusableInTouchMode="true"
tools:context="com.skholingua.android.searchview.MainActivity" >
<SearchView
android:id="@+id/searchView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:background="@drawable/search_view_border"
android:iconifiedByDefault="false"
android:padding="2dp"
android:queryHint="Search...."
android:layout_alignParentTop="true" >
</SearchView>
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/searchView1"
android:layout_centerHorizontal="true" >
</ListView>
</RelativeLayout>
NextActivity.class
package com.skholingua.android.searchview;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class NextActivity extends Activity {
TextView t1,t2;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.next);
t1 = (TextView) findViewById(R.id.name1);
t2 = (TextView) findViewById(R.id.name2);
Intent intent = getIntent();
String state_name= getIntent().getExtras().getString("selectedState");
String stateDescription= getIntent().getExtras().getString("anotherStringArray");
t1.setText(state_name);
t2.setText(stateDescription);
}
}
next.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"
android:background="#00ffff"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Word:"
android:typeface="serif"
android:textColor="#000"
android:textSize="19dp" />
<TextView
android:id="@+id/name1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text=""
android:textColor="#f00"
android:textSize="18dp"
android:typeface="serif" />
</LinearLayout>
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:textColor="#000"
android:typeface="serif"
android:text="Definition:"
android:textSize="19dp" />
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/name2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="#000"
android:padding="10dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:text=""
android:background="#fff"
android:textAppearance="?android:attr/textAppearanceMedium" />
</ScrollView>
</LinearLayout>
</RelativeLayout>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">SearchView</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string-array name="stateList">
<item>Abstract Window Toolkit (AWT)</item>
<item>abstract</item>
<item>abstract class</item>
<item>abstract method</item>
<item>access control</item>
<item>ACID</item>
<item>activation</item>
<item>actual parameter list</item>
<item>alpha value</item>
</string-array>
<string-array name="anotherStringArray">
<item>A collection of graphical user interface (GUI) components that were implemented usingnative-platform versions of the components. These components provide that subset of functionalitywhich is common to all native platforms. Largely supplanted by the Project Swing component set.</item>
<item>A Java(TM) programming language keyword used in a class definition to specify that a class is not to be instantiated, but rather inherited by other classes. An abstract class can have abstract method that are not implemented in the abstract class, but in subclasses.</item>
<item>A class that contains one or more abstract methods, and therefore can never be instantiated. Abstract classes are defined so that other classes can extend them and make them concrete by implementing the abstract methods.</item>
<item>A method that has no implementation.</item>
<item>The methods by which interactions with resources are limited to collections of users or programs for the purpose of enforcing integrity, confidentiality, or availability constraints.</item>
<item>The acronym for the four properties guaranteed by transactions: atomicity, consistency, isolation,and durability.</item>
<item>The process of transferring an enterprise bean from secondary storage to memory.</item>
</string-array>
</resources>
I hope you will help me to fix this problem . Thank you.
Here is your solution.
First replace your main activity listView.setOnItemClickListener=> onItemClick method with below code
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String stateName = listView.getAdapter().getItem(position).toString();
int pos=0;
for(int i=0;i<stateList.length;i++)
{
if(stateList[i].equals(stateName))
{
pos=i;
break;
}
}
// Put selected state to intent.
Intent intent = new Intent(MainActivity.this, NextActivity.class);
intent.putExtra("selectedState", stateName);
intent.putExtra("anotherStringArray", anotherStringArray[pos]);
startActivity(intent);
}
});
And also make your stateList array and and anotherStringArray as same size in string.xml file.
<string-array name="stateList">
<item>Abstract Window Toolkit (AWT)</item>
<item>abstract</item>
<item>abstract class</item>
<item>abstract method</item>
<item>access control</item>
<item>ACID</item>
<item>activation</item>
<item>actual parameter list</item>
<item>alpha value</item>
</string-array>
<string-array name="anotherStringArray">
<item>A collection of graphical user interface (GUI) components that were implemented usingnative-platform versions of the components. These components provide that subset of functionalitywhich is common to all native platforms. Largely supplanted by the Project Swing component set.</item>
<item>A Java(TM) programming language keyword used in a class definition to specify that a class is not to be instantiated, but rather inherited by other classes. An abstract class can have abstract method that are not implemented in the abstract class, but in subclasses.</item>
<item>A class that contains one or more abstract methods, and therefore can never be instantiated. Abstract classes are defined so that other classes can extend them and make them concrete by implementing the abstract methods.</item>
<item>A method that has no implementation.</item>
<item>The methods by which interactions with resources are limited to collections of users or programs for the purpose of enforcing integrity, confidentiality, or availability constraints.</item>
<item>The acronym for the four properties guaranteed by transactions: atomicity, consistency, isolation,and durability.</item>
<item>The process of transferring an enterprise bean from secondary storage to memory.</item>
<item>Add your text here</item>
<item>Add some other text here</item>
</string-array>