ListView没有显示所有数据?

时间:2012-02-16 03:53:18

标签: android listview listadapter baseadapter

我为我在这里发布的列表视图制作了一个自定义适配器,但问题是它没有显示所有项目 - 只是第一个。为什么是这样?它从另一个Java文件读取静态String []时工作正常......

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

My 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;
}
}

1 个答案:

答案 0 :(得分:2)

而不是

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

public int getCount() {
    return gradesName.length
}