将错误的值发送到搜索中的下一个活动

时间:2015-02-23 10:56:53

标签: android listview search android-activity import

我遇到搜索问题,当用户输入内容时,listview会发生变化,因此下一个活动会返回错误的值。 我的代码:

    package com.example.finaltest;

import java.util.ArrayList;
import java.util.HashMap;

import android.support.v7.app.ActionBarActivity;
import android.text.Editable;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.text.TextWatcher;
import android.view.View;

public class Search extends ActionBarActivity {

    // List view
    private ListView lv;

    // Listview Adapter
    ArrayAdapter<String> adapter;


    // Search EditText
    EditText inputSearch;


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

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.search);

                // Listview Data
        String products[] = getResources().getStringArray(R.array.search);

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

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

        /**
         * Enabling Search Filter
         * */
        inputSearch.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
                // When user changed the Text

            }

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

            }

            @Override
            public void afterTextChanged(Editable arg0) {
                // TODO Auto-generated method stub   
                Search.this.adapter.getFilter().filter(arg0);
            }
        });

        // after click
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, final View view,
                int position, long id) {

                final String item = lv.getItemAtPosition(position).toString();
                int total_number = 50;
                for(int x = 1; x < total_number+1; x = x+1) {
                        String SubjectName = "subject_" + String.valueOf(x);
                        int resID = getResources().getIdentifier(SubjectName, "string", getPackageName());
                        String subject = getResources().getString(resID);

                        if(item.equals(subject)) {
                                String StringClass = "com.example.finaltest.Subject_" + String.valueOf(x);
                                Class<?> c = null;
                                if(StringClass != null) {
                                    try {
                                        c = Class.forName(StringClass);
                                    } catch (ClassNotFoundException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                    }
                                }
                                Intent i = new Intent(getApplicationContext(), Show_Subjects.class);
                                String Subject_number = String.valueOf(position+1);
                                i.putExtra("subject_number", Subject_number);
                                startActivity(i); 
                        }
                }

            }

          });
        }

        @Override
        public void onResume() {
            super.onResume();
            adapter.notifyDataSetChanged();
        }


        @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) {
            int id = item.getItemId();
            switch (id) {
                case R.id.action_settings:
                    Intent delIntent = new Intent(Search.this, Settings.class);
                    Search.this.startActivity(delIntent);
                    return true;
                case R.id.action_favorites:
                    Intent delItemIntent = new Intent(Search.this, Favorites.class);
                    Search.this.startActivity(delItemIntent);
                    return true;
            }
            return id == R.id.action_settings || super.onOptionsItemSelected(item);
        }
    }

我的字符串名称是Subject_N,我想将N的值发送到下一个活动(来自项目行号)

我的语言是波斯语,我还有其他问题,搜索字段只支持英语!更改语言是非活动的

1 个答案:

答案 0 :(得分:0)

//outside the for loop
ArrayList<String> ArraySubject_number = new ArrayList<String>();
String Subject_number;

//initialize your arraylist inside the loop for each row
//use something like
//for each row
Subject_number = String.valueOf(position+1);
ArraySubject_number.add(Subject_number);  

//outside for loop
Intent intent= new Intent(getApplicationContext(), Show_Subjects.class);
intent.putExtras("subject_number", ArraySubject_number);

然后在您的其他活动中使用此代码访问它们

ArrayList<String> ArraySubject_number = (ArrayList<String>) getIntent().getSerializableExtra("subject_number");