没有调用ArrayAdapter的getView“

时间:2012-11-21 09:07:43

标签: android listview android-arrayadapter

我有一个自定义适配器及其" getView"没有被召唤。 这是我的主要活动:

public class MainActivity extends Activity {
RelativeLayout mainRelativeLayout;
ListView listViewOne;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    String[] values1 = new String[] { " ", "Android", "iPhone", "WindowsMobile" };
    ListArr listArr = new ListArr(this, 3);
    listArr.AddListToListArr(values1);
}

}

那是我的适配器:

public class MyAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
private TextView textView;
private ImageView icon;

public MyAdapter(Context context, String[] values) {
    super(context, R.layout.first_row, values);
    this.context = context;
    this.values = values;
}

@Override
public int getCount() {
    return values.length;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView;

    rowView = inflater.inflate(R.layout.first_row, parent, false);
    textView = (TextView) rowView.findViewById(R.id.row_text_view);

    icon.setImageDrawable(context.getResources().getDrawable(R.drawable.ic_launcher));
    textView.setText(values[position]);
    return rowView;
}}

那就是创建列表的类:

public class ListArr {
int indexOfEmptyCellInArr;
ListView[] listArr;
Context context;
LinearLayout mainRelativeLayout;

public ListArr(Context context, int numOFLists) {
    this.listArr = new ListView[numOFLists];
    this.indexOfEmptyCellInArr = 0;
    this.context = context;
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = inflater.inflate(R.layout.activity_main, null);
    this.mainRelativeLayout = (LinearLayout) v.findViewById(R.id.main_relative_layout);
}

public void AddListToListArr(String[] stringsOfNewList) {
    ArrayAdapter<String> arrayAdapter = new MyAdapter(context, stringsOfNewList);
    ListView listview = new ListView(context);
    listview.setAdapter(arrayAdapter);
    listArr[indexOfEmptyCellInArr] = listview;
    mainRelativeLayout.addView(listArr[indexOfEmptyCellInArr]);

    indexOfEmptyCellInArr++;
}

public void RemoveLastList(){
    int indexOfListToRemove = indexOfEmptyCellInArr - 1;
    listArr[indexOfListToRemove] = null;
    indexOfEmptyCellInArr--;
}

为什么没有调用getView? 谢谢!

3 个答案:

答案 0 :(得分:1)

您尚未将mainRelativeLayout添加到活动的contentview中。你应该使用findViewById()代替应用额外的LayoutInflater。使用LayoutInflater.inflate()方法将导致布局的额外膨胀,并且它将创建一个尚未添加到列表视图的新视图。所以将代码更改为以下内容:

public class MainActivity extends Activity {
RelativeLayout mainRelativeLayout;
ListView listViewOne;

@Override

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String[] values1 = new String[] { " ", "Android", "iPhone", "WindowsMobile" };
        ListArr listArr = new ListArr(this, this, 3);
        listArr.AddListToListArr(values1);
    }
    }

和ListAddr类到下面的代码

public class ListArr {
int indexOfEmptyCellInArr;
ListView[] listArr;
Context context;
Activity activity;
LinearLayout mainRelativeLayout;

public ListArr(Activity activity, Context context, int numOFLists) {
    this.listArr = new ListView[numOFLists];
    this.indexOfEmptyCellInArr = 0;
    this.context = context;
    this.activity = activity;
    this.mainRelativeLayout = (LinearLayout) activity.findViewById(R.id.main_relative_layout);
}

public void AddListToListArr(String[] stringsOfNewList) {
    ArrayAdapter<String> arrayAdapter = new MyAdapter(context, stringsOfNewList);
    ListView listview = new ListView(context);
    listview.setAdapter(arrayAdapter);
    listArr[indexOfEmptyCellInArr] = listview;
    mainRelativeLayout.addView(listArr[indexOfEmptyCellInArr]);

    indexOfEmptyCellInArr++;
}

public void RemoveLastList(){
    int indexOfListToRemove = indexOfEmptyCellInArr - 1;
    listArr[indexOfListToRemove] = null;
    indexOfEmptyCellInArr--;
}

答案 1 :(得分:0)

您还必须覆盖getItem(int position)中的ArrayAdapter方法。

@Override
public String getItem(int position) {
    return values[position];
}

答案 2 :(得分:0)

/ **      *适配器显示患者和医生的健康提示      * /     公共类HealthTipsAdapter扩展             ArrayAdapter实现了ServiceCallBacks {

    @Override
    public HealthPost getItem(int position) {
        // TODO Auto-generated method stub
        return super.getItem(position);
    }

    @Override
    public int getPosition(HealthPost item) {
        // TODO Auto-generated method stub
        return super.getPosition(item);
    }

    @Override
    public void notifyDataSetChanged() {
        // TODO Auto-generated method stub
        super.notifyDataSetChanged();
    }

    /** Context of application(here DoctorCommentsActivity object) */
    private Context context;
    /** Used to store parentCommentId in order to show doctor reply */
    private String parentCommentId;
    /** Common list used in both patient comment and doctor reply lists */
    private List<HealthPost> commentsList;
    /** List used to store patient comments */
    private List<HealthPost> patientCommentsList;
    /** List used to store doctor replies */
    private List<HealthPost> doctorCommentsList;
    /** boolean list used to decide icon color for plusOneCommentIcon */
    private List<Boolean> isCommentPlusOneList;
    /**
     * boolean hashmap used to store comment id(key) and if it has
     * reply(value)
     */
    private HashMap<String, Boolean> commentIdHasReplyMap;
    /**
     * boolean hashmap used to store comment id(key) and its plus one
     * count(value)
     */
    private HashMap<String, String> commentIdPlusCountMap;

    /** stores comment id of comment to be deleted or plus oned */
    private String commentId;

    /**
     * stores position of list item in case Internet connection is
     * unavailable
     */
    private int retryForPosition = -1;
    /** stores position of list item in case of right doctor is clicked */
    private int positionValue;

    /**
     * differentiates in two lists whether list stores patient comments or
     * doctor replies
     */
    private boolean isReply = false;

    public HealthTipsAdapter(Context context, List<HealthPost> list) {
        super(context, R.layout.list_health_tips,list);
        this.context = context;
        this.commentsList = new ArrayList<HealthPost>();
        Log.v(tag,"construtor-"+list.get(1).getPost());
        this.commentsList.addAll(list);
    }

    @Override
    public int getCount() {
        Log.v(tag,"size"+commentsList.size());
        return commentsList.size();

    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        Log.v(tag,"getview-"+position+"commentlist-"+commentsList.get(1).getPost());
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View rowView = inflater.inflate(R.layout.list_health_tips,
                parent, false);

        final TextView tv_comment_right_count = (TextView) rowView
                .findViewById(R.id.tv_comment_right_count);

        final View rl_comment_delete = rowView
                .findViewById(R.id.rl_comment_delete);
        final TextView textViewUserName = (TextView) rowView
                .findViewById(R.id.textView_UserName);
        final TextView textViewComment = (TextView) rowView
                .findViewById(R.id.txtComment);
        final TextView textViewDate = (TextView) rowView
                .findViewById(R.id.textView_date);
        final TextView textViewTime = (TextView) rowView
                .findViewById(R.id.textView_time);
        final View rl_comment_right = (View) rowView
                .findViewById(R.id.rl_comment_right);
        final ImageView iv_comment_right = (ImageView) rowView
                .findViewById(R.id.iv_comment_right);
        final View rl_comment_offensive = (View) rowView
                .findViewById(R.id.rl_comment_offensive);
        final View rl_doctor_reply = (View) rowView
                .findViewById(R.id.rl_doctor_reply);
        final ImageView iv_doctor_reply_seperator = (ImageView) rowView
                .findViewById(R.id.iv_doctor_reply_seperator);
        Log.v(tag,"getView-"+commentsList.get(1).getPost());
        // Actual comment text to be displayed
        textViewComment.setText("hi"+commentsList.get(position).getPost());


        return rowView;
    }