Sanfeyz
Sanfeyz

Reputation: 1

Android Programming Filtering Listview

I have a trouble to find the correct code.. I have use a filter on my listview,it works fine, but when I click a filtered item, then show wrong activity.. I've been stuck for a long time, any help will be great.. here is my code

public class Colour extends Activity {
// List view
private ListView lv;

// Listview Adapter
ArrayAdapter<String> adapter;

// Search EditText
EditText inputSearch;

// ArrayList for Listview
ArrayList<HashMap<String, String>> productList;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list);

    // Listview Data
    final String products[] = {"Blue","Green","Red"};


    lv = (ListView) findViewById(R.id.list);
    inputSearch = (EditText) findViewById(R.id.inputSearch);

    // Adding items to listview
    adapter = new ArrayAdapter<String>(this,
                  R.layout.colour, R.id.product_name, products);
    lv.setAdapter(adapter);

    /**
     * Enabling Search Filter
     * */

    inputSearch.addTextChangedListener(new TextWatcher() {

        public void onTextChanged(CharSequence cs, int arg1, int arg2,
                int arg3) {
            // When user changed the Text
            Colour.this.adapter.getFilter().filter(cs);
        }

        public void beforeTextChanged(CharSequence arg0, int arg1,
            int arg2, int arg3) {
            // TODO Auto-generated method stub

        }

        public void afterTextChange(Editable arg0) {
            // TODO Auto-generated method stub

        }

        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub

        }
    });

    lv.setOnItemClickListener(new OnItemClickListener() {
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            String openClass = products[position];
            if (openClass.equals("Blue")) {

                 Intent myIntent = new Intent(view.getContext(), Blue.class);
                 startActivityForResult(myIntent, 0);
            }
            else if (openClass.equals("Green")) {

                Intent myIntent1 = new Intent(view.getContext(), Green.class);
                startActivityForResult(myIntent1, 0);

            }
            else if (openClass.equals("Red")) {

                Intent myIntent2 = new Intent(view.getContext(), Red.class);
                startActivityForResult(myIntent2, 0);

            }

        }
    });
;

} }

Upvotes: 0

Views: 674

Answers (4)

Peter
Peter

Reputation: 5

What you need is pretty simple. You need to do few things.

First I'd change from:

Intent myIntent = new Intent(view.getContext(), Blue.class);

to:

Intent myIntent = new Intent(Colour.this, Blue.class);

Second. You need to put something extra to an intent in the class you start at and then you need to catch intent in the class you want it to get to with your extra thing.

So declare your "extra thing" like this before onCreate:

public final static String ID_E="yourpackagename._ID";

then put in in your intent:

Intent myIntent = new Intent(Colour.this, Blue.class);
intent.putExtra(ID_E, String.valueOf(id));
                 startActivity(myIntent);

and then you catch the "extra thing" in your target class, like this:

declare that before onCreate:

String passedVariable=null;

and catch it in onCreate:

 passedVariable=getIntent().getStringExtra(Colour.ID_E);

good luck!

Upvotes: 0

user2488738
user2488738

Reputation: 21

try replacing

String openClass = products[position]; 

with

String openClass = adapter.getItem(position);

Upvotes: 0

MH.
MH.

Reputation: 45503

Personally, I'd always get the clicked item from the supplied AdapterView. That's the only entity that will automatically take into account any header and/or footer views, and deal with other scenarios where your own adapter may get wrapped by another adapter for extra functionality.

lv.setOnItemClickListener(new OnItemClickListener() {
    @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String openClass = (String) parent.getItemAtPosition(position);
        // do something with the item ...
    }

Upvotes: 0

Dinithe Pieris
Dinithe Pieris

Reputation: 1992

there is an another efficient approach to list item click and show new activity. try this way

@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
            // getting values from selected ListItem
            String name = ((TextView) view.findViewById(R.id.name)).getText().toString();
            String cost = ((TextView) view.findViewById(R.id.email)).getText().toString();
            String description = ((TextView) view.findViewById(R.id.mobile)).getText().toString();

            // Starting new intent
            Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
            in.putExtra(TAG_NAME, name);
            in.putExtra(TAG_EMAIL, cost);
            in.putExtra(TAG_PHONE_MOBILE, description);
            startActivity(in);

        }
    });

Here is the "SingleMenuItemActivity class"

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class SingleMenuItemActivity  extends Activity {

private static final String TAG_NAME = "name";
private static final String TAG_EMAIL = "email";
private static final String TAG_PHONE_MOBILE = "mobile";
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.single_list_item);

    // getting intent data
    Intent in = getIntent();

    // Get JSON values from previous intent
    String name = in.getStringExtra(TAG_NAME);
    String cost = in.getStringExtra(TAG_EMAIL);
    String description = in.getStringExtra(TAG_PHONE_MOBILE);

    // Displaying all values on the screen
    TextView lblName = (TextView) findViewById(R.id.name_label);
    TextView lblCost = (TextView) findViewById(R.id.email_label);
    TextView lblDesc = (TextView) findViewById(R.id.mobile_label);

    lblName.setText(name);
    lblCost.setText(cost);
    lblDesc.setText(description);
    }
}

cheers..

Upvotes: 1

Related Questions