Listview未在第一次更新时已进入活动声音

时间:2017-01-03 12:43:07

标签: android

1- MainActivity:

public class MainActivity extends AppCompatActivity {

    private List<String> mListItems = Arrays.asList("Hamaki","Amr Diab");
    private ArtistsAdapter mAdapter;

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

        ListView listView = (ListView)findViewById(R.id.Artist_list_view);

        mAdapter = new ArtistsAdapter(this, mListItems);
        listView.setAdapter(mAdapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                    Intent intent = new Intent(MainActivity.this,SoundActivity.class);
                    intent.putExtra("Artist", mListItems.get(position));
                    startActivity(intent);
                }

        });

    }

}

2 - 声音活动:

public class SoundActivity extends AppCompatActivity {


    @Override
    protected void onStart() {
        super.onStart();

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sound);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction().add(R.id.container, new SoundFragment()).commit();
        }

    }

3-Sound Fragment:

public class SoundFragment extends Fragment {

    static SCTrackAdapter mAdapter;
    static DatabaseReference db;
    static FirebaseHelper helper;
    private TextView mSelectedTrackTitle;
    static ArrayList<Music> mTracks = new ArrayList<>();
    static MediaPlayer mMediaPlayer;
    private ImageView mPlayerControl;
    static String Artist;
    static ListView listView;
    int currentTrack;
    private static String fileName;

    public SoundFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onStart() {
        super.onStart();

        new Fetchtracks().execute();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_sound, container, false);

        Intent intent = getActivity().getIntent();
        if (intent != null) {
            Artist = intent.getStringExtra("Artist");
        }

        listView = (ListView) rootView.findViewById(R.id.track_list_view);
        mAdapter = new SCTrackAdapter(getActivity(), mTracks);
        listView.setAdapter(mAdapter);
        return rootView;
    }

4- STrack适配器:

public class SCTrackAdapter extends BaseAdapter {

    private Context mContext;
    private ArrayList<Music> mTracks;

    public SCTrackAdapter(Context context, ArrayList<Music> tracks) {
        mContext = context;
        mTracks = tracks;
    }

    public void update_tracks(ArrayList<Music> list)
    {
        mTracks.clear();
        mTracks.addAll(list);
    }
    @Override
    public int getCount() {
        return mTracks.size();
    }

    @Override
    public Music getItem(int position) {
        return mTracks.get(position);
    }

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

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

        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.track_list_row, parent, false);
            holder = new ViewHolder();
            holder.titleTextView = (TextView) convertView.findViewById(R.id.track_title);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.titleTextView.setText(mTracks.get(position).getName());
        return convertView;
    }

    static class ViewHolder {
        TextView titleTextView;
    }

}

5- Fetchtracks:

公共类Fetchtracks扩展了AsyncTask&gt; {

@覆盖

protected ArrayList<Music> doInBackground(Void... voids) {

    db = FirebaseDatabase.getInstance().getReference().child(Artist);
    helper = new FirebaseHelper(db);
    mTracks.addAll(helper.retrieve());
   // Log.e("doInBackground: ",mTracks.size()+"");

    return mTracks;
}

@Override
protected void onPostExecute(ArrayList<Music> list) {
    super.onPostExecute(list);
    Log.e("doInBackground: ",list.size()+"");
    mAdapter.update_tracks(list);
    mAdapter.notifyDataSetChanged();
    listView.setAdapter(mAdapter);
}

}

6- FirebaseHelper:

公共类FirebaseHelper {

DatabaseReference db;
Boolean saved=null;
ArrayList<Music> A = new ArrayList<>();

public FirebaseHelper(DatabaseReference db) {
    this.db = db;
}
//WRITE

public Boolean save(Music m)
{
    if(m==null)
    {
        saved=false;
    }else
    {
        try
        {
           // db.child(Artist).push().setValue(m);
            saved=true;
        }catch (DatabaseException e)
        {
            e.printStackTrace();
            saved=false;
        }
    }
    return saved;
}
//READ
public ArrayList<Music> retrieve()
{
    A.clear();
    db.addChildEventListener(new ChildEventListener() {

        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            Log.e("onChildAdded: ", "1");
            fetchData(dataSnapshot);
        }
        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {
            Log.e("onChildAdded: ", "2");
            fetchData(dataSnapshot);
        }
        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {
           // Log.e("onChildAdded: ", "3");
        }
        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {
           // Log.e("onChildAdded: ", "4");
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {
           // Log.e("onChildAdded: ", "5");
        }
    });

    return A;
}
private void fetchData(DataSnapshot dataSnapshot)
{
        Music m=new Music();

        m.setName(dataSnapshot.child("name").getValue().toString());
        m.setUrl(dataSnapshot.child("url").getValue().toString());
        A.add(m);

// SoundFragment.mTracks.add(m); // Log.e(“onFetch:”,SoundFragment.mTracks.size()+“”);

}

}

7-音乐:

public class Music {

    String name;
    String url;

    public Music() {

    }
    public void setName(String name) {
        this.name = name;
    }
    public void setUrl(String url) {
        this.url = url;
    }
    public String getName() {
        return name;
    }
    public String getURL() {
        return url;
    }
}

3 个答案:

答案 0 :(得分:1)

doInBackground()始终返回mTracks,之前未修改,因此返回空列表! 您需要从helper.retrieve();onPostExecute()将数据传递给适配器!

答案 1 :(得分:1)

在onCreateView()中检查mTracks列表大小,将适配器设置为列表视图。

可能你的曲目请求没有完成(doInBackground()正在运行),片段显示在屏幕上,空列表。

答案 2 :(得分:0)

doInBackground()中的

mTracks为空

返回值为空