Should I create an adapter for Android's ListView in a static inner class?

Asked 2 years ago, Updated 2 years ago, 27 views

The adapter of Android's ListView is implemented as a static inner class.
In order to see the Activity variable from the Adapter, we have a weak reference in the Adapter.

Below are some questions.

·Does mActivity.get() return null in Adapter?
If so, can I return null in TestAdapter.getView()?
However, an exception occurred when null was returned as fixed.

·Some website said that static classes do not leak memory, so I set Adapter to static inner classes, but is this the correct implementation?

Please give me some advice.

public class TestActivity extensions Activity {

  private List<String>mData=new ArrayList<String>();
  private ListView mListView;

  @ Override
  public void onCreate (Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.testactivity);
    mListView=(ListView) findViewById (R.id.listview);
    mListView.setAdapter (new TestAdapter (this));
  }

  private static class ViewHolder {

    public ViewHolder (View view) {
    }

  }

  private static class TestAdapter extensions BaseAdapter {

    private WakeReference<TestActivity>mActivity;

    public TestAdapter (TestActivity) {
      super();
      mActivity = new WakeReference <TestActivity > (activity);
    }

    @ Override
    public intgetCount(){
      TestActivity activity=mActivity.get();
      return activity!=null?activity.mData.size(): 0;
    }

    @ Override
    publicObjectgetItem(int position){
      TestActivity activity=mActivity.get();
      return activity!=null?activity.mData.get(position): null;
    }

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

    @ Override
    publicView getView(int position, View convertView, ViewGroup parent) {
      MovieListActivity activity=mActivity.get();
      if(activity==null)
        return null;// Can I return null here?

    }

  }

}

android

2022-09-29 21:51

2 Answers

·Does mActivity.get() return null in the adapter?

It's impossible with a normal implementation, but if you want to force yourself to write a code that happens, it's not impossible.I can't say that what you did without being conscious of it doesn't cause the same result.If you're not sure what kind of writing is within the "normal" range, you'd better think it's possible.

If so, can I return null in TestAdapter.getView()?

TestAdapter.getView() is called because ListView is trying to act as ListView, so returning null is still likely to be an exception.If you take the "possibly" position above, you should think that null should not be returned.

However, it may be possible to intentionally make an exception and find a bug quickly because it usually doesn't happen without some kind of bug, whether it's forced or unconscious.

·Some site says that static classes do not leak more memory, so I made Adapter static inner classes, but is this the correct implementation?

Not all non-static inner classes cause memory leaks, but even those who fully understand how "non-static inner classes cause memory leaks" often find it extremely difficult to look at a particular code and determine if that code causes memory leaks.Furthermore, if you don't have enough mechanisms, you should remember that you must be static when writing an inner class.

However, there should be an option to choose a completely different class instead of an inner class in the first place, so it is very doubtful whether it is necessary to choose an inner class with such care.I can't say whether it's the right implementation or not.

Last but not least,

You have a weak reference in your adapter.

I would like to assert that this is the correct implementation.It is the right decision to use weak references to avoid the fact that there will be clear circular references if not weak references.Even in Java, where garbage collection works, the presence of circular references is a heavy burden on garbage collectors, and if you fully understand how non-static inner classes cause memory leaks above, you should understand that circular references increase the risk.

If you can avoid it easily, you should avoid circular references and keep in mind.


2022-09-29 21:51

The adapter contains weak references to Activity.

I don't think it's necessary to keep it as a weak reference.

·Will mActivity.get() return null in Adapter?

As long as it's a weak reference, I can't say that there isn't.

Can I return null in TestAdapter.getView()?

I don't think it's going to work because it falls.

I made Adapter a static inner class, but is it the correct implementation?

Correct implementation.If not static, memory leaks can occur.

As for my impression, there seems to be no particular reason to use WeakReference this time, so

private TestActivity mActivity;
public TestAdapter (TestActivity) {
  super();
  mActivity = activity;
}

I think it would be good to say that


2022-09-29 21:51

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.