Textview properties in ListView do not change.

huangapple go评论75阅读模式
英文:

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文件列表,并设置了ListViewTextView的文本颜色和大小。

英文:

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:

Textview properties in ListView do not change.

public class MainActivity extends AppCompatActivity {
DatabaseReference databaseReference;
List&lt;pdfClass&gt; 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&lt;&gt;();
viewAllFiles();
listview.setOnItemClickListener((parent, view, position, id) -&gt; {
pdfClass pdfupload = uploads.get(position);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setType(&quot;application/pdf&quot;);
intent.setData(Uri.parse(pdfupload.getUrl()));
startActivity(intent);
});
floatingActionButton.setOnClickListener(view-&gt;{
Intent intent = new Intent(getApplicationContext(),UploadPDF.class);
startActivity(intent);
});
}
private void viewAllFiles() {
databaseReference = FirebaseDatabase.getInstance().getReference(&quot;Uploads&quot;);
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&lt;Uploads.length;i++){
Uploads[i]=uploads.get(i).getName();
}
ArrayAdapter&lt;String&gt; adapter= new ArrayAdapter&lt;String&gt;(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

&lt;? xml version=&quot;1.0&quot; encoding=&quot; utf-8 &quot;? &gt;
&lt;TextView xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
android:id=&quot;@+id/tv&quot;
android:textColor=&quot;@color/black&quot;
android:textSize=&quot;22sp&quot;
android:layout_width=&quot;fill_parent&quot;
android:layout_height=&quot;fill_parent&quot;/&gt;

Change adapter's item layout.

ArrayAdapter&lt;String&gt; adapter = new ArrayAdapter&lt;String&gt;(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.

huangapple
  • 本文由 发表于 2023年5月8日 02:46:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76195714.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定