Reputation: 13747
I have a custom adapter and its "getView" is not being called. Thats my main activity:
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);
}
}
thats my adapter:
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;
}}
and thats the Class that is creating the list:
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--;
}
Why is it that the getView is not being called? Thanks!
Upvotes: 0
Views: 4068
Reputation: 456
/** * Adapter to show patient and doctor healthTips */ public class HealthTipsAdapter extends ArrayAdapter implements 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;
}
Upvotes: 0
Reputation: 29199
You havent add the mainRelativeLayout to the contentview, of the activity. you should use, findViewById(), instead, applying an extra LayoutInflater. Using LayoutInflater.inflate() method will result an extra inflating of layout, and it will create a new view, who has not been added to the list view yet. so Change your code to following:
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);
}
}
and ListAddr class to the below code
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--;
}
Upvotes: 1
Reputation: 4297
You also have to overwrite the getItem(int position)
method in your ArrayAdapter
.
@Override
public String getItem(int position) {
return values[position];
}
Upvotes: 0