Java I';我制作了自己的ListView适配器,但没有打印任何数据
这是我尝试过的,但是我的ListView没有被填满。我使用自定义适配器,因为我不想更改没有布尔值=true的项目的背景色。我正在使用Android Studio。 希望有人能帮忙。 我不熟悉安卓系统Java I';我制作了自己的ListView适配器,但没有打印任何数据,java,android,listview,adapter,custom-adapter,Java,Android,Listview,Adapter,Custom Adapter,这是我尝试过的,但是我的ListView没有被填满。我使用自定义适配器,因为我不想更改没有布尔值=true的项目的背景色。我正在使用Android Studio。 希望有人能帮忙。 我不熟悉安卓系统 public class ShoppingListActivity extends ActionBarActivity { private TextView header; private ListView listView; private Cus
public class ShoppingListActivity extends ActionBarActivity {
private TextView header;
private ListView listView;
private CustomArrayAdapter arrayAdapter;
private ShoppingList shoppingList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shopping_list);
header = (TextView)findViewById(R.id.textView2);
//get reference to ListView
listView = (ListView)findViewById(R.id.shoppingListItemsView);
shoppingList = (ShoppingList) getIntent().getSerializableExtra(Globals.CHOSENLISTKEY);
arrayAdapter = new CustomArrayAdapter(this, R.layout.custom_list_view, shoppingList.getItemList());
listView.setAdapter(arrayAdapter);
header.setText(shoppingList.toString());
Button btnShop = (Button) findViewById(R.id.btnShop);
btnShop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(ShoppingListActivity.this, SelectStoreActivity.class);
startActivity(intent);
}
});
}
public void setData() {
for(int i = 0; i < shoppingList.getItemList().size(); i++) {
arrayAdapter.add(shoppingList.getItemList().get(i));
}
}
}
public class CustomArrayAdapter extends ArrayAdapter<Entry> {
private int resource;
private ArrayList<Entry> list;
public CustomArrayAdapter(Context context, int resource, ArrayList<Entry> list) {
super(context, resource);
this.resource = resource;
this.list = list;
}
@Override
public View getView(int position, View convertView, ViewGroup parentGroup) {
View view = convertView;
Entry entry = list.get((position));
if(view == null) {
LayoutInflater layoutInflater = ((Activity) getContext()).getLayoutInflater();
view = layoutInflater.inflate(resource, parentGroup, false);
}
TextView textView = (TextView) view.findViewById(R.id.customName);
if(entry.getItem().isBought()) {
textView.setBackgroundColor(Color.BLUE);
}
return view;
}
}
公共类ShoppingListActivity扩展了ActionBarActivity{
私有文本视图头;
私有列表视图列表视图;
私人客户阵列适配器阵列适配器;
私人购物清单;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u shopping\u list);
header=(TextView)findViewById(R.id.textView2);
//获取对ListView的引用
listView=(listView)findViewById(R.id.shoppingListItemsView);
shoppingList=(shoppingList)getIntent().getSerializableExtra(Globals.CHOSENLISTKEY);
arrayAdapter=new CustomArrayAdapter(这个,R.layout.custom_list_视图,shoppingList.getItemList());
setAdapter(arrayAdapter);
header.setText(shoppingList.toString());
按钮btnShop=(按钮)findViewById(R.id.btnShop);
btnShop.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
意向意向=新意向(ShoppingListActivity.this,选择StoreActivity.class);
星触觉(意向);
}
});
}
public void setData(){
对于(int i=0;i
要在ListView行中显示数据,请调用从getView
方法返回的TextView方法:
TextView textView = (TextView) view.findViewById(R.id.customName);
// get Text from entry and pass it to setText method
String strTextViewData=entry.getItem()...;
textView.setText(strTextViewData);
if(entry.getItem().isBought()) {
textView.setBackgroundColor(Color.BLUE);
}
尝试打印shoppingList.getItemList();我认为它将返回null或空值。你是否调用过
setData
?我打印了它,但它不是空的-revolution不在代码中,我不知道它为什么会丢失:Pi在添加代码时出现此错误java.lang.NullPointerException:尝试调用虚拟方法void android.widget.TextView.setText(java.lang.CharSequence)'在空对象上reference@user2993005:使用view=layoutInflater.flate(R.layout.custom\u list\u视图,父组,false)代码>行而不是view=layoutInflater.inflate(资源,父组,false)代码>好的,这给出了相同的错误,我在getView中的代码现在看起来像这样@user2993005:尝试使用LayoutInflater LayoutInflater=ShoppingListActivity.this.getLayoutInflater()代码>而不是LayoutInflater LayoutInflater=((活动)getContext())。getLayoutInflater()代码>其在2个单独类中的CustomArrayAdapter不是内部类。我发现ShoppingListActivity不是封闭类时出错