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;
}
Related
I'm making an app that uses a listview that has section headers and content. For each header I want to use an image but the view is setting it's height to the image's and not the value that I set the height at. This is in my adapter:
#Override public View getView(int position, View convertView, ViewGroup parent) {
TextView view = (TextView) super.getView(position, convertView, parent);
view.setTextColor(Color.DKGRAY);
Item item = getItem(position);
if (item.type == Item.SECTION) {
if (position == 0) {
view.setBackgroundResource(R.drawable.myImage);
view.setHeight(400);
view.setMaxHeight(400);
}
else {
//deal with other views
}
}
return view;
}
I'm testing this by just setting the first item in the list as a photo, but as I said, the view isn't using 400px as the height. If I set the height the same way in the else block and just set the background as a color it works fine. I should note that I want to avoid just scaling the picture manually to fit because eventually I want the view to just show a frame of the full image and add a scrolling effect.
I'm not quite sure what you are asking. Do you want it only to only show 400px if there is an image and smaller if there is not? With the code you have does the image show?
I immediately want to rewrite your code like this:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = new TextView(mContext); // where mContext = context; appears in the adapter constructor
convertView.setTextColor(Color.DKGRAY);
Item item = getItem(position);
if (item.type == Item.SECTION) {
if (position == 0) {
convertView.setBackgroundResource(R.drawable.myImage);
convertView.setHeight(400);
convertView.setMaxHeight(400);
//need something like convertView.setText((String) item); to get text to show
}
else {
//deal with other views
}
}
return convertView;
}
I'd recommend using xml to define the layout for the row. Makes it far easier to customise the look of the row.
I have a question.
how to make highlight selection of row in List View by different colors? but I want to do it correct with list selectors.
Please provide some simple for few colors
thank you
You may set up colors in your Adapter.getView() method:
public View getView(int position, View convertView, ViewGroup parent) {
View result = ...
// check convertView, inflate new layout, bla-bla-bla
if (position % 2 == 0) {
result.setBackgroundResource(R.drawable.even);
} else {
result.setBackgroundResource(R.drawable.odd);
}
return result;
}
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
i am trying to show the contents using a list activity in android.
I am getting a list from backend. now i want to fetch each list value and see whether that item is shown in red color or in green color. this value will also be set in customVO.
I tried refering to few article on internet like this. but here they are using List only but i need VO so that i can fetch the value to decide whether that menu item should be red or green.
P.S i am beginner in android so excuse me if my question seems to be bit stupid.
thanks in advance :)
Just override the getView method to set the background:
final ArrayAdapter<MyClass> adapter = new ArrayAdapter<MyClass>(
getActivity(),
R.layout.list_item,
myArray) {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = super.getView(position, convertView, parent);
/* Set values of TextViews here */
MyClass currentItem = getItem(position);
if (currentItem.getColor() == MyVoClass.GREEN) {
convertView.setBackgroundColor(0x0000FF00);
} else if (currentItem.getColor() == MyVoClass.RED) {
convertView.setBackgroundColor(0x00FF0000);
} else {
convertView.setBackgroundColor(0x00FFFFFF);
}
return convertView;
}
};
I would like to have a listView that the first list item will have a red background and the second will have black.Is that possible?And if yes,how will i create the custom list adapter?
thanks!!
|Black item|
|Red item|
|Black item|
|Red item|
|Black item|
etc.
You should Override getView in your arrayadapter. One of the parameters passed into this method is a position. So you can just do the position % 2 to determine if the row is even or odd. Depending on what you want to do you can change you can inflate two totally different layouts there.
When you have the public View getView(int position, View convertView, ViewGroup parent) method make it apply different style for each position %2 == 0. This way you can easily make those items differ from each other :)
I hope this helped.
here's the getView from my latest project's adapter. I've simplified it to highlight a couple of things: 1. that you can use whatever criteria you like to decide what kind of view will be returned, and 2. that you can use LayoutInflater.inflate to get any kind of view at all, whatever the case may be.
public View getView(final int position, View convertView, ViewGroup parent)
{
View v;
int n = itemList.get(position);
if (n < 0)
{
v = inflator.inflate(R.layout.layout1, null);
}
else if (n > 0)
{
v = inflator.inflate(R.layout.layout2, null);
}
else
v = inflator.inflate(R.layout.layout3, null);
return v;
}