Android自定义适配器 - ArrayAdapter要求资源ID为Textview错误

时间:2014-06-01 02:46:36

标签: android android-listview android-adapter

我创建了一个自定义适配器,以便为我的列表项目提供标题,描述和图像,但是它一直在抛出这个:

java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView

我明白为什么如果我使用标准的Android适配器会抛出它,但我想通过制作自己的适配器你几乎可以避免这种情况。我有什么遗失的吗?

这是我的适配器类:

package homepage;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

public class Adapter_Homepage extends BaseAdapter {
    private final Context context;
    private final ArrayList<String> titles;
    private final ArrayList<String> descriptions;

    static class ViewHolder {
        public TextView title, description;
        public ImageView image;
    }

    public Adapter_Homepage(Context context, ArrayList<String> titles, ArrayList<String> descriptions) {
        this.context = context;
        this.titles = titles;
        this.descriptions = descriptions;
    }


    public String getItemTitle(int position) {
        return titles.get(position);
    }

    public String getItemDescription(int position) {
        return descriptions.get(position);
    }

    @Override
    public int getCount() {
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View rowView = convertView;
        ViewHolder holder;

        if (rowView == null) {
            LayoutInflater inflater = (LayoutInflater) context.getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            rowView = inflater.inflate(R.layout.home_activity_item, null);
            // configure view holder
            holder = new ViewHolder();
            holder.title = (TextView) rowView.findViewById(R.id.firstLine);
            holder.description = (TextView) rowView.findViewById(R.id.secondLine);
            holder.image = (ImageView) rowView.findViewById(R.id.icon);
            rowView.setTag(holder);
        }
        else {
            holder = (ViewHolder) rowView.getTag();
        }

        holder.title.setText(getItemTitle(position));
        holder.description.setText(getItemDescription(position));
        holder.image.setImageDrawable(context.getResources().getDrawable(R.drawable.icon));
        return rowView;
    }
}

这是我的活动课程:

public class Activity_Homepage extends Activity {

private ListView listview;
private Adapter_Homepage adapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.home_activity_listview);

    listview = (ListView) findViewById(R.id.listview);

    ArrayList<String> titles = new ArrayList<String>();
    titles.add("Aaron Rodgers");
    titles.add("Jordy Nelson");

    ArrayList<String> descriptions = new ArrayList<String>();
    descriptions.add("Quarterback");
    descriptions.add("Wide Receiver");

    adapter = new Adapter_Homepage(this, titles, descriptions);
    listview.setAdapter(adapter);
    listview.setOnItemClickListener(global_OnItemClickListener);


}

final AdapterView.OnItemClickListener global_OnItemClickListener = new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }
};

}

1 个答案:

答案 0 :(得分:1)

您似乎使用的是ArrayAdapter而不是自定义的实施,而您没有提供id的{​​{1}}。

此外,您需要确保在TextView方法上返回正确的值。在[{1}}上返回BaseAdapter,在0上返回getCount()将导致您的应用崩溃。