Can't integrate ImageButton with ListActivity

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Can't integrate ImageButton with ListActivity

Postby qmei » Sat Jan 30, 2010 9:22 pm

Hi,

I have a working ListActivity class. Each of the list item consists of
an ImageView and a TextView (I am using Android SDK 1.6).

However, when I try to replace the ImageView with ImageButton, The
list becomes unselectable. The onListItemClick or any other function
no longer get called when press a list item (Although the track ball
can still focus a list item, but nothing more can be done).

I have searched the forum and also the internet and failed to find any
such working example, apart from one guy reporting a similar problem
but with no answer.

The following is my code:

1. The list_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/
android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight">

<ImageButton
android:id="@+id/refresh"

android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:src="@drawable/refresh" />
<TextView
android:id="@+id/title"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_toRightOf="@id/refresh"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:singleLine="true"/>
</RelativeLayout>

2. the Main ListActivity class:

import android.app.Dialog;
import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class TestListActivity extends ListActivity implements
Constants {


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new EfficientAdapter(this));
}

@Override
public void onStart(){
super.onStart();
getListView().setItemsCanFocus(true); // seems no use!
}

@Override
protected void onListItemClick(ListView l, View v, int index, long
id) {
Log.i("", "onListitemClick");
}

public boolean onContextItemSelected(MenuItem item) {
Log.i("", "onContextItemSelected");
return false;
}

private class EfficientAdapter extends BaseAdapter implements
Constants {
private LayoutInflater layoutInflater;

Context ctx;
public EfficientAdapter(Context context) {
// Cache the LayoutInflate to avoid asking for a new one each time.
layoutInflater = LayoutInflater.from(context);
ctx = context;
}


@Override
public int getCount() {
Log.i("", "getCount");
return SIZE;
}

@Override
public Object getItem(int position) {
Log.i("", "getItem");
return "TBD"; }

@Override
public long getItemId(int positin) {
Log.i("", "getItemId");
return positin;
}

public void update(int position) {
notifyDataSetChanged(); // triggers the view data to be refreshed!
}

/**
* Make a view to hold each list item
*/
@Override
public View getView(int position, View convertView, ViewGroup
parent) {
Log.i("", "getView");
ViewHolder holder;
if (convertView == null) {
convertView = this.layoutInflater.inflate(R.layout.list_item,
parent, false);

holder = new ViewHolder();
holder.title = (TextView) convertView.findViewById(R.id.title);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}

holder.title.setText(R.string.title);
parent.focusableViewAvailable(convertView); // originally without
this. Added for testing, still not working!
return convertView;
}

private class ViewHolder {
//ImageButton imageButton;
TextView title;
}
}
}

Is there any way to make ListActivity working with ImageButton or this
actually can not be achieved (iPhone can this)?

Any help would be most appreciated.
qmei
Freshman
Freshman
 
Posts: 2
Joined: Sat Jan 30, 2010 8:32 pm
Location: london

Top

Postby qmei » Mon Feb 01, 2010 1:42 pm

Problem solved by simply adding the following into the code:

holder.imageButton.setFocusable(false);
qmei
Freshman
Freshman
 
Posts: 2
Joined: Sat Jan 30, 2010 8:32 pm
Location: london

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 6 guests