【问题标题】:Android get data from Listview and show in a new activityAndroid 从 Listview 获取数据并显示在新活动中
【发布时间】:2016-04-30 20:45:10
【问题描述】:

我是 Android 新手。我打电话给lv.setOnItemClickListener 从 ListView 中选择一个选定的项目,并通过一个新的意图在一个新的活动中显示数据。但我不知道能做到这一点。我需要一些帮助。

主活动

public class MainActivity extends Activity implements
        SearchView.OnQueryTextListener {

    ListView lv;
    SearchView search_view;

    String[] country_names, iso_codes;
    TypedArray country_flags;

    ArrayList<Country> countrylist;
    CustomAdapter adapter;

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

        lv = (ListView) findViewById(R.id.list_view);
        search_view = (SearchView) findViewById(R.id.search_view);

        country_names = getResources().getStringArray(R.array.country_names);
        iso_codes = getResources().getStringArray(R.array.iso_Code);
        country_flags = getResources().obtainTypedArray(R.array.country_flags);

        countrylist = new ArrayList<Country>();
        for (int i = 0; i < country_names.length; i++) {
            Country country = new Country(country_names[i], iso_codes[i],
                    country_flags.getResourceId(i, -1));
            countrylist.add(country);
        }

        adapter = new CustomAdapter(getApplicationContext(), countrylist);
        lv.setAdapter(adapter);

        lv.setOnItemClickListener(new OnItemClickListener(){

            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {


      **// I DON'T KNOW WHAT PUT HERE**





            }

        });

        search_view.setOnQueryTextListener(this);
    }

    @Override
    public boolean onQueryTextChange(String newText) {
        adapter.getFilter().filter(newText);
        return false;
    }

    @Override
    public boolean onQueryTextSubmit(String query) {
        return false;
    }

}

国家

公共类国家{

    String name;
    int flag;
    String iso_code;

    Country(String name, String iso_code, int flag) {
        this.name = name;
        this.iso_code = iso_code;
        this.flag = flag;
    }

    public String getIso_code() {
        return iso_code;
    }

    public void setIso_code(String iso_code) {
        this.iso_code = iso_code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getFlag() {
        return flag;
    }

    public void setFlag(int flag) {
        this.flag = flag;
    }

}

自定义适配器

public class CustomAdapter extends BaseAdapter implements Filterable {

    Context context;
    ArrayList<Country> countrylist;
    ArrayList<Country> mStringFilterList;
    ValueFilter valueFilter;

    CustomAdapter(Context context, ArrayList<Country> countrylist) {
        this.context = context;
        this.countrylist = countrylist;
        mStringFilterList = countrylist;
    }

    @Override
    public int getCount() {
        return countrylist.size();
    }

    @Override
    public Object getItem(int position) {
        return countrylist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return countrylist.indexOf(getItem(position));
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater mInflater = (LayoutInflater) context
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

        convertView = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.list_item, null);

            TextView name_tv = (TextView) convertView.findViewById(R.id.name);
            TextView iso_tv = (TextView) convertView.findViewById(R.id.code);
            ImageView iv = (ImageView) convertView.findViewById(R.id.flag);

            Country country = countrylist.get(position);

            name_tv.setText(country.getName());
            iso_tv.setText(country.getIso_code());
            iv.setImageResource(country.getFlag());
        }
        return convertView;
    }

    @Override
    public Filter getFilter() {
        if (valueFilter == null) {
            valueFilter = new ValueFilter();
        }
        return valueFilter;
    }

    private class ValueFilter extends Filter {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults results = new FilterResults();

            if (constraint != null && constraint.length() > 0) {
                ArrayList<Country> filterList = new ArrayList<Country>();
                for (int i = 0; i < mStringFilterList.size(); i++) {
                    if ((mStringFilterList.get(i).getName().toUpperCase())
                            .contains(constraint.toString().toUpperCase())) {

                        Country country = new Country(mStringFilterList.get(i)
                                .getName(), mStringFilterList.get(i)
                                .getIso_code(), mStringFilterList.get(i)
                                .getFlag());

                        filterList.add(country);
                    }
                }
                results.count = filterList.size();
                results.values = filterList;
            } else {
                results.count = mStringFilterList.size();
                results.values = mStringFilterList;
            }
            return results;

        }

        @Override
        protected void publishResults(CharSequence constraint,
                FilterResults results) {
            countrylist = (ArrayList<Country>) results.values;
            notifyDataSetChanged();
        }

    }

}

【问题讨论】:

    标签: android listview


    【解决方案1】:
    Intent intent=new Intent(getActivity(),StartAnotherActitivity.class);
    intent.putExtra(key,value);
    startActivity(intent);
    

    http://developer.android.com/reference/android/content/Intent.html

    【讨论】:

    • Intent类型中的putExtra(String, boolean)方法不适用于参数(String, Country)
    • 如果你想传入整个对象,该对象需要实现 Serializable 或 parseable 接口。 3pillarglobal.com/insights/… 当我需要跨活动或片段使用相同的数据时,我之前所做的就是使用单例。单例将有一个 ArrayList 来存储 Country 对象,因此现在您可以在应用程序的任何位置检索数据。
    【解决方案2】:

    这里是步骤。

    1. 获取点击项的位置:这个值会传递给onItemClick方法
    2. 当您尝试启动下一个activity 时,现在将此值作为Intent extra
    3. 从您的下一个Activity 中读取点击项目的index
    4. 获取详细信息activity的内容

    【讨论】:

      【解决方案3】:
      Intent intent = new Intent(MainActivity.this,NewActivity.class);
      intent.putExtra("Counttry",countrylist.get(arg2));
      startActivity(intent);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-03-10
        • 1970-01-01
        • 2022-06-13
        • 1970-01-01
        • 2023-03-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多