简体   繁体   中英

How to call the method in Main_Activity Class from Helper class?

The display function is under the Main_activity class. It displays all the files under the given directory. It is called from

protected void OnCreate(Bundle SavedInstanceState)
{
 ..//other code
display(path)
}

public  void display(String path)
{
   try {
    File folderX = new File(path);
    ActionBar actionBar = getSupportActionBar();
    actionBar.setTitle(folderX.getParentFile().getName() + " / " + folderX.getName());

    final File[] folderXCHILD = folderX.listFiles();
    Arrays.sort(folderXCHILD, new FileComparator());
    GridView gridView = (GridView)findViewById(R.id.gridView);
    gridAdapter = new GridAdapter(this, folderXCHILD,isShow);
    gridView.setAdapter(gridAdapter);

}
catch (Exception e)
{
    Toast.makeText(this, "Cannot display the Content.", Toast.LENGTH_SHORT).show();
}

 }

Now, i have another class called GridAdapter which displays the content in grid. GridAdapter class is not an Activity itself.

 @Override
public View getView(int position, View convertView, ViewGroup parent) {
    final Holder holder = new Holder();

    View rowView;

    if (convertView == null) {

        rowView = inflater.inflate(R.layout.gridout, parent, false);

    } else {
        rowView = convertView;
    }
    holder.tv = (TextView) rowView.findViewById(R.id.textView1);
    holder.img = (ImageView) rowView.findViewById(R.id.imageView1);
    holder.chk = (CheckBox) rowView.findViewById(R.id.checkBox);

    if (isShow) {
        holder.chk.setVisibility(View.VISIBLE);
    } else {
        holder.chk.setVisibility(View.GONE);
    }
    final File f = data[position];

    if (f.isDirectory())
    {
      holder.img.setImageResource(R.mipmap.folderpic);
        holder.tv.setText(f.getName());
        holder.chk.setChecked(checkBoxState[position]);
    }
    else
    {
        ImageLoader.getInstance().displayImage("file://" + f.getPath(), holder.img);

        holder.tv.setBackgroundResource(R.color.colorSecond);
        holder.tv.setText("");
        holder.chk.setChecked(checkBoxState[position]);
    }



rowView.setOnClickListener(new View.OnClickListener() {
                 @Override
                 public void onClick(View v) {
                       if(f.isDirectory())
                              {
                     // I want to call the display(f.getPath()) method of MainActivity.
                 }
             });

    return rowView;
}

The problem is that i want to call the display() method from onClick() method inside GridAdapter. How do i proceed?

You have to options: 1. Cast context to an activity 2. Cast context to an interface

Looking at the line:

gridAdapter = new GridAdapter(this, folderXCHILD,isShow);

You are sending the context to the adapter. So inside the adapter save the reference of context and use it

((YourActivityOrInterface)context).YourFunctionOnActivity()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM