0

我很难找到正确的代码..我在列表视图上使用了一个过滤器,它工作正常,但是当我点击一个过滤的项目时,然后显示错误的活动..我已经卡了很长时间了,任何帮助会很棒..这是我的代码

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

            }

        }
    });
;

} }

4

4 回答 4

1

还有另一种有效的方法来列出项目单击并显示新活动。试试这个方法

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

        }
    });

这是“ SingleMenuItemActivity 类

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);
    }
}

干杯..

于 2013-09-20T03:11:01.950 回答
0

就个人而言,我总是从提供的AdapterView. 这是唯一会自动考虑任何页眉和/或页脚视图的实体,并处理您自己的适配器可能被另一个适配器包装以获得额外功能的其他场景。

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 ...
    }
于 2013-09-20T03:31:45.013 回答
0

你需要的很简单。你需要做的事情很少。

首先我会改变:

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

至:

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

第二。您需要在您开始的课程中为意图添加一些额外的内容,然后您需要在您希望它与您的额外内容相关的课程中捕捉意图。

所以在onCreate之前声明你的“额外的东西”:

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

然后输入您的意图:

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

然后你在你的目标类中捕捉到“额外的东西”,就像这样:

在 onCreate 之前声明:

String passedVariable=null;

并在 onCreate 中捕获它:

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

祝你好运!

于 2015-03-18T18:39:12.847 回答
0

尝试更换

String openClass = products[position]; 

String openClass = adapter.getItem(position);
于 2013-11-04T07:02:06.773 回答