0

我为我在这里发布的列表视图制作了一个自定义适配器,但问题是它没有显示所有项目 - 只是第一个。为什么是这样?当它从另一个 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();          
        }       
    });
    }
}

我的 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;
}
}
4

1 回答 1

2

代替

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

public int getCount() {
    return gradesName.length
}
于 2012-02-16T04:13:31.013 回答