我已经编写了一个代码来上传音频文件及其名称和艺术家名称并成功上传。但是当我从列表视图中的firebase获取数据时,它只显示列表视图中的歌曲名称,但我想在列表视图的子项目中显示艺术家名称 我的代码 获取代码。
ViewUploadsActivity extends AppCompatActivity {
//the listview
ListView listView;
//database reference to get uploads data
DatabaseReference mDatabaseReference;
//list to store uploads data
List<Upload> uploadList;
Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_upload);
uploadList = new ArrayList<>();
listView = (ListView) findViewById(R.id.listView);
//adding a clicklistener on listview
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Upload upload = uploadList.get(i);
try {
MediaPlayer player = new MediaPlayer();
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setDataSource(upload.getUrl());
if (player.isPlaying()) {
player.stop();
player.release();
} else {
player.prepare();
player.start();
}
}catch (Exception e) {
// TODO: handle exception
}
}
});
//getting the database reference
mDatabaseReference = FirebaseDatabase.getInstance().getReference(Constants.DATABASE_PATH_UPLOADS);
//retrieving upload data from firebase database
mDatabaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
Upload upload = postSnapshot.getValue(Upload.class);
uploadList.add(upload);
}
String[] uploads = new String[uploadList.size()];
for (int i = 0; i < uploads.length; i++) {
uploads[i] = uploadList.get(i).getName();
}
//displaying it to list
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,uploads);
listView.setAdapter(adapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
上传文件代码
public class Upload {
public String name;
public String url;
public String ar;//artist
// Default constructor required for calls to
// DataSnapshot.getValue(User.class)
public Upload() {
}
public Upload(String name, String url,String ar) {
this.name = name;
this.url = url;
this.ar=ar;
}
public String getName() {
return name;
}
public String getUrl() {
return url;
}
public String getAr ()
{
return ar;
}
}
输出我上传的内容
我得到了什么
答案 0 :(得分:0)
因为你使用它:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,uploads);
初始化适配器时,您只能获得一个文本,例如数据库中的name
(Dil Diyan Gallar)。
这部分:
android.R.layout.simple_list_item_1
表示每个项目中只有一个textview
,因此没有子项目。如果您希望有多个textview
使用自定义适配器或使用simple_list_item_2
并相应地更改代码。
答案 1 :(得分:0)
使用android.R.layout.simple_list_item_1
时,您的.XML
文件看起来实际上是这样的:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:minHeight="?android:attr/listPreferredItemHeightSmall" />
这意味着在您的布局中,您只有一个TextView
。我从你的截图中看到你需要有两个TextView。所以,你也可以使用:
android.R.layout.simple_list_item_1
但这也是 constraint
。最好的解决方案是使用您自己的自定义ArrayAdapter
。为此,您首先需要为名为.XML
的项目列表创建list_item.xml
文件。此文件应如下所示:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/first_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/second_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
您的CustomArrayAdapter
应如下所示:
public class CustomArrayAdapter extends ArrayAdapter<Upload> {
CustomArrayAdapter(Context context, ArrayList<Upload> arrayList) {
super(context, 0, arrayList);
}
@NonNull
@Override
public View getView(int position, @Nullable View listItemView, @NonNull ViewGroup parent) {
if(listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
}
TextView firstTextView = listItemView.findViewById(R.id.first_text_view);
TextView secondTextView = listItemView.findViewById(R.id.second_text_view);
Upload upload = getItem(position);
if (upload != null) {
firstTextView.setText(upload.getAr());
secondTextView.setText(upload.getName());
}
return listItemView;
}
}
要调用CustomArrayAdapter
类的构造函数,您需要使用以下代码行:
CustomArrayAdapter adapter = new CustomArrayAdapter(this, yourArrayList);
然后将适配器设置为ListView
,如下所示:
listView.setAdapter(adapter);
使用此代码,您可以随意使用任意数量的视图。