【问题标题】:ArrayAdapter requires the resource id to be a textview in listview Android StudioArrayAdapter 要求资源 id 是 listview Android Studio 中的 textview
【发布时间】:2015-12-01 11:00:35
【问题描述】:

我已经为此工作了一段时间,但无法真正弄清楚为什么会出现多个错误。任何帮助将不胜感激。 - 我创建了一个类并从共享首选项文件夹中获取字符串并将它们分配给类中的数据成员。 - 我制作了一个自定义 adpater,以便使用线性布局和每个布局中的两个 texview 将这些项目分配到列表视图中 - 我收到关于 id 的错误以及关于无法将布局转换为 textview 的错误

public class MainActivity extends AppCompatActivity {

private static final String GROCERIES = "items";

private EditText itemEditText;
private SharedPreferences savedItems;
private ArrayList<String> ITEMS;
private ArrayList<String> People;
private ArrayList<CustomObject> objectList;
private ArrayAdapter<CustomObject>  adapter;
private TextView anchor;

//private ListView christList;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ListView christList =(ListView) findViewById(R.id.listView);

    savedItems = getSharedPreferences("items", MODE_PRIVATE);



    People = new ArrayList<String>(savedItems.getAll().keySet());
    Collections.sort(People, String.CASE_INSENSITIVE_ORDER);

    objectList = new ArrayList<CustomObject>();

    for (int i = 0; i < People.size();  i++){
        String PersonName = People.get(i);

        ITEMS =new ArrayList<String>( Arrays.asList(getItemList(this, PersonName)));

        for (int j = 0; j < ITEMS.size(); j++){
            CustomObject newObject = new CustomObject(ITEMS.get(j), PersonName);
            objectList.add(newObject);
        }

    }


    adapter = new ChristmasAdapter(this, R.layout.dual_list_item,R.id.item_name,R.id.family_member, objectList);
    christList.setAdapter(adapter);

   /* adapter = new ArrayAdapter<String>(this, R.layout.list_item, ITEMS);
    christList.setAdapter(adapter);

    adapter.notifyDataSetChanged();
   */

    ImageButton addButton =
            (ImageButton) findViewById(R.id.add_button);
    addButton.setOnClickListener(addButtonListener);

    //christList.setOnItemClickListener(itemClickListener);

   // christList.setOnItemLongClickListener(itemLongClickListener);
}

public static String[] getItemList(Activity activity,String Key){
    String ItemsList = getStringsFromPreferences(activity, null, Key);
    return convertStringToArray(ItemsList);
}

private static String getStringsFromPreferences(Activity activity, String nick, String key){
    SharedPreferences sharedPreferences = activity.getPreferences(Activity.MODE_PRIVATE);
    String temp = sharedPreferences.getString(key, "");
    return temp;
}

private static String[] convertStringToArray(String str){
 String[] arr = str.split(",");
    return arr;
}

public void sendMessage(View view){
    Intent intent = new Intent(this,Main2Activity.class);
    startActivity(intent);
}

public class CustomObject {

    private String Item;
    private String FamilyMem;

    public CustomObject(String prop1, String prop2) {
        this.Item = prop1;
        this.FamilyMem = prop2;
    }

    public String getItem1() {
        return Item ;
    }

    public String getFamilyMem() {
        return FamilyMem;
    }
};


class ChristmasAdapter extends ArrayAdapter <CustomObject>{

    private LayoutInflater layoutinflater;

    public ChristmasAdapter(Context context, int id, int itemId,
                            int famId,
                            ArrayList<CustomObject> objects){
        super(context, id, objects);
        layoutinflater = LayoutInflater.from(context);

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        View view = layoutinflater.inflate(R.layout.dual_list_item, null);
        CustomObject newItem = getItem(position);

        TextView Item =(TextView) view.findViewById(R.id.item_name);
        TextView Fam = (TextView) view.findViewById(R.id.family_member);

        Item.setText(newItem.getItem1());
        Fam.setText(newItem.getFamilyMem());


        return super.getView(position,convertView,parent);
    }

}

例外是:

java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
        at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:386)

Caused by: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView

【问题讨论】:

  • 对任何格式错误表示抱歉。这是我的第一篇文章。
  • 发布您的 xml 以及完整的 logcat

标签: java android android-layout listview android-listview


【解决方案1】:
return view;

而不是

return super.getView(position,convertView,parent);

视图本身有 2 个项目(双重项目)要显示在资源中,但 super.getView 只返回一个。换句话说,资源要显示的项目数应该等于视图中要显示的项目数。

【讨论】:

  • 扩展说明为什么这会有所帮助/有效
  • @Holloway 因为视图本身应该在资源中显示 2 个项目(双重项目)......换句话说,资源要显示的项目数应该等于项目数在要显示的视图中
  • @msoliman 谢谢,我已将其添加到答案中,请检查编辑。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-10-15
  • 1970-01-01
  • 2011-10-12
  • 2012-03-06
  • 1970-01-01
相关资源
最近更新 更多