英文:
Textview properties in ListView do not change
问题
以下是您要翻译的代码部分:
public class MainActivity extends AppCompatActivity {
DatabaseReference databaseReference;
List<pdfClass> uploads;
ListView listview;
FloatingActionButton floatingActionButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview = findViewById(R.id.listview);
floatingActionButton = findViewById(R.id.floatingActionButton);
uploads = new ArrayList<>();
viewAllFiles();
listview.setOnItemClickListener((parent, view, position, id) -> {
pdfClass pdfupload = uploads.get(position);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setType("application/pdf");
intent.setData(Uri.parse(pdfupload.getUrl()));
startActivity(intent);
});
floatingActionButton.setOnClickListener(view->{
Intent intent = new Intent(getApplicationContext(),UploadPDF.class);
startActivity(intent);
});
}
private void viewAllFiles() {
databaseReference = FirebaseDatabase.getInstance().getReference("Uploads");
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
for (DataSnapshot postsnapshot : snapshot.getChildren()){
pdfClass pdfClass = postsnapshot.getValue(furkan.demirel.myapplication.pdfClass.class);
uploads.add(pdfClass);
}
String[] Uploads = new String[uploads.size()];
for (int i =0;i<Uploads.length;i++){
Uploads[i]=uploads.get(i).getName();
}
ArrayAdapter<String> adapter= new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_list_item_1,Uploads){
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent){
View view = super.getView(position,convertView,parent);
TextView text =(TextView) view.findViewById(android.R.id.text1);
text.setTextColor(Color.BLACK);
text.setTextSize(22);
return view;
}
};
listview.setAdapter(adapter);
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
}
请注意,这段代码涉及Android应用程序,主要是用于显示PDF文件列表,并设置了ListView
中TextView
的文本颜色和大小。
英文:
I have a ListView
, but when I try to change the text color and size of the TextView
in the ListView
, it doesn't change, but I don't get an error. I used to get errors when I tried something but now I don't. I do not know how to do it. Here is the image showing the ListView
:
public class MainActivity extends AppCompatActivity {
DatabaseReference databaseReference;
List<pdfClass> uploads;
ListView listview;
FloatingActionButton floatingActionButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview = findViewById(R.id.listview);
floatingActionButton = findViewById(R.id.floatingActionButton);
uploads = new ArrayList<>();
viewAllFiles();
listview.setOnItemClickListener((parent, view, position, id) -> {
pdfClass pdfupload = uploads.get(position);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setType("application/pdf");
intent.setData(Uri.parse(pdfupload.getUrl()));
startActivity(intent);
});
floatingActionButton.setOnClickListener(view->{
Intent intent = new Intent(getApplicationContext(),UploadPDF.class);
startActivity(intent);
});
}
private void viewAllFiles() {
databaseReference = FirebaseDatabase.getInstance().getReference("Uploads");
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
for (DataSnapshot postsnapshot : snapshot.getChildren()){
pdfClass pdfClass = postsnapshot.getValue(furkan.demirel.myapplication.pdfClass.class);
uploads.add(pdfClass);
}
String[] Uploads = new String[uploads.size()];
for (int i =0;i<Uploads.length;i++){
Uploads[i]=uploads.get(i).getName();
}
ArrayAdapter<String> adapter= new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_list_item_1,Uploads){
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent){
View view = super.getView(position,convertView,parent);
TextView text =(TextView) view.findViewById(android.R.id.text1);
text.setTextColor(Color.BLACK);
text.setTextSize(22);
return view;
}
};
listview.setAdapter(adapter);
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
}
答案1
得分: 1
你可以尝试更改布局。
像这样:
添加 custom_textview.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tv"
android:textColor="@color/black"
android:textSize="22sp"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
更改适配器的项目布局。
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.custom_textview, yourData);
listview.setAdapter(adapter);
如果仍然不起作用,你可以尝试实现适配器。
如果仍然不起作用,请考虑提前设置适配器。
英文:
You can try to change the layout.
Lile this:
Add custom_textview.xml
<? xml version="1.0" encoding=" utf-8 "? >
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tv"
android:textColor="@color/black"
android:textSize="22sp"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
Change adapter's item layout.
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.custom_textview, yourData);
listview.setAdapter(adapter);
If still not working, you can try implement Adapter
If still not working, think about set adapter early.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论