【问题标题】:Adapter error on a ListViewListView 上的适配器错误
【发布时间】:2016-10-07 14:22:28
【问题描述】:

我想将包含一些特定数据的新布局添加到 ListView。
我创建了一个扩展 BaseAdapter 的类,但是当我尝试将我的数据和上下文(this)放入我的新类时,它显示错误

package com.bignner.hotel;

public class MainActivity extends AppCompatActivity {
private Context context;
private ListView Placename;
@Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button btn= (Button)findViewById(R.id.btnAdd);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Placename =(ListView) findViewById(R.id.ListView);

            RadioGroup RG=(RadioGroup)findViewById(R.id.radiogrop);
            int SelectedId= RG.getCheckedRadioButtonId();
            RadioButton RB= (RadioButton)findViewById(SelectedId);
            String textRB=RB.getText().toString();
            int place=0;

            switch (textRB){
                case "Gust-house":
                    place=R.drawable.g;
                    break;
                case "Apartment":
                    place=R.drawable.a;
                    break;
                case "Hotel":
                    place=R.drawable.h;
                    break;
            }
            TextView placeName=(TextView)findViewById(R.id.editTextName);
            String name=placeName.getText().toString();
            TextView placeAddress=(TextView)findViewById(R.id.editTextAddress);
            String address=placeAddress.getText().toString();

            List<HotelEntry> tourPlace = new ArrayList<HotelEntry>();
            tourPlace.add(new HotelEntry(name,place));
            Myadapter adapter = new Myadapter(this,tourPlace);
            Placename.setAdapter(adapter);

        }
    });
}
}

还有我的班级:

public class Myadapter extends BaseAdapter {

private Context context;
private List<HotelEntry> hotels;

@Override
public int getCount() {
    return hotels.size();
}

@Override
public Object getItem(int position) {
    return hotels.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = inflater.inflate(R.layout.my_layout,parent,false);
    TextView placeName = (TextView) convertView.findViewById(R.id.textViewName);
    placeName.setText(hotels.get(position).getHotelName());
    ImageView LetterIcon = (ImageView) convertView.findViewById(R.id.imageViewIcon);
    LetterIcon.setImageResource(hotels.get(position).getTypeIcon());
    return convertView;

}
}

错误在这一行:

Myadapter adapter = new Myadapter(this,tourPlace);

我也将 MainActivity.this 替换为 this,但我仍然看到该错误。

我不知道为什么以及如何解决它。
我很高兴能得到一些帮助。

错误:

【问题讨论】:

  • 它显示什么样的错误?

标签: android android-arrayadapter baseadapter android-context


【解决方案1】:

您还没有应用Myadapter 类的构造函数。所以你需要创建它。

public Myadapter(Context context, List<HotelEntry> list) { ... }

并带回MainActivity.this - 没有它,您将尝试将OnClickListener 作为构造函数参数。

【讨论】:

    【解决方案2】:

    类名放在this之前

    【讨论】:

      【解决方案3】:

      将适配器移动到onCreate。添加到列表并在 onClick 中通知适配器。

      (可选)还可以将所有findViewById 移动到onCreate,因为每次单击按钮时查找视图都会影响性能。

      public class MainActivity extends AppCompatActivity {
      
          List<HotelEntry> tourPlace = new ArrayList<HotelEntry>();
          Myadapter adapter;
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
      
              // These three lines need to be removed from the onClick
              Placename =(ListView) findViewById(R.id.ListView);
              Myadapter adapter = new Myadapter(this, tourPlace); 
              Planename.setAdapter(adapter);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多