Reputation: 33
I am new to Android and I am currently developing an app that contains a listview with simple text. When the user clicks the text in the listview a sharing intent is triggered. I have used a custom adapter to set a different typeface to the text in my listview. The problem is when I run the app the listview is being seen but the content is invisible. But When I tap on the listview the sharing intent is triggered as it should be. I don't know what is causing it. I searched in Stack Overflow and google and tried some steps but it didn't work out for me. Please help me out. Thanks in advance.
Code:
Main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.proda.technologies.app.testapp"
android:background="@drawable/inspirationalbg">
<ImageButton
android:layout_width="130dp"
android:layout_height="40dp"
android:id="@+id/back_Button"
android:background="@drawable/backbutton"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ListView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/list"
android:layout_below="@+id/back_Button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="#ffffff"
android:id="@+id/instView"/>
</LinearLayout>
CustomAdapter.java
public class CustomAdapter extends ArrayAdapter<String>{
private final Context context;
private final String[] values;
public CustomAdapter(Context context, String[] values) {
super(context,R.layout.list_item,values);
this.context = context;
this.values = values;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
View rview = inflater.inflate(R.layout.list_item,null);
TextView txt = (TextView)rview.findViewById(R.id.instView);
AssetManager mgr = context.getAssets();
Typeface tf = Typeface.createFromAsset(mgr,"fonts/RalewayRegular.ttf");
txt.setTypeface(tf);
return rview;
}
}
Main.java
String[] phone = {"Nexus","Htc","Samsung","Oppo","Apple"};
Listview inslist;
inslist = (ListView)findViewById(R.id.list);
CustomAdapter adapter = new CustomAdapter(Main.this,phone);
inslist.setAdapter(adapter);
I am not getting any errors. I can see the listview with the lines seperating each content. But the actual content(text) is invisible. Please help me
Upvotes: 1
Views: 236
Reputation: 3751
Your ListViewItem contain a TextView but its value was not init. Try to set value for textView in xml file or in java code. Try one of below ways:
1. By XML file: list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="#ffffff"
android:id="@+id/instView"
android:text="This is text sample"/>
</LinearLayout>
2. by Java code: CustomAdapter.java
public class CustomAdapter extends ArrayAdapter<String>{
private final Context context;
private final String[] values;
public CustomAdapter(Context context, String[] values) {
super(context,R.layout.list_item,values);
this.context = context;
this.values = values;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
View rview = inflater.inflate(R.layout.list_item,null);
TextView txt = (TextView)rview.findViewById(R.id.instView);
txt.setText(values[position]);
AssetManager mgr = context.getAssets();
Typeface tf = Typeface.createFromAsset(mgr,"fonts/RalewayRegular.ttf");
txt.setTypeface(tf);
return rview;
}
}
Upvotes: 3
Reputation: 5150
I didn't see anywhere that you added text inside TextView.
Can you put the data to show in TextView view inside getView() method like :
txt.setText(values.get(position));
Upvotes: 1