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