gpt4 book ai didi

android - Listview 没有第一次更新我已经进入了 Activity Sound

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:13:41 25 4
gpt4 key购买 nike

1- 主 Activity :

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-声音 Activity :

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 音 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:

公共(public)类 Fetchtracks 扩展 AsyncTask> {

@覆盖

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:

公共(public)类 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;
}
}

最佳答案

doInBackground() 总是返回 mTracks 之前没有修改,因此返回一个空列表!您需要返回从 helper.retrieve(); 接收到的数据,并且 onPostExecute() 将数据传递给适配器!

关于android - Listview 没有第一次更新我已经进入了 Activity Sound,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41443825/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com