如何从内部方法引用数组到外部方法?

时间:2016-05-19 18:20:47

标签: android android-studio

这是我的Android应用程序项目,在此活动之前,我有一个通过intent发送变量的类。所以现在,我想再将它发送到另一个活动,它不能再通过Intent发送,因为我已经创建了适配器类(用于制作列表视图)。帮助我,我真的很困惑这个

活动:

  public class KantorActivity extends AppCompatActivity {


        @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_kantor);
        getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
        getSupportActionBar().setCustomView(R.layout.actionbar);

        SearchView searchView = (SearchView) findViewById(R.id.searchView);
        ListView listView = (ListView) findViewById(R.id.listView);

        final Adapter adapter = new Adapter(this, getKantors());
        listView.setAdapter(adapter);


       searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {

                adapter.getFilter().filter(newText);
                return false;
            }
        });


        }


     public ArrayList<Kantor> getKantors(){
            Intent next = getIntent();
            String[] namaKantor = next.getStringArrayExtra("namaKantor");
            int [] gambar = next.getIntArrayExtra("gambar");
            String[] desk = next.getStringArrayExtra("desk");

            ArrayList<Kantor> kantors = new ArrayList<Kantor>();
            Kantor k;

            SharedPreferences sharedPreferences = getSharedPreferences
                    ("id.ac.unsyiah.elektro.mobile.pservices", Context.MODE_PRIVATE);


            this.namaKantor=namaKantor; //...?
           this.gambar=gambar; //...?
            this.desk=desk; //.....?

            for (int i=0;i<namaKantor.length;i++){
                k =new Kantor(namaKantor[i], gambar[i], desk[i]);
                kantors.add(k);
            }
            return kantors;
        }

            String[]namaKantor; //HOW TO REFERENCE THIS ARRAY TO ARRAY inside method above??
            String []desk; //HOW TO REFERENCE THIS ARRAY TO ARRAY inside method above??
            int [] gambar; //HOW TO REFERENCE THIS ARRAY TO ARRAY inside method above??
        }

适配器:

    package id.ac.unsyiah.elektro.mobile.pservices;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

public class Adapter extends BaseAdapter implements Filterable {
    Context c;
    ArrayList<Kantor> kantors;
    CustomFilter filter;
    ArrayList<Kantor> filterList;

    public Adapter(Context c,ArrayList<Kantor>kantors){
        this.c=c;
        this.kantors=kantors;
        this.filterList=kantors;
    }

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

    @Override
    public Object getItem(int pos) {
        return kantors.get(pos);
    }

    @Override
    public long getItemId(int pos) {
        return 0;
    }

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

        LayoutInflater inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if(convertView==null){
            convertView=inflater.inflate(R.layout.model,parent,false);
        }
        //tampilan
        ImageView img= (ImageView) convertView.findViewById(R.id.imgModel);
        TextView tv = (TextView) convertView.findViewById(R.id.namaModel);
        TextView tv2 = (TextView) convertView.findViewById(R.id.deskModel);

        //set data
        img.setImageResource(kantors.get(pos).getImg());
        tv.setText(kantors.get(pos).getNama());
        tv2.setText(kantors.get(pos).getDesk());

        //itemClick
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OpenDetail(pos);
            }
        });
        return convertView;
    }

//THIS IS FOR OPEN ANOTHER ACTIVITY VIA INTENT
    private void OpenDetail (int pos) {
            Intent next = new Intent(c, DetailActivity.class);
           next.putExtra("pos", pos);
            c.startActivity(next);
    }

    @Override
    public Filter getFilter() {
        if(filter==null){
            filter=new CustomFilter();
        }
        return filter;
    }

    class CustomFilter extends Filter {

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults results = new FilterResults();
            if (constraint !=null & constraint.length()>0){
                constraint=constraint.toString().toUpperCase();

                ArrayList<Kantor> filters=new ArrayList<Kantor>();

                for(int i=0;i<filterList.size();i++){
                    if (filterList.get(i).getNama().toUpperCase().contains(constraint)){
                        Kantor k = new Kantor(filterList.get(i).getNama(), filterList.get(i).getImg(),filterList.get(i).getDesk());
                        filters.add(k);
                    }
                }
                results.count=filters.size();
                results.values=filters;
            }
            else{
                results.count=filterList.size();
                results.values=filterList;
            }

            return results;
        }

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

1 个答案:

答案 0 :(得分:0)

你需要多说一点。根据您提供的内容。我同意@PradeepCharan。通过简单地调用:

,肯定可以多次调用Intent
Intent intent=getIntent();

在此之后,提取您想要重用

的任何值

让你感到困惑的是一个奇迹!