BaseAdapter GetView With Condition - android

I am stuck at a very minor error ,
I want to place a condition inside the getview of the Baseadapter class.
If the condition is true it should not inflate ,else it should inflate in the listview.
Some help would be really appreciated
This is my code
public View getView(final int position, View convertView, ViewGroup parent) {
View itemView = inflater.inflate(R.layout.slidelistrow, parent, false);
// Get the position
resultp = data.get(position);
if(resultp.get(MainFragment.TAG_PACKAGE).equals(Constants.PACKAGE_NAME))
{
return null;
}else
{
return itemView;
}
}

you are doing it wrong. getView can not return null, otherwise your application will crash. What you have to do is to submit the exact dataset you want show, it means that your data has to contains exactly what you want to show in the ListView

Related

ListView position problem while scrolling

I'm developing an app with a form users will have to fill.
The form has some textview, edittexts, a listview and two buttons.
The listview consist of a textview and two radiobuttons.
I populate the listview with a question and two radiobuttons in every row.
If I reuse the convertview given in overriden getView method, when I check a radiobutton, it will check the first radiobutton visible while scrolling down as per every screen scrolled.
#Override
public View getView(int position, View convertView, ViewGroup parent){
if(convertView == null){
convertView = ((Activity)context).getLayoutInflater().inflate(R.layout.riskrowlayout, parent, false);
}
((TextView)convertView.findViewById(R.id.tvPreg)).setText(data.get(position));
return convertView;
}
Otherwise, if I inflate the layout everytime getView gets called, it will automatically uncheck the radiobutton I checked while scrolling.
#Override
public View getView(int position, View convertView, ViewGroup parent){
convertView = ((Activity)context).getLayoutInflater().inflate(R.layout.riskrowlayout, parent, false);
((TextView)convertView.findViewById(R.id.tvPreg)).setText(data.get(position));
return convertView;
}
What I'm doing wrong? How can I solve this?
If you need more info ask and I'll give you.
Thank you very much in advantage!
Problem from last comment solved, just pasted this code from another post:
#Override
public int getViewTypeCount() {
return getCount();
}
#Override
public int getItemViewType(int position) {
return position;
}
You need to keep your check data into an array. When you scroll down and came back checked row it doesnt know where is checked. If you dont have model array simply create a checked list and set all false inside and inside getview method look at array.
#Override
public View getView(int position, View convertView, ViewGroup parent){
if(convertView == null){
convertView = ((Activity)context).getLayoutInflater().inflate(R.layout.riskrowlayout, parent, false);
}
TextView textView = (TextView) convertView.findViewById(R.id.tvPreg);
textView.setText(data.get(position));
radioButton.setChecked(checked.get(position));
return convertView;
}

Android Listview with different child views

In my application i need to show a list of objects. In the first row of listview contains single object, but the second row should contain two objects from list. A sample image for my requirement is attached here please go through this. if any idea please share here. Thanks .
In your BaseAdapter do this.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if(position%2 != 0) {
view = layoutInflater.inflate(R.layout.view_1, null, true);
// Do action for view 1
}
else{
view = layoutInflater.inflate(R.layout.view_2, null, true);
// Do action for view 2
}
return view;
}

Android how to use different layouts for each listview item

In my application I can only use the same layout for each listview item in each row. This is not what I want for my application and I would like to have a different layout for each of the listview items.
As an example of what I would like to achieve please see the screenshot below.
You can do it via android Listview BaseAdapter. In BaseAdapter getView method, you can added code like this.
public View getView(final int position, View convertView, ViewGroup parent) {
if(item1 or your condition)
{
convertView=inflater.inflate(R.layout.from_right_row.xml, parent, false);
}else if(item2 or your condition) {
convertView=inflater.inflate(R.layout.from_left_row.xml, parent, false);
}
return convertView;
}

List Adapter setting - How can I not to repeat the list item layout resource?

I have made a ListView with an ArrayAdapter. It works. But I had to put the resource id for the item layout twice: in the adapter definition and in the getView, in the case when the View parameter is null.
// --------------------------------------------here is the FIRST use
lvShows.setAdapter(new ArrayAdapter<TvShow>(this, R.layout.show_row, allKnownShows) {
#Override
public View getView(int position, final View rowView, ViewGroup parent) {
LinearLayout showView;
if (rowView == null) {
// --------------------------------- here is the SECOND use
showView = (LinearLayout) inflater.inflate(R.layout.show_row, parent, false);
}
else {
showView = (LinearLayout) rowView;
}
((TextView) showView.getChildAt(0)).setText(time));
((TextView) showView.getChildAt(1)).setText(name);
return showView;
}
});
Such style is disgustful, of course. Could you kindly advise, what am I understanding wrong and how can I use the resource only once?
If creating a new ArrayAdapter I am setting the layout id, it should know it and use somehow. How could I reach it? Or better I would expect the Adapter to create item views automatically. Again - how can I use it?
What ArrayAdapter does with that resource we feed to it when creating a new one? All its constructors take the item resource and we manage this resource and inflate it "by hand". It is not the effective way.
You can override ArrayAdapter constructor and call super :
public ArrayAdapter<T>(Context context, TheDataType data) {
super(context, R.layout.show_row, data);
}
And store the id in a ArrayAdapter member variable. It avoid the adapter "user" to know what is the view needed for the adapter.
Or you can use a BaseAdapter.
I had looked into the source code of the ArrayAdapter, and it already does all this stuff about view creation:
// citation from public class ArrayAdapter<T>
private View createViewFromResource(int position, View convertView, ViewGroup parent,
int resource) {
View view;
TextView text;
if (convertView == null) {
view = mInflater.inflate(resource, parent, false);
} else {
view = convertView;
}
So, we should simply use it:
lvShows.setAdapter(new ArrayAdapter<TvShow>(this,
R.layout.show_row,
R.id.nameField,
allKnownShows){
public View getView(int position, final View convertView, ViewGroup parent) {
LinearLayout showView = (LinearLayout) super.getView(position, convertView, parent);
setAnythingForItem(showView);
return showView;
}
Attention: we have changed the constructor!
ArrayAdapter allows to use no-TextView item layout. But if it is not TextView itself, it should have one and the id of this very inner TextView field should be given to the constructor as the third parameter. ArrayAdapter needs it to be set, to write there Strings if the array connected to it has String elements.
It wants a TextView always, even if it doesn't need it really, if the array consists of Objects, not Strings. Otherway it checks the item layout for being a TextView and if it is not, throws an error.

Android ListView: getTag() returns null

Hallo all,
I have a ListView which contains a Button in each line. The following code is part of the getView() Method
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
TextView tv;
Button saveA_button;
EditText edittext;
FITB_ViewWrapper wrapper;
if (row == null) {
LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (ChooseMode_Act.modeInfo.equalsIgnoreCase("Training")) {
row = li.inflate(R.layout.exercise_for_training_fitb,parent, false);
}else {
row = li.inflate(R.layout.exercise_for_exam_fitb,parent, false);
}
wrapper=new FITB_ViewWrapper(row);
row.setTag(wrapper);
if (ChooseMode_Act.modeInfo.equalsIgnoreCase("Exam")) {
saveA_button=wrapper.getSaveAnswer_Button();
OnClickListener l=new OnClickListener() {
#Override
public void onClick(View v) {
Integer mp=(Integer)v.getTag();
Log.i("mp","my Position is: "+mp);
}
};
saveA_button.setOnClickListener(l);
}
}else {
wrapper=(FITB_ViewWrapper) row.getTag();
}
For my App i need to known to which item the Button belongs to, so i try to detect it. The code
Log.i("mp","my Position is: "+mp);
puts out a message: mp myPosition is: null
I can't understand, why do i get a "null" but not an Integer? How can i find out the Position of an Item in a ListView?
Thanks a lot.
Log.i("mp","my Position is: "+position);
you have the position already !
public View getView(final int position, View convertView, ViewGroup parent) {
The Views in a ListView are reused as you scroll up and down. Thus, setting values in getView often has unintented consequences, like the image that you meant to set for item number 5 appearing in item number 10, 15 and 20 also.
To avoid this, if you want to set properties in getView you need to make sure you set or unset them for each view.
I'm not sure what exactly you are trying to accomplish with your code, but it might help to move the setTag outside of the if statement, to make sure you are setting it each time that a view is used.

Categories

Resources