2014-12-03 89 views
1

我的自定義列表視圖顯示空行,但是當我使用logcat顯示數據時,它顯示數據全部存在於我的數組列表中...如何使listview顯示我的數組列表項目?顯示空行的自定義列表視圖

public class Myarrayadapter extends ArrayAdapter<String> { 

Context context; 
ArrayList<String> values; 
int resid; 

public Myarrayadapter(Context context, int resid, ArrayList<String> values) { 
    super(context, resid, values); 
    this.resid = resid; 
    this.context = context; 
    this.values = values; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View rowView = inflater.inflate(resid, parent, false); 
    return rowView; 
} 
} 

CLASSS HERE ///////////////////// ///

final ListView HomeListView = (ListView) findViewById(R.id.HomeListView); 
    String[] filelist = fileList(); 
    ArrayList<String> fileList = new ArrayList<String>(); 

    for (int i=0; i < filelist.length; i++) {//displays only first half of job number 
      String[] stemp = filelist[i].split("\\."); 
     if(!fileList.contains(stemp[0])) {//makes sure only one job number is show in the list 
      fileList.add(stemp[0]); 
     } 
    } 
    Collections.reverse(fileList); 
    Myarrayadapter listAdapter = new Myarrayadapter(this, R.layout.homelistview, fileList); 
    HomeListView.setAdapter(listAdapter); 
    HomeListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      //get the clicked items job no. and pass it to the next activity which will display all relevant jobs 
      Intent detailedlist_intent = new Intent(Home.this, Home2.class); 
      detailedlist_intent.putExtra("Jobno", HomeListView.getItemAtPosition(position).toString()); 
      startActivity(detailedlist_intent); 
     } 
    }); 
+1

您是否在列表上設置適配器? – kha 2014-12-03 08:57:32

+0

檢查:http://stackoverflow.com/questions/19079400/arrayadapter-in-android-to-create-simple-listview – 2014-12-03 09:01:03

+1

你膨脹的觀點,但從來沒有設置任何值,所以如果XML視圖(渣油)是空的或者有空的textViews你會得到空行?! – Yazan 2014-12-03 09:06:48

回答

1

則應將值設置爲渣油布局內的TextView。您沒有將文本設置爲textview。這就是爲什麼它的空白。

1

創建自定義列表 1Create模型保存數據對每個列表視圖行。 2.將數據保存到模型。 3.獲取ArrayList中的每個Model類對象。 4.創建自定義適配器。將Arraylist傳遞給適配器。適配器爲每個listview行遞歸調用,並從ArrayList中的Model中獲取數據。 5.在每個Listview行中調用適配器內的xml文件(使用適配器內的LayoutInflater)。 6.內部MainActivity設置適配器的ListView 7. AnadroidMainifest.xml文件定義列表

參考您可以使用下面的鏈接

http://androidexample.com/How_To_Create_A_Custom_Listview_-_Android_Example/index.php?view=article_discription&aid=67&aaid=92