2012-02-16 96 views
0

我爲我在這裏發佈的listview做了一個自定義的適配器,但問題是它沒有顯示所有的項目 - 只是第一個。爲什麼是這樣?它工作得很好,當它從另一個Java文件中讀取靜態的String [] ...ListView不顯示所有數據?

public class MyList extends Activity { 
/** Called when the activity is first created. */ 
ListView listView; 
ProgressDialog pd; 
String pas2s = ""; 
String user2s = ""; 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.list); 
    pas2s = getIntent().getExtras().getString("userthang"); 
    user2s = getIntent().getExtras().getString("passthang"); 
    listView = (ListView) findViewById(R.id.lv_country); 
    String[] haha = {"Hi","Bye","Shy"}; 
    String[] bahaha = {"Bye","Hi","Lye"}; 
    listView.setAdapter(new EfficientAdapter(this,haha,bahaha)); 
    listView.setOnItemClickListener(new OnItemClickListener(){ 
     public void onItemClick(AdapterView<?> arg0, View view,int position, long arg3)  { 
      // TODO Auto-generated method stub 
      Toast.makeText(getApplicationContext(),"You Selected Item  "+Integer.toString(position), Toast.LENGTH_LONG).show();   
     }  
    }); 
    } 
} 

我BaseAdapter是這樣的:

class EfficientAdapter extends BaseAdapter { 
private LayoutInflater mInflater; 
private String[] gradesName; 
private String[] gradesVal; 
public EfficientAdapter(Context context, String[] gradesname, String[] gradesval) { 
    mInflater = LayoutInflater.from(context); 
    gradesName = gradesname; 
    gradesVal = gradesval; 
} 

public int getCount() { 
    return CountriesList.abbreviations.length; 
} 

public Object getItem(int position) { 
    return position; 
} 

public long getItemId(int position) { 
    return position; 
} 

public View getView(int position, View convertView, ViewGroup parent) { 
    ViewHolder holder; 
    if (convertView == null) { 
     convertView = mInflater.inflate(R.layout.row, null); 
     holder = new ViewHolder(); 
     holder.text1 = (TextView) convertView 
       .findViewById(R.id.TextView01); 
     holder.text2 = (TextView) convertView 
       .findViewById(R.id.TextView02); 

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

    holder.text1.setText(gradesName[position]); 
    holder.text2.setText(gradesVal[position]); 

    return convertView; 
} 

static class ViewHolder { 
    TextView text1; 
    TextView text2; 
} 
} 

回答

2

而不是

public int getCount() { 
    return CountriesList.abbreviations.length; 
} 

public int getCount() { 
    return gradesName.length 
} 
+0

哦哈,這對我有點粗心!感謝你的回答! – 2012-02-16 04:30:36