1

我是Android新手,目前我正在开发一个包含简单文本列表视图的应用程序。当用户点击列表视图中的文本时,共享意图被触发。我使用了一个自定义适配器来为我的列表视图中的文本设置不同的字体。问题是当我运行应用程序时,Listview被看到,但内容不可见。但是当我点击列表视图时,共享意图被触发,因为它应该是。我不知道是什么原因造成的。我在堆栈溢出和谷歌搜索,并尝试了一些步骤,但它没有为我工作。请帮助我。提前致谢。Listview中的内容是不可见的

代码:

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); 

我没有得到任何错误。我可以看到用分隔每个内容的行的列表视图。但实际的内容(文本)是不可见的。请帮我

+0

首先,打开设备上的开发设置并启用“显示布局边界”重新打开您的应用程序,并查看列表项目布局,组件是否在屏幕上。 – Broak

+0

如果您的文本使用#fff,如果您的列表视图的背景为白色,您将看不到它 – Pztar

+0

@Pztar文本颜色不是问题。我检查了它 – eashdroidian

回答

3

你的ListViewItem包含一个TextView但其价值没有被初始化。 尝试在xml文件或java代码中为textView设置值。 尝试以下方法之一:

1. XML文件: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. Java代码: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; 
} 


} 
+0

我尝试了这两种方法,但listview只显示“This is text sample”。它不显示String []手机的内容。 – eashdroidian

+0

再次检查我的编辑CustomAdapter,我已经改变如何为TextView设置文本,因为你想 –

+0

记住,在getCount()你应该返回值的大小 –

1

我没有看到任何你在TextView中添加文本的地方。

你可以把数据在内部getView TextView的视图显示像()方法:

txt.setText(values.get(position)); 
+0

@Ranjith Pati的values.get(位置)不可用?有没有其他方法可以将文本的值设置为txt? – eashdroidian

+0

我使用txt.setText(values [position])解决了它; – eashdroidian