2011-10-14 74 views
1

我有一行的ListView(列表視圖是內部的LinearLayout)等ListView中沒有顯示結果,但是通過代碼

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
    <CheckBox 
     android:id="@+id/chk" 
     android:layout_alignParentLeft="true" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 
    <TextView 
     android:id="@+id/txtChoice" 
     android:textColor="#FF0000" 
     android:text="TEST" 
     android:layout_toRightOf="@id/chk" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" /> 
</RelativeLayout> 

和適配器等內部類

private class SimpleAdapter extends BaseAdapter { 

    private String[] list=null; 

    public SimpleAdapter(String[] list) { 
     this.list = list; 
    } 

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

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

    @Override 
    public long getItemId(int position) { 
     return list[position].hashCode(); 
    } 

    // Generate view for coupon's row 
    public View getView(int position, View convertView, ViewGroup parent) { 

     View view = convertView; 

     if(view == null){ 
     LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     view = inflater.inflate(R.layout.row, null); 
     }  

     return view; 
    } 
    } 

和我設置適配器等

 SimpleAdapter m=new SimpleAdapter(data); 
      listView.setAdapter(m); 

data is String[] data=new String[]{"test","test_1"}; 但列表不顯示任何事情。我流下了眼淚,它正常地流逝,但沒有任何表現。有什麼問題?

+0

你在哪裏使用getView()中的列表對象? – Pratik

回答

3

您在發佈相同的代碼問題,我已經嘗試過了。 以及完美運行..

我用我的活動相同的適配器類和XML

在我TestThis.java文件

我用你的適​​配器類,也 我已經寫了,

@Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     // setContentView(R.layout.maintwiter); 

     String[] data=new String[]{"test","test_1"}; 
     SimpleAdapter m=new SimpleAdapter(data); 
     ListView listView = new ListView(this); 
     listView.setAdapter(m); 
     setContentView(listView); 
    } 

但你仍然需要從String數組的數據設置爲文本視圖getView()方法

男人它給了我的結果列表與2行。

this

1
public View getView(int position, View convertView, ViewGroup parent) { 

    View view = convertView; 

    if(view == null){ 
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    view = inflater.inflate(R.layout.row, null); 
    }  

    return view; 
} 

修改getView本:

if(convertView == null){ 
    LayoutInflater inflater (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    convertView = inflater.inflate(R.layout.row, null); 
    } TextView t = (TextView) convertView.findViewById(your text view); t.setText(...); return convertView; 

這裏誇大你沒有設置行的內容行..使用convertiview以檢索您的文本視圖,調用getItem與通過位置作爲getView方法的參數,並設置您的文本。

2

我認爲你的getView()需要一些工作。你不是真的這樣做與你所傳遞的數據進行任何試試這個:

public View getView(int position, View convertView, ViewGroup parent) { 

    View v; 
    if (convertView == null) 
     v = LayoutInflater.from(context).inflate(R.layout.row, parent, false); 
    else 
     v = convertView; 

    TextView text = (TextView) v.findViewById(R.id.txChoice); 
    text.setText(list[position]);  

    return v; 

}

1

你也可以試試這個:

public View getView(int position, View convertView, ViewGroup parent) { 

      ViewHolder holder; 

      if(view == null) 
      { 
        LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
        convertView = mInflater.inflate(R.layout.row, null); 
        holder = new ViewHolder(); 

        holder.txtChoice=(TextView)convertView.findViewById(R.id.txtChoice);     

      } 
      else 
        holder=(ViewHolder)convertView.getTag();  

      holder.textChoice.setText(lis[position]));  

      return view; 
     } 

     static class ViewHolder 
     {  
      TextView txtChoice;   
     } 
相關問題