我已经尝试过很多教程来为listview使用基础适配器但我无法使用它们。我在哪里错了?
MainActivity:
public class MainActivity extends Activity {
ListView listView;
int[] resIds = new int[] {R.drawable.apple,R.drawable.banana,R.drawable.orange,R.drawable.watermelon};
String[] balls = new String[] {"apple","banana","orange","watermelon"};
String[] engNames = {"1","2","3","4"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listView);
MyAdapter adapter = new MyAdapter(this);
listView.setAdapter(adapter);
Toast.makeText(this,this.toString(),Toast.LENGTH_LONG).show();
}
public class MyAdapter extends BaseAdapter {
Context context;
LayoutInflater myInflater;
public MyAdapter(Context C){
context = C;
myInflater = LayoutInflater.from(this.context);
}
@Override
public int getCount() {
return 0;
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = myInflater.inflate(R.layout.activity_my_layout,viewGroup,false);
ImageView imgLogo = (ImageView) view.findViewById(R.id.imgLogo);
TextView txtName = (TextView) view.findViewById(R.id.txtName);
TextView txtendName = (TextView) view.findViewById(R.id.txtendName);
imgLogo.setImageResource(resIds[i]);
txtName.setText(balls[i]);
txtendName.setText(engNames[i]);
Toast.makeText(MainActivity.this,"Test",Toast.LENGTH_SHORT).show();
return view;
}
}
}
答案 0 :(得分:2)
你必须覆盖getCount& getItem正确。
@Override
public int getCount() {
return balls.length; //your data list length
}
@Override
public Object getItem(int i) {
return balls[i]; //data list item
}