Breaking News

java – Image is visible but text is not visible in ListView-Exceptionshub

Questions:

I am trying to create a custom layout for list view in android studio. I was able to successfully get image in the in ListView on runtime but wasn’t able to print text in the list view . When i run my program in emulator it runs without any error but it doesn’t show any text it only shows image from drawables.

MainActivity.java

    import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.ListView;

public class MainActivity extends AppCompatActivity {
    private final  static String[] names={"A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A",
            "A","A","A","A","A","A","A","A","A","A","A","A","A","A",};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ListView ls = findViewById((R.id.customlistview));
        ls.setAdapter(new CustomListAdapter(getApplicationContext(),names,R.drawable.za));
    }
}

activity_main.xml

   


    

CustomListAdapter.java

    import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CustomListAdapter extends BaseAdapter {
    private final Context context;
    private final String[] names;
    private final int image;
    private final LayoutInflater layoutInflater;

    public  CustomListAdapter(Context context,String[] names,int image) {

        this.context=context;
        this.image=image;
        this.names=names;
        layoutInflater= (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return names.length;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View rootview;
        rootview = layoutInflater.inflate(R.layout.list_item_layout, null);
        TextView tv= rootview.findViewById(R.id.textView);
        ImageView iv=rootview.findViewById(R.id.imageView);
        tv.setText(names[position]);
        iv.setImageResource(image);
        return rootview;

    }
}

list_item_layout.xml

    


    

    

How to&Answers:

The issue is in your list_item_layout file. There is an issue with match_parent in your layout try using below code

 
 




Answer:

The problem was caused by the text colour , changing the colour of textview text in list_item_layout resolved the issue.

About rankhawks

Check Also

javascript – Readonly textfield tooltip using jquery

February 22, 2020 Jquery Leave a comment Questions: I am working on a web-app which …

javascript – Saving div innerText directly to database

Questions: I have a simple div with contenteditable and I want to store its value …

jquery – Javascript how to add a click event listener in the body?

Questions: How do i add a click event listener to the body so when i …

yii2 advanced app – How to Run .ics (outlook calender file) using php instead of download or read as a text file?

Questions: I am working on yii2 advanced app where i need to add my events …

jquery – Hide all list items except one when collapsed and show all when expanded in JavaScript

Questions: Suppose I do have a like One Two Three Four Now , I want …

Leave a Reply

Your email address will not be published. Required fields are marked *