Search code examples
androidlistviewandroid-intentandroid-xmlsearchview

Android Intent Listview in Another page with a Searchview


Please Help! I really need this app. today.

This is the main page of the app.

Mainpage Then after that, when you search and you click one of that word I want to display like this,

Secondpage but how ?

This is my codes :

MainActivity.class

package com.skholingua.android.searchview;

import android.app.Activity;
import android.content.Intent;
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 {

    private SearchView searchView;
    private ListView listView;
    private ArrayAdapter<String> adapter;


    private final String[] stateList = { "Example", "Goa", "Karnataka",
            "Kerala", "Maharashtra", "Madhya Pradesh", "Tamil Nadu" };
    private final String[] anotherStringArray = { "Example_description", "Goa_description", "Karnataka_description",
                    "Kerala_description", "Maharashtra_description", "Madhya Pradesh_description", "Tamil Nadu_description" };

    @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);
        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");

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String stateName = listView.getAdapter().getItem(position).toString();
                //String stateName = stateList[position];
                //Log.e("Selected State Name", stateName);

                // Put selected state to intent.
                Intent intent = new Intent(MainActivity.this, NextActivity.class);
                intent.putExtra("selectedState", stateName);
                intent.putExtra("anotherStringArray",anotherStringArray[position]);
                startActivity(intent);
            }
        });

    }

    // Called when the query text is changed by the user.

     @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_main.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"
      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: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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
     >



    <TextView
        android:id="@+id/name1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#000"
         />

    <TextView
        android:id="@+id/name2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#000"
         />



</RelativeLayout>

I really need this app. Hope there's someone who will help me. Thank you so much.


Solution

  • You can add description for each stateList in anotherStringArray with respective index. And you can simply get description from list item position.

    private final String[] stateList = { "Example", "Goa", "Karnataka",
                "Kerala", "Maharashtra", "Madhya Pradesh", "Tamil Nadu" };
    private final String[] anotherStringArray = { "Example_description", "Goa_description", "Karnataka_description",
                        "Kerala_description", "Maharashtra_description", "Madhya Pradesh_description", "Tamil Nadu_description" };
    
    
             listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            String stateName = listView.getAdapter().getItem(position).toString();
                            Log.e("Selected State Name", stateName);
    
                            // Put selected state to intent.
                            Intent intent = new Intent(MainActivity.this, NextActivity.class);
                            intent.putExtra("selectedState", stateName);
                            intent.putExtra("anotherStringArray",anotherStringArray[position])
                            startActivity(intent);
                        }
                    });
    

    And on NextActivity

    String state_name= getIntent().getExtras().getString("selectedState");
    String stateDescription= getIntent().getExtras().getString("anotherStringArray");