Display One more Image on top of that clicked grid item - android

I have grid view with 4 items,now onlclick of each grid item i need to display one more image on top of that clicked grid item.
How to acheive this?
This is my adapter class get View:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder view;
LayoutInflater inflator = activity.getLayoutInflater();
GridView.LayoutParams params = null;
if (convertView == null) {
view = new ViewHolder();
convertView = inflator.inflate(R.layout.tile_row, null);
view.textView = (TextView) convertView.findViewById(R.id.title);
view.imageView = (ImageView) convertView.findViewById(R.id.icons);
convertView.setTag(view);
params = new GridView.LayoutParams(Width, Height);
convertView.setLayoutParams(params);
} else {
view = (ViewHolder) convertView.getTag();
}
// set image view parameters
setImageParamaeters(view.imageView);
view.textView.setText(adapterNames.get(position));
view.imageView.setImageResource(adapterIcondIds.get(position));
if (params != null) {
convertView.setLayoutParams(params);
}
convertView.setId(position);
convertView.setOnClickListener(onClickListeners.get(position));
return convertView;
}

You can do this by positioning a popup window on top of the grid item.
In order to find the exact on screen position of the grid Item you can use the following code
Rect loc = new Rect();
int[] location = new int[2];
view.getLocationOnScreen(location);
loc.left = location[0];
loc.top = location[1];
loc.right = loc.left + view.getWidth();
loc.bottom = loc.top + view.getHeight();
The variable loc holds the exact on screen location of the view that was clicked, this is nothing but the 2nd paramter of the method
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
}
With this information you can easily achieve what you need. Best is to calculate the center of the view and align the center of the popup to the center of the clicked view.
Note:You will need adjustments for showing the pop up in case part of the view is visible.
Edited:
Since i did not get your question, i am adding an image to bring more clarity.
I have already provided the code for case 1. For case 2 you will need a modified layout where you can show the overlay images on click of the grid item
Edited: Implementing case 2
Modified adapter code, moved around some variables for optimization
public CustomAdapter(Activity activity, ...){
inflator = activity.getLayoutInflater();
...
}
LayoutInflater inflator;
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder view;
if (convertView == null) {
view = new ViewHolder();
convertView = inflator.inflate(R.layout.tile_row, null);
view.textView = (TextView) convertView.findViewById(R.id.title);
view.imageView = (ImageView) convertView.findViewById(R.id.icons);
view.clickedImage = (ImageView) convertView.findViewById(R.id.clickedImage);
convertView.setTag(view);
} else {
view = (ViewHolder) convertView.getTag();
}
// set image view parameters
view.textView.setText(adapterNames.get(position));
view.imageView.setImageResource(adapterIcondIds.get(position));
//add a int variable to the view holder
view.position = position;
if(position != clickedPosition){
view.clickedImage.setVisibility(View.GONE);
}
convertView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
ViewHolder holder = (ViewHolder)v.getTag();
clickedPosition = holder.position;
holder.clickedImage.setVisibility(View.VISIBLE);
//you can also set the resource of the imageview here.
notifyDataSetChanged();
}
});
return convertView;
}
//this is used for resetting the previous view when a new view is clicked
int clickedPosition = -1;
To preserve the clicked state of an item in the grid view, i store the
clicked item position of the grid. Now in onClick i change visibility
of the clicked image(which was in GONE state), update the variable
clickedPosition and simply call notifyDataSetChanged. This tells that
the adapter that the dataset has changed and getView for all the
visible grid items will be called. So, if you click item 1 initially,
and then click item 3 then you will notice that item 1 has been reset
when item 3 was clicked.
XML title_row.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="200dp"
android:layout_height="200dp" >
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/droid_logo_black"
android:visibility="gone" />
<ImageView
android:id="#+id/clickedImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/login_help_logo" />
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Title"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>

In the onClickListener of the element, call the imageView and load the image dynamically.
Or predefine the image resource, and call the Imageview with the id of the new image.

onlclick of each grid item i need to display one more image on top of that clicked grid item.
I think you have to create custom Gird View. and append ImageView Dynamically as per the needs.
setVisibilty(View.Visible) when you want to show and setVisibilty(View.Hide) when you don't want to show.

int id = getResources().getIdentifier("yourpackagename:drawable/" + StringGenerated, null, null);
Try something of this sort and you'll get the id. Try changing the image using
imageview.setImageResource(id);
Good Luck!

Related

Android ListView actions repeat in different items

I have a custom layout for a Listview, each row item contains a button that when clicked it shows a small imageview in the item, however the actions i perform in one item, repeats for another item down the list, for example, if i click the button in item 1, the imageview will show up in item 1 and item 10, if i click the button in item 2, the Imageview will show up on item 2 and item 11 and while i scroll it will keep repeating in different items, heres the code for my custom adapter:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
mparent = parent;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.places_item, parent, false);
holder = new ViewHolder();
holder.placeimage = (CircularImageView) convertView.findViewById(R.id.locationimage_pilayout);
holder.addbtn = (TextView) convertView.findViewById(R.id.addbtn_pilayout);
holder.delbtn = (TextView) convertView.findViewById(R.id.delbtn_pilayout);
holder.oribtn = (TextView) convertView.findViewById(R.id.oribtn_pilayout);
holder.placename = (TextView) convertView.findViewById(R.id.locationname_pilayout);
holder.selected = (ImageView) convertView.findViewById(R.id.selected_pilayout);
holder.origin = (ImageView) convertView.findViewById(R.id.origin_pilayout);
holder.swipeLayout = (SwipeRevealLayout) convertView.findViewById(R.id.swipe_pilayout);
holder.mainLayout = (LinearLayout) convertView.findViewById(R.id.main_pilayout);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
final Place item = getItem(position);
/*
my code assigning the button click listener and assigning the views
*/
return convertView;
}
Am i missing something? im sure this could be a simple fix, but i havent found it yet. any help would be kindly appreciated.
In ListView the individual views for rows, get reused. For example, let's say the window can show up to 10 rows inside the ListView. Now when you scroll down, the 1st view gets out of the window from the top, and a new 11th view comes into the window from the bottom. In order to save memory and CPU power, Android uses the same 1st view for the 11th view. That's the purpose of convertView and the if (convertView == null) {} else {} code.
So the reason why the image is being shown in the 1st item and also in the 11th item, is that they are exactly one view object. To tackle this issue, in the getView() method, you need to reset every attribute of every view and don't rely on the defaults.
So adding a line like the one below, will get rid of the mentioned problem:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// all your code ...
holder.placeImage.setImageResource(0); //<-- This clears any previously set image.
return convertView;
}

Why do the ListView repeat every 3rd item?

When i click in the first row the layout in rows 1 , 4 , 7 , 10 are visible.
When i click in second row the layout in rows 2 , 5 , 8 , 11 are visible
This is Update For getView i used the holder and (tag like id)
private class ViewHolder {
private LinearLayout[] myLay , myDesLay;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
final ViewHolder holder ;
if(row == null){
row = inflater.inflate(R.layout.list_items, parent , false);
holder = new ViewHolder();
holder.myLay = new LinearLayout[values.length];
holder.myDesLay = new LinearLayout[values.length];
for(int i = 0; i < holder.myLay.length;i++){
holder.myLay[i] = (LinearLayout) row.findViewById(R.id.post_background);
holder.myDesLay[i] = (LinearLayout) row.findViewById(R.id.post_des_layout);
}
row.setTag(holder);
}else{
holder = (ViewHolder) row.getTag();
}
holder.myDesLay[position].setTag(values[position]);
holder.myLay[position].setTag(values[position]);
holder.myLay[position].setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String myTag = String.valueOf(v.getTag());
String myTag2 = String.valueOf(holder.myDesLay[position].getTag());
Toast.makeText(context, myTag2, Toast.LENGTH_LONG).show();
if((clicked == false) && myTag2.equals(myTag) ){
holder.myDesLay[position].setVisibility(View.VISIBLE);
clicked = true;
}else{
holder.myDesLay[position].setVisibility(View.GONE);
clicked = false;
}
}
});
return row;}
Update
I See at a video from google which talking about ListView And They Said The Problem may be
in List Height And should Make it to WrapContent And i did this but it still reapeated :(
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</ListView>
The change to visibility will effect the listItem at that point every time you scroll down. So if it's the second item in the current visible list for every set of items you scroll through the second item will be affected.
I would assign a unique ID to each list item then in getView set the visibility to false for all items that do not match that ID. So have fields and set them like:
selectedID = currentID;
in onClick(), or have selectedID as an array if you need to store multiple clicked list items.
Then in getView():
if(currentID==selectedID)
holder.desLayout.setVisibility(View.VISIBLE);
else
holder.desLayout.setVisibility(View.GONE);
Add this to your original code so that the visibility is still altered in onClick()
Make unique id to every list item "Post" you can insert items to arraylist and access them using the index not the position :)

Cannot change the visiblity of an ImageView

I have a ListView using a custom cursoradapter to fill the ListView.
The row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<LinearLayout
android:orientation="vertical"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="fill_parent">
<TextView
android:id="#+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:gravity="center_vertical"
android:ellipsize="marquee"
android:textSize="24dp" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/artist"
android:singleLine="true"
android:ellipsize="marquee"
android:textSize="14dp" />
</LinearLayout>
<ImageView
android:id="#+id/currentplaying"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginLeft="1dip"
android:src="#android:drawable/ic_media_play"
android:contentDescription="#string/now_playing"
android:visibility="gone" />
</LinearLayout>
As you can see, the ImageView's visibility is gone. I want to make it
visible for one particular row. Here is the code I tried but it is not
working...
View view = getListView().getAdapter().getView(0, null, null);
ImageView iv = (ImageView)view.findViewById(R.id.currentplaying);
iv.setVisibility(ImageView.VISIBLE);
Thanks in advance.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView==null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.yourlayout, null);
holder.imgViewLogo = (ImageView) convertView.findViewById(R.id.imgViewLogo);
convertView.setTag(holder);
}
else {
holder=(ViewHolder)convertView.getTag();
}
if(position==0) {
holder.imgViewLogo.setVisiblity(View.VISIBLE);
}
return convertView;
}
EDIT:
I got it working. I used this to start the ListView activity.
intent.putExtra("id", c.getInt(c.getColumnIndex(DatabaseHelper._ID)));
startActivity(intent);
In the ListView activity,
currentplayingid = getIntent().getExtras().getInt("id");
Then I added this in bindview()
ImageView imgview = (ImageView)view.findViewById(R.id.currentplaying);
int id = c.getInt(c.getColumnIndex(DatabaseHelper._ID));
if (id == SongsListActivity.this.currentplayingid)
imgview.setVisibility(View.VISIBLE);
else
imgview.setVisibility(View.GONE);
I got it working. I used this to start the list view activity.
intent.putExtra("id", c.getInt(c.getColumnIndex(DatabaseHelper._ID)));
startActivity(intent);
In the listview activity,
currentplayingid = getIntent().getExtras().getInt("id");
Then i added this in bindview()
ImageView imgview = (ImageView)view.findViewById(R.id.currentplaying);
int id = c.getInt(c.getColumnIndex(DatabaseHelper._ID));
if ( id == SongsListActivity.this.currentplayingid )
imgview.setVisibility(View.VISIBLE);
else
imgview.setVisibility(View.GONE);
perhaps you should do it in getView() of your adapter
EDIT:
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
// codes...
if (position == 0)
{
holder.imgViewLogo.setVisibility(ImageView.VISIBLE);
}
else
{
holder.imgViewLogo.setVisibility(ImageView.GONE);
}
// codes...
}
You have to do it like this
iv.setVisibility(View.VISIBLE);
If it is working some time then may be i can help you. What happen that whenever you move you listview it recreates all views again in this case it never save the last state of view. So what you need to do is to save state of your each imageview and in getView() you have to set accordingly.I am posting one of my answer it may help you.
Here is a little code for your help: I will create a boolean arraylist.
private ArrayList imageview_visible = null;
Then I will set states of all imageview as false in my constructor:
for (int i=0; i < no_of_elements.size(); i++) {
imageview_visible.add(i, false);
}
In your getView write this code:
public View getView (int position, View convertView, ViewGroup parent)
{
//WRITE YOUR CODE
if (imageview_visible.get(position) == true)
{
//SET YOUR IMAGE VIEW AS VISIBLE
} else {
// SET IMAGEVIEW AS GONE
}
}
Whenever you unhide or hide your view just save it into imageview_visible.set(true or false) this will save state of you all imageview and set every image view accordingly
Use LayoutInflater to get view object
LayoutInflater inflater = this.getLayoutInflater();
View rowView = inflater.inflate(R.layout.row, null, true);
ImageView iv = (ImageView)view.findViewById(R.id.currentplaying);
iv.setVisibility(ImageView.VISIBLE);
Try the following code as follows,
private class ViewHolder
{
ImageView imgViewLogo;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder;
if(convertView==null)
{
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.yourlayout, null);
holder.imgViewLogo = (ImageView) convertView.findViewById(R.id.imgViewLogo);
convertView.setTag(holder);
}
else
{
holder=(ViewHolder)convertView.getTag();
}
if(position==0)
{
holder.imgViewLogo.setVisiblity(View.VISIBLE);
}
return convertView;
}
It Works for me...It may help you.
I ran into similar problems where several widgets would appear for some rows but not for others. The problems were due to view recycling. I'm not exactly sure if that's your issue here, but you should handle it anyway. The trick is to set visibility for every row; instead of just for the row that you want to appear/disappear.
So:
if (position == 0)
{
iv.setVisibility(ImageView.VISIBLE);
}
else
{
iv.setVisibility(ImageView.GONE);
}
Otherwise, you're assuming that for positions other than 0 the visibility is GONE but that might not be the case with view recycling. I do this work in bindView, by the way. Not sure if that's technically correct.
I have the same issue... i solved with a non standar solution, but worked for me...
v.setImageResource(R.color.transparent);
importing R from android
import android.R;
Both iv.setVisibility(View.VISIBLE); and iv.setVisibility(ImageView.VISIBLE); are correcte but It's better to use View instead of ImageView because VISIBLE & GONE are defined in View class.
You most change Both Visibility (VISIBLE or GONE) in that if. like:
if(?)
iv.setVisibility(View.VISIBLE);
else iv.setVisibility(View.GONE);
You can hide or show views using setVisibility(int) .
use iv.setVisibility(View.VISIBLE);

OnItemClickListener Not Triggered on Android GridView

I have a Gridview filled by an Adapter which returns LinearLayouts each contains an ImageButton and TextView.
In the adapter I am binding an onClick and onLongClick event to the ImageButton.
I am trying to bind OnItemClickListener to the gridview but I don't know why that the onItemclicked never fired up.
It's my 6th hour without anything.
By the way;
OnItemSelectListener working perfectly on the Grid.
I am checking if some piece of code accidentally handles the onItemClicked but couldn't catch yet.
I need help guys.
gridView = (GridView) layoutInflater.inflate(R.layout.gridview, null);
gridView.setOnItemClickListener(new ItemClickListener());
.
.
.
//inner handler class
class ItemClickListener implements AdapterView.OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(mainActivity.getApplicationContext(),view + " clicked at pos " +
i,Toast.LENGTH_SHORT).show();
}
}
Do not use clickable objects in the grid. In that case Android cannot handle the click event of GridView.
Instead, use something to show a similar user interface view. Then handle that object's click actions.
Don't: put Button in the GridView to perform some click actions.
Do: put an ImageView instead of ImageButton and handle ImageView's click events.
If you wants to use Button or ImageButton then you need to write these attributes in your xml code of the widgets.
android:focusable="false"
android:focusableInTouchMode="false"
Its works for me.
But in GridView, Try to avoid use of these widgets. You can use any other widgets in place of these (Like ImageView or any other).
Also make sure, that your ListAdpter returns true for
public boolean isEnabled(int _position)
for the position you want to click.
Hey guyz finally got a solution...
what we were doing is directly accessing the Layout inside the GridView, so the onItemClickListener finds it confusing to access the item.
So the solution is to apply the onClickListener inside the Adapter (i.e. normally ArrayAdapter)
so what i m trying to say is:
public View getView(int position, View convertView, ViewGroup parent) {
//Here row is a view and we can set OnClickListener on this
final View row;
ViewHolder holder = null;
if (convertView == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
//Here we inflate the layout to view (linear in my case)
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ViewHolder();
holder.imageTitle = (TextView) row.findViewById(R.id.text);
holder.image = (ImageView) row.findViewById(R.id.image);
row.setTag(holder);
} else {
row = convertView;
holder = (ViewHolder) row.getTag();
}
ImageItem item = data.get(position);
holder.imageTitle.setText(item.getTitle());
holder.image.setImageBitmap(item.getImage());
//Now get the id or whatever needed
row.setId(position);
// Now set the onClickListener
row.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(context, "Clicked" + row.getId() + "!!",
Toast.LENGTH_SHORT).show();
}
});
return row;
}
Try to set
android:clickable="false"
android:focusable="false"
I meet same problem too, because of several reasons.
So, here's my tips:
Extend BaseAdapter for your adapter;
Use OnClickListener inside the getView in adapter instead setting OnItemClickListener for GridView;
Avoid setting LayoutParams multiple times;
Check if position = 0, don't use convertView, inflate new View;
Set OnClickListener not only for parent View, but for any child View, if any;
Make all your Views clickable.
I just tested it on 4 devices, and this solution works as expected. Hope, it will help in your case.
Correct me, if I made something wrong.
Layout code XML:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#273238"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:padding="1dp">
<ImageView
android:id="#+id/open_image_item_imageview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/loh"
android:clickable="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:scaleType="centerCrop"/>
<TextView
android:id="#+id/open_image_item_textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_gravity="bottom"
android:padding="4dp"
android:textSize="10sp"
android:ellipsize="start"
android:background="#55000000"
android:textColor="#FFFFFF"
android:text="image name"/>
</FrameLayout>
Adapter code Java:
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = null;
if(convertView != null && position != 0)
view = convertView;
else{
view = LayoutInflater.from(getContext()).inflate(R.layout.open_image_item_layout, null, false);
view.setLayoutParams(new GridView.LayoutParams(GridView.AUTO_FIT, size));
}
TextView textView = (TextView)view.findViewById(R.id.open_image_item_textview);
ImageView imageView = (ImageView)view.findViewById(R.id.open_image_item_imageview);
...
View.OnClickListener onClickListener = getOnClickListener(files[position]);
view.setOnClickListener(onClickListener);
textView.setOnClickListener(onClickListener);
imageView.setOnClickListener(onClickListener);
return view;
}

nullPointer when findViewById() in SimpleCursorAdapter

I was using SimpleCursorAdapter with an xml file with some views defined in it:
<LinearLayout ...>
<ImageView android:id="#+id/listIcon" />
<TextView android:id="#+id/listText" />
</LinearLayout>
My aim was to set the text color of the TextView, and the background color of the LinearLayout (that is, each row in the ListView) programmatically; the color is returned from a database.
I was getting NPEs when trying to manipulate the TextView for example, after it had found it with no complaints:
TextView tv = (TextView) findViewById(R.id.listText);
tv.setTextColor(color); // NPE on this line
Which is fair; if there's multiple entries in the list, it's reasonable to assume that "R.id.listText" will not work. So I extended SimpleCursor Adapter:
public View getView(int position, View convertView, ViewGroup parent) {
View row = super.getView(position, convertView, parent);
TextView text = (TextView) row.findViewById(R.id.listText);
// ImageView icon = (ImageView) row.findViewById(R.id.listIcon);
// If there's an icon defined
if (mIcon_id != 0) {
// icon.setImageResource(mIcon_id);
}
// If text color defined
if (mTextColor != 0) {
text.setTextColor(mTextColor);
}
// If background color set
if (mBackgroundColor != 0) {
row.setBackgroundColor(mBackgroundColor);
}
return(row);
}
And I get two different errors:
A similar NPE is thrown at
"text.setTextColor(mTextColor)"
If the lines with the ImageView are
uncommented, I get a
"ClassCastException:
android.widget.TextView" where I am
calling
"row.findViewById(R.id.listIcon)"
For reference, I was trying to use Commonsware's sample code, applying it to my situation. link (pdf)
Changed to this:
public View getView(int position, View convertView, ViewGroup parent) {
convertView = super.getView(position, convertView, parent);
if (convertView == null) convertView = View.inflate(mContext, R.layout.theme_item, null);
TextView text = (TextView) convertView.findViewById(R.id.listText_tv);
ImageView icon = (ImageView) convertView.findViewById(R.id.listIcon_iv);
// If there's an icon defined
if (mIcon_id != 0) {
icon.setImageResource(mIcon_id);
}
// If text color defined
if (mTextColor != 0) {
text.setTextColor(mTextColor);
}
// If background color set
if (mBackgroundColor != 0) {
convertView.setBackgroundColor(mBackgroundColor);
}
bindView(convertView, mContext, mCursor);
return(convertView);
}
Now I get a ClassCastException in the next activity (on list item click). Nothing has been modified in the next activity; it worked when using a SimpleListAdapter for the list which had entries (upon which clicking would lead to Activity2), so I think it's still something I'm doing wrong in the this extended class.
It's not true that convertView will always be an existing instance; you should check if it's null and then instantiate it. If not, you can change it just as you did.
This should be like:
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null)
convertView = //inflate your row here
View row = convertView;
//Manipulate the row here
return(row);
}
I would modify the getView method:
public View getView(int position, View convertView, ViewGroup parent) {
convertView = View.inflate(getContext(), R.layout.myLayout, null);
TextView text = (TextView) convertView.findViewById(R.id.listText);
ImageView icon = (ImageView) convertView.findViewById(R.id.listIcon);
// If there's an icon defined
if (mIcon_id != 0) {
icon.setImageResource(mIcon_id);
}
// If text color defined
if (mTextColor != 0) {
text.setTextColor(mTextColor);
}
// If background color set
if (mBackgroundColor != 0) {
convertView.setBackgroundColor(mBackgroundColor);
}
return convertView;
}
I think that you're getting NPE because you're trying to create a textview and an imageview in a view where they aren't there.
When you want inflate a ListView with entries from a database, in your activity you define main.xml whith a ListView:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listView1">
</ListView>
and in the onCreate method you set the view to this xml with setContentView(R.layout.main);. Then you create your cursor to your database and your custom adapter:
MySimpleCursorAdapter adapter = new MySimpleCursorAdapter(this, R.layout.entry,
names, new String[] {Phones.NAME, Phones.NUMBER}, new int[] {
R.id.listIcon, R.id.listText});
startManagingCursor(cursor);
ListView listView = (ListView) findViewById(R.id.listView1);
listView.setAdapter(adapter);
and you define an entry.xml with your listIcon and listText, where the adapter points. In my example, I'm querying the names and numbers from contact list.
In your custom adapter, you should access to your textview and imageview inside getView or bindView without any problem.
Here you have and example to get all the contacts in your contact list with its picture, name and number, but using ListActivity instead of activity, and only one xml with two text views and an imageview. If you use ListActivity you don't need to use a ListView and you don't need to set the content view in the activity.
I hope it helps!
Don't forget to put : layout_width and layout_heigth for each of your views .

Categories

Resources