这是我的android监视器的输出。实际上我在列表视图中添加了21个项目,但它只出现了9个项目。我没有对arrayadapter编号设置任何限制。
01-05 20:44:23.368 5969-5969/? I/System.out: getView 0 null
01-05 20:44:23.378 5969-5969/? I/System.out: getView 1 null
01-05 20:44:23.388 5969-5969/? I/System.out: getView 2 null
01-05 20:44:23.398 5969-5969/? I/System.out: getView 3 null
01-05 20:44:23.408 5969-5969/? I/System.out: getView 4 null
01-05 20:44:23.418 5969-5969/? I/System.out: getView 5 null
01-05 20:44:23.438 5969-5969/? I/System.out: getView 6 null
01-05 20:44:23.448 5969-5969/? I/System.out: getView 7 null
01-05 20:44:23.458 5969-5969/? I/System.out: getView 8 null
01-05 20:44:23.488 5969-5969/? I/System.out: getView 0 android.widget.LinearLayout{41bb18f8 V.E..... ......ID 0,0-432,72}
01-05 20:44:23.488 5969-5969/? I/System.out: getView 1 android.widget.LinearLayout{41bb5708 V.E..... ......ID 0,73-432,145}
01-05 20:44:23.488 5969-5969/? I/System.out: getView 2 android.widget.LinearLayout{41bb9318 V.E..... ......ID 0,146-432,218}
01-05 20:44:23.488 5969-5969/? I/System.out: getView 3 android.widget.LinearLayout{41bbcf28 V.E..... ......ID 0,219-432,291}
01-05 20:44:23.488 5969-5969/? I/System.out: getView 4 android.widget.LinearLayout{41bc0b38 V.E..... ......ID 0,292-432,364}
01-05 20:44:23.488 5969-5969/? I/System.out: getView 5 android.widget.LinearLayout{41bc4748 V.E..... ......ID 0,365-432,437}
01-05 20:44:23.488 5969-5969/? I/System.out: getView 6 android.widget.LinearLayout{41bc8358 V.E..... ......ID 0,438-432,510}
01-05 20:44:23.488 5969-5969/? I/System.out: getView 7 android.widget.LinearLayout{41bcbf68 V.E..... ......ID 0,511-432,583}
MyCustomAdapter.java 这是我的自定义适配器类
public class MyCustomAdapter extends BaseAdapter {
public ArrayList<PackingItem> packingItems;
Context context;
LayoutInflater layoutInflater;
int count;
public MyCustomAdapter(Context context, int textViewResourceId,
ArrayList<PackingItem> countryList) {
this.context = context;
this.packingItems = countryList;
this.packingItems.addAll(countryList);
layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
private class ViewHolder {
CheckBox name;
public TextView number_text;
public Button plus;
public Button minus;
}
@Override
public int getCount() {
return packingItems.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
Log.v("ConvertView", String.valueOf(position));
System.out.println("getView " + position + " " + convertView);
if (convertView == null) {
//convertView = vi.inflate(R.layout.item_info, null);
holder = new ViewHolder();
convertView = layoutInflater.inflate(R.layout.item_info,parent,false);
holder.name = (CheckBox) convertView.findViewById(R.id.checkBox1);
holder.number_text= (TextView) convertView.findViewById(R.id.number_text);
convertView.setTag(holder);
holder.number_text.setText(String.valueOf(count));
holder.plus = (Button) convertView.findViewById(R.id.plus);
final ViewHolder finalHolder = holder;
holder.plus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (count>=0) {
count++;
}
finalHolder.number_text.setText(String.valueOf(count));
}
});
holder.minus = (Button) convertView.findViewById(R.id.minus);
holder.minus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (count > 0) {
count--;
} else {
if (count <= 0) {
count = 0;
}
finalHolder.number_text.setText(" ");
}
finalHolder.number_text.setText(String.valueOf(count));
}
});
holder.name.setOnClickListener( new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v ;
PackingItem packingItem = (PackingItem) cb.getTag();
Toast.makeText(context,
"Clicked on Checkbox: " + cb.getText() +
" is " + cb.isChecked() + " " + packingItem.getNumber(),
Toast.LENGTH_LONG).show();
packingItem.setSelected(cb.isChecked());
packingItem.setNumber(finalHolder.number_text.getText().toString());
}
});
}
else {
holder = (ViewHolder) convertView.getTag();
}
PackingItem packingItem = packingItems.get(position);
holder.name.setText(packingItem.getName());
holder.name.setChecked(packingItem.isSelected());
holder.name.setTag(packingItem);
holder.number_text.setTag(packingItem);
return convertView;
}
}
在我的 ItemList java类中,我也没有超出限制
public class ItemList extends AppCompatActivity {
MyCustomAdapter dataAdapter = null;
String[] shirtlist = {"Gloves", "Hat", "Jacket", "Shoes"};
String[] underwearlist = {"Bra", "Pants" ,"Bandana", "Belt", "Blouse", "Cap", "Coat"};
String[] weatherlist = {"umbrella", "Raincoat", "Sunglasses"};
String[] personallist = {"Glass / lenses", "Health insurance", "ID card", "Money / credit card",
"Passport / Visa", "Personal medication", "Tickets"};
Boolean shirt;
Boolean underwear;
Boolean weather,personal;
ArrayList<PackingItem> packingItems = new ArrayList<PackingItem>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_item_list);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
shirt =getIntent().getExtras().getBoolean("shirt");
underwear =getIntent().getExtras().getBoolean("underwear");
weather =getIntent().getExtras().getBoolean("weather");
personal =getIntent().getExtras().getBoolean("personal");
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
displayListView();
checkButtonClick();
}
private void checkButtonClick() {
Button myButton = (Button) findViewById(R.id.findSelected);
myButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
StringBuffer responseText = new StringBuffer();
responseText.append("The following were selected...\n");
ArrayList<PackingItem> packingItems = dataAdapter.packingItems;
for(int i=0;i<packingItems.size();i++){
PackingItem packingItem = packingItems.get(i);
if(packingItem.isSelected()){
responseText.append("\n" + packingItem.getName() + " " + packingItem.getNumber());
}
}
Toast.makeText(ItemList.this,
responseText, Toast.LENGTH_LONG).show();
}
});
}
private void displayListView() {
if (shirt){
for (int i = 0; i <shirtlist.length; i++){
PackingItem packingItem = new PackingItem(shirtlist[i],false);
packingItems.add(packingItem);
}
}
if (personal){
for (int i = 0; i <personallist.length; i++){
PackingItem packingItem = new PackingItem(personallist[i],false);
packingItems.add(packingItem);
}
}
if (underwear){
for (int i = 0; i <underwearlist.length; i++){
PackingItem packingItem = new PackingItem(underwearlist[i],false);
packingItems.add(packingItem);
}
}
if (weather){
for (int i = 0; i <weatherlist.length; i++){
PackingItem packingItem = new PackingItem(weatherlist[i],false);
packingItems.add(packingItem);
}
}
//create an ArrayAdaptar from the String Array
dataAdapter = new MyCustomAdapter(this,
R.layout.item_info, packingItems);
ListView listView = (ListView) findViewById(R.id.listView1);
// Assign adapter to ListView
listView.setAdapter(dataAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
PackingItem packingItem = (PackingItem) parent.getItemAtPosition(position);
Toast.makeText(getApplicationContext(),
"Clicked on Row: " + packingItem.getName(),
Toast.LENGTH_LONG).show();
}
});
}
}
我只是将数组放到数组适配器列表中。谢谢你,对不起我的英语。
答案 0 :(得分:0)
您的自定义列表适配器实现不正确 这是我的custim适配器的一个例子
public class ResultListAdapter extends BaseAdapter{
Context context;
UsersMain values;
LayoutInflater layoutInflater;
public ResultListAdapter(Context context,UsersMain values){
this.context=context;
this.values=values;
layoutInflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return values.getNearestUsers().size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if(convertView==null){
viewHolder= new ViewHolder();
convertView=layoutInflater.inflate(R.layout.match_list_item,parent,false);
viewHolder.username=(TextView)convertView.findViewById(R.id.userResquestTv);
convertView.setTag(viewHolder);
}
else {
viewHolder=(ViewHolder)convertView.getTag();
}
viewHolder.username.setText(values.getNearestUsers().get(position).getName());
return convertView;
}
public static class ViewHolder{
public TextView username;
}
}
按照这个。希望帮助
答案 1 :(得分:0)
我认为你缺少getCount()方法。重写该方法并返回list.Size()。希望它有所帮助。