I have a ListActivity that I'm trying to bind to an array of business objects using a custom ArrayAdapter.
When I run the application on either the emulator or the real android device I get a null reference in the ArrayAdapter on the line "holder.txtTeam1.setText(info.Team1);". After doing some debugging it looks like the call to inflate the layout didn't actually create the child TextViews, it only created the outter TableLayout.
This is all based on a tutorial I found on the internet, and I've been banging my head off the desk for almost a week on this one. Can anyone tell me what I'm doing wrong?
The activity looks like this:
public class BaseballPressActivity extends ListActivity {
private GameDataAdapter adapter;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
adapter = new GameDataAdapter(this, new GameData[]{new GameData("3:10pm","Team 1","Team 2")});
this.setListAdapter(adapter);
}
}
The ArrayAdapter looks like this:
public class GameDataAdapter extends ArrayAdapter<GameData> {
Context context;
GameData data[] = null;
public GameDataAdapter(Context context, GameData[] data) {
super(context,R.layout.listitem_game, R.id.Name1, data);
this.context = context;
this.data = data;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
GameViewHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(R.layout.test , parent, false);
holder = new GameViewHolder();
holder.txtTeam1 = (TextView)row.findViewById(R.id.Name1);
row.setTag(holder);
}
else
{
holder = (GameViewHolder)row.getTag();
}
GameData info = data[position];
holder.txtTeam1.setText(info.Team1);
return row;
}
static class GameViewHolder
{
TextView txtTeam1;
TextView txtTeam2;
TextView txtGameTime;
}
}
And the layout for the list activity item looks like this:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow android:id="#+id/tableRow1" android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView android:text="TextView" android:layout_weight="0" android:id="#+id/textView1" android:layout_height="wrap_content" android:layout_width="0dp"></TextView>
<ImageView android:id="#+id/imageView1" android:src="#drawable/icon" android:layout_height="wrap_content" android:layout_weight="0" android:layout_width="0dp"></ImageView>
<LinearLayout android:id="#+id/linearLayout1" android:orientation="vertical" android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="0dp">
<TextView android:text="TextView" android:id="#+id/Name1" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<TextView android:text="TextView" android:id="#+id/Name2" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</LinearLayout>
<ImageView android:id="#+id/imageView2" android:src="#drawable/icon" android:layout_height="wrap_content" android:layout_weight="0" android:layout_width="0dp"></ImageView>
</TableRow>
</TableLayout>
ok, i think the problem is here
row = inflater.inflate(R.layout.test , parent, false);
change false to true, i think you have to attach the view to it's parent.
if that doesn't work try this.
row = inflater.inflate(R.layout.test , null);
Abandon ArrayLayout for SimpleAdapter or SimpleCursorAdapter. This will allow you to easily bind your data with simple to fairly complex layouts.
Related
My Problem is --> I put a white line as a view in the Fragment item xml but it doesn't appear when i debug it. How can i get the changes i make in the fragment item xml file. Because i tried some little changes like alpha property change but it didnt change is it because of this is a listview and has itself design? Please help me about that problem.
The xml code and adapter code are below.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="vertical"
android:gravity="center"
android:paddingTop="5dp"
>
<ImageView
android:id="#+id/menulogo"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="#drawable/infoicon"
android:layout_gravity="center"
android:paddingTop="5dp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="vertical"
android:gravity="center">
<TextView
android:id="#+id/txtMenu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Company Name"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_weight="1" />
</LinearLayout>
<View
android:id="#+id/line"
android:layout_width="match_parent"
android:layout_height="5dp"
android:layout_below="#+id/txtMenu"
android:background="#FFFFFF"
/>
</LinearLayout>
My listview adapter codes right down below.
public class MenuListAdapter extends ArrayAdapter<Menu_Item> {
private final ArrayList<Menu_Item> list;
private final Activity context;
private int[] colors = new int[]{0x34a4a4a4, 0x34fafafa};
public MenuListAdapter(Activity context, ArrayList<Menu_Item> list) {
super(context, R.layout.menu_item, list);
this.context = context;
this.list = list;
}
static class ViewHolder {
public ImageView menulogo;
public TextView txtMenu;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
view = inflator.inflate(R.layout.menu_item, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.menulogo = (ImageView) view.findViewById(R.id.menulogo);
viewHolder.txtMenu = (TextView) view.findViewById(R.id.txtMenu);
view.setTag(viewHolder);
int colorPos = position % colors.length;
view.setBackgroundColor(colors[colorPos]);
} else {
view = convertView;
}
ViewHolder holder = (ViewHolder) view.getTag();
holder.txtMenu.setText(list.get(position).getMenuName());
//holder.menulogo.setImageBitmap(list.get(position).getMenuImage());
String menuName=list.get(position).getMenuName();
if (menuName.equals("Info")){
holder.menulogo.setImageResource(R.drawable.infoicon);
}
if (menuName.equals("Odalar")){
holder.menulogo.setImageResource(R.drawable.accomodation);
}
if (menuName.equals("Galeri")){
holder.menulogo.setImageResource(R.drawable.galeryicon);
}
if (menuName.equals("Aktiviteler")){
holder.menulogo.setImageResource(R.drawable.activitiesicon);
}
if (menuName.equals("Robin's Kids Club")){
holder.menulogo.setImageResource(R.drawable.kidsclub);
}
if (menuName.equals("Restaurants")){
holder.menulogo.setImageResource(R.drawable.restaurantsicon);
}
view.setId(list.get(position).getMenu_id());
return view;
}
#Override
public int getViewTypeCount() {
if (getCount() != 0)
return getCount();
return 1;
}
}
I found an answer to my question and wanted to share it with you.
I only used these 2 properties below of ListView
android:divider="#FFFFFF"
android:dividerHeight="1dip"
Like this...
<ListView
android:id="#+id/menuList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:divider="#FFFFFF"
android:dividerHeight="1dip"
/>
In ListView XML add these two lines
<ListView
android:divider="#android:color/white"
android:dividerHeight="5dip"
.
.
</ListView>
Try to change the View with id line for a Space component. It is an extension of View, and works better for what you want.
Here is a description of the widget:
https://developer.android.com/reference/android/widget/Space.html
I have a list that when you click on the row on the right side the layout with the icon is changed by other. This makes it well but when I scrolling in the list, I notice there are rows with the same icon without do click. Also, if I scroll quickly these same icons are lost and are built differently. I think it's on the reuse of cells but I can not find the solution. What I want is that the image stays active only in the row where clicka.
Thanks!!
Adapter class:
public class ListadoVotantesArrayAdapter extends ArrayAdapter<Votante> {
private Context context;
private LayoutInflater inflater;
private ArrayList<Votante> listaVotantes;
private int rowLayout;
private View mConverView;
public ListadoVotantesArrayAdapter(Context context, int resource, ArrayList<Votante> objects) {
super(context, resource,objects);
this.context = context;
this.inflater = LayoutInflater.from(context);
this.listaVotantes = objects;
this.rowLayout = resource;
}
public int getCount(){
return this.listaVotantes.size();
}
public Votante getVotante(int position){
return this.listaVotantes.get(position);
}
private static class ViewHolder {
public TextView lblName;
public TextView lblLastName;
public TextView lblDni;
public TextView lblVoterNumber;
public RelativeLayout lytVoted;
public RelativeLayout lytCanVote;
public RelativeLayout lytUndo;
}
public View getView(int position, View converView, ViewGroup parent) {
final ViewHolder viewHolder;
mConverView = converView;
if (mConverView == null){
viewHolder = new ViewHolder();
this.mConverView = inflater.inflate(this.rowLayout, parent, false);
if (mConverView != null){
viewHolder.lblName = (TextView) mConverView.findViewById(R.id.lblVoterName);
viewHolder.lblLastName = (TextView) mConverView.findViewById(R.id.lblVoterLastName);
viewHolder.lblDni = (TextView) mConverView.findViewById(R.id.lblDni);
viewHolder.lblVoterNumber = (TextView) mConverView.findViewById(R.id.lblNumber);
viewHolder.lytVoted = (RelativeLayout) mConverView.findViewById(R.id.lytVoted);
viewHolder.lytCanVote = (RelativeLayout) mConverView.findViewById(R.id.lytCanVote);
viewHolder.lytUndo = (RelativeLayout) mConverView.findViewById(R.id.lytUndo);
mConverView.setTag(viewHolder);
}
}else{
viewHolder = (ViewHolder) mConverView.getTag();
}
Votante votante = getVotante(position);
viewHolder.lblName.setText(votante.getNombre());
viewHolder.lblLastName.setText(votante.getApellidos());
viewHolder.lblDni.setText(votante.getDni());
viewHolder.lblVoterNumber.setText(""+votante.getNumVotante());
if (votante.getVotado()){
viewHolder.lytVoted.setVisibility(View.VISIBLE);
viewHolder.lytCanVote.setVisibility(View.GONE);
}else{
viewHolder.lytVoted.setVisibility(View.GONE);
viewHolder.lytCanVote.setVisibility(View.VISIBLE);
}
mConverView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
viewHolder.lytUndo.setVisibility(View.VISIBLE);
notifyDataSetChanged();
}
});
return mConverView;
}
}
Layout Row:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/lyt_voter"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="1dp">
<RelativeLayout
android:id="#+id/lytVoterNumber"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_alignParentLeft="true"
android:background="#color/lightBlueItemList">
<TextView
android:id="#+id/lblNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="1999"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
<LinearLayout
android:id="#+id/lytVoterData"
android:layout_width="wrap_content"
android:layout_height="70dp"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/lytCanVote"
android:layout_toRightOf="#+id/lytVoterNumber"
android:layout_toStartOf="#+id/lytCanVote"
android:background="#color/whiteItemList"
android:orientation="vertical"
android:padding="5dp">
<TextView
android:id="#+id/lblVoterLastName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Suarez Garcia de la Serna"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/lblVoterName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="José Carlos"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/lblDni"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="44950962S"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
<RelativeLayout
android:id="#+id/lytCanVote"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_alignParentRight="true"
android:background="#color/yellowItemList"
android:minHeight="30dp"
android:minWidth="30dp">
<ImageView
android:id="#+id/imgVote"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/flecha" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/lytVoted"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_alignParentRight="true"
android:background="#color/grrenAcceptList"
android:minHeight="30dp"
android:minWidth="30dp"
android:visibility="gone">
<ImageView
android:id="#+id/imgAccept"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/aceptar"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="70dp"
android:layout_height="70dp"
android:minHeight="30dp"
android:minWidth="30dp"
android:background="#color/redD3"
android:id="#+id/lytUndo"
android:layout_alignParentRight="true"
android:visibility="gone">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imgUndo"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:src="#drawable/undo"/>
</RelativeLayout>
</RelativeLayout>
You need to follow below Idea
1) this line of code mConverView = converView; doesn't makes sense. directly use converView(View from getView param). Remove mConvertView from the adapter.
2) Add some mechanism which can tell you which row is clicked and save that variable.
Example:- Have an boolean Array of size Rows.size . and set them in onClick.
Boolean[] array = new Boolean[getSize()];
in onClick that you already have in your getview set this.
public void onClick(View v) {
array[position] = !array[position];
viewHolder.lytUndo.setVisibility(View.VISIBLE);
//You do not need to call notifyDatasetChange.
3) in getView(), when you are setting data in to row items/or just before onClick. have a check like below.
if(array[position])// this will tell you if you need to show or hid lytUndo thing
viewHolder.lytUndo.setVisibility(View.VISIBLE); // Show it
else
viewHolder.lytUndo.setVisibility(View.GONE); // hide it or do whatever you want
You might need to set some params as final
I have written this as simple as possible, comment back with your result.
So i have a base adapter class that I built. I also try to use this in one of my activities. I have a xml for the parent layout and an xml for the rows to inflate. However, these components are not linking together. The app doesnt crash but the rows are not inflated (although the parent xml file still displays on the creen). Could some look at my code and tell me what critical component i am overlooking? I will only post the relevant code. Cant post the error logs since no errors are occurring. Thanks very much
Parent XML (nearby_places_list)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:orientation="vertical"
android:background="#drawable/bg">
<Button
android:id="#+id/continuetomap"
android:layout_height="100dp"
android:layout_width="match_parent"
android:background="#drawable/map_continue"
/>
<ListView
android:id="#+id/list_view_nearby"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#b5b5b5"
android:listSelector="#drawable/list_selector">
</ListView>
</LinearLayout>
Row to Inflate XML (nearby_list_row)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
android:orientation="vertical"
android:background="#drawable/list_selector">"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="#drawable/list_selector">
<LinearLayout
android:layout_width="0dp"
android:layout_weight="3"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#drawable/list_selector">
<TextView
android:id= "#+id/name"
android:textSize="20dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="name"
android:textColor="#54944d"/>
<TextView
android:id= "#+id/vicinity"
android:textSize="20dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="vicinity"
android:textColor="#000000"/>
<TextView
android:id= "#+id/openorclosed"
android:textSize="20dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Open"
android:textColor="#000000"
android:textStyle="bold"/>
</LinearLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.74" >
<Button
android:layout_width="60dp"
android:id="#+id/button_add_to_fav"
android:background="#drawable/add_square"
android:layout_height="60dp"
android:layout_alignParentRight="true"
/>
<Button
android:layout_width="60dp"
android:id="#+id/button_call_rest"
android:background="#drawable/add_square"
android:layout_height="60dp"
android:layout_alignParentRight="true"
android:layout_below="#+id/button_add_to_fav"
/>
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
Base Adapter Class
public class AdapterForList extends BaseAdapter {
private final Context context;
private final ArrayList<HashMap<String, String>> alHm;
private final String TAG_A;
private final String TAG_B;
private final String TAG_C;
public AdapterForList(Context context, ArrayList<HashMap<String, String>> alhm, String TAG_1, String TAG_2, String TAG_3) {
super();
this.context = context;
this.alHm = alhm;
TAG_A = TAG_1;
TAG_B = TAG_2;
TAG_C = TAG_3;
}
#Override
public View getView(int position, View convertView , ViewGroup parent){
LayoutInflater inflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflator.inflate(R.layout.nearby_list_row, parent, false);
TextView tv1 = (TextView) convertView.findViewById(R.id.name);
TextView tv2 = (TextView) convertView.findViewById(R.id.vicinity);
TextView tv3 = (TextView) convertView.findViewById(R.id.openorclosed);
Button btnAdd= (Button) convertView.findViewById(R.id.button_add_to_fav);
Button btnCall= (Button) convertView.findViewById(R.id.button_call_rest);
if (alHm.get(position).containsKey(TAG_A)){ // get the value from the key (if it exist) and set the first text view to it
String value = alHm.get(position).get(TAG_A);
tv1.setText(value);;
}
if (alHm.get(position).containsKey(TAG_B)){
String value = alHm.get(position).get(TAG_B);
tv2.setText(value);;
}
if (alHm.get(position).containsKey(TAG_C)){ // get the value from the key (if it exist) and set the first text view to it
String value = alHm.get(position).get(TAG_C);
tv3.setText(value);;
}
return convertView;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return alHm.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
}
Relevant activity code
protected void onPostExecute(Boolean result){
if (result){
// adding data to listview
final ListView lv = (ListView) findViewById(R.id.list_view_nearby);
lv.setAdapter(new AdapterForList(context, listOfHM, JSONextractor.TAG_NAME,
JSONextractor.TAG_VICINITY, JSONextractor.TAG_OPERATINGHOURS_OPENNOW));
//more code ...
}
}
So it turns out my code did work (but not optimally). I posted below how to correctly use the base adapter
Just read the article (really opened my mind) posed by savadow. basically the way i have my BaseAdapter im not utilizing any of the optimization ability of the class. Instead I will change the code to the following in the base adapter class.
http://lucasr.org/2012/04/05/performance-tips-for-androids-listview/
public View getView(int position, View convertView , ViewGroup parent){
LayoutInflater inflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null){
convertView = inflator.inflate(R.layout.nearby_list_row, parent, false);
}
// now if the convertView is not null i simply update the values rather than re-inflate the view (inflating is expensive)
tv1 = (TextView) convertView.findViewById(R.id.name);
tv2 = (TextView) convertView.findViewById(R.id.vicinity);
tv3 = (TextView) convertView.findViewById(R.id.openorclosed);
btnAdd= (Button) convertView.findViewById(R.id.button_add_to_fav);
btnCall= (Button) convertView.findViewById(R.id.button_call_rest);
Copying other people's code that I only half understand, I have succeeded in making a listview, each element of which contains three TextViews and a CheckBox.
The code involves the following two xml files. First "customrowview.xml":
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="142dp"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView android:id="#+id/text1"
android:layout_width="match_parent"
android:layout_height="fill_parent"/>
<TextView android:id="#+id/text2"
android:layout_width="match_parent"
android:layout_height="fill_parent"/>
<TextView android:id="#+id/text3"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right" >
<CheckBox
android:id="#+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<Button
android:id="#+id/editbut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Edit" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Then "customlistview.xml":
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView android:id="#id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000fff"
android:layout_weight="2"
android:drawSelectorOnTop="false">
</ListView>
<TextView android:id="#id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFff00"
android:text="No data"
/>
</LinearLayout>
I get access to the list via:
listView = (ListView) findViewById(R.id.mylist);
The code also involves:
SimpleAdapter adapter = new SimpleAdapter(
this,
list,
R.layout.custom_row_view,
new String[] {"label","daysordate","time"},
new int[] {R.id.text1,R.id.text3, R.id.text2}
);
listView.setAdapter(adapter);
Now what I want to do is something like listView.setlistenerforbuttonwithinlist() !
But can not work out how to do it. I know there have been related questions on SO before, but I can not understand the answers :-(
EDIT: After seeing azgolfers answer... I made a custom adapter as follows:
public class myadapter extends SimpleAdapter
{
LayoutInflater mLayoutInflater;
public void myadapter(Context context)
{
mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View view = null;
if (convertView != null)
{
view = convertView;
}
else
{
view = mLayoutInflater.inflate(R.layout.custom_row_view, null);
}
Button buttonEdit = (Button) view.findViewById(R.id.editbut);
buttonEdit.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0)
{
Log.i("xx","Button pressed!");
}
});
return super.getView(position, convertView, parent);
}
public myadapter(Context context, List<? extends Map<String, ?>> data,int resource, String[] from, int[] to)
{
super(context, data, resource, from, to);
// TODO Auto-generated constructor stub
}
}
Unfortunatly this crashes at the line
view = mLayoutInflater.inflate(R.layout.custom_row_view, null);
With a null pointer exception... not sure why :-(
First, you need to extend your own Adapter, probably from an ArrayAdapter. ArrayAdapter has a method called getView that you will need to override and provide the UI for a listview row at a certain position. e.g.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView != null) {
view = convertView;
} else {
view = mLayoutInflater.inflate(R.layout.custom_row_view, null);
}
Button buttonEdit = (Button) view.findViewById(R.id.editbut);
buttonEdit.setOnClickListener(...);
}
In getView(), since you are building the UI of the row, you have a chance here to set the click handler on your button.
Also, you also need to add this to your Button's xml tag:
android:focusable="false"
android:focusableInTouchMode="false"
Without that, button inside a listview will not fire OnClick event when pressed.
try to сhange
view = mLayoutInflater.inflate(R.layout.custom_row_view, null);
to
view = mLayoutInflater.inflate(R.layout.custom_row_view, parent, false);
Good luck!
I've faced a problem by adding a question for my questionnaire from res/values.
As I need a ListView followed by checkbox in each line, I've created a custom ListView.
This is my list_item.xml
I've deleted android:text="#string/textLarge" and "#string/textSmall" lines, that's why it has an empty line in TextView tags.
<LinearLayout android:id="#+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="2dp">
<TextView
android:id="#+id/textView_large"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp">
</TextView>
<TextView
android:id="#+id/textView_small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp">
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="2dp"
android:gravity="right">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/checkBxc">
</CheckBox>
</LinearLayout>
</LinearLayout>
and a simple list
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
my Main.java file is next
public class Main extends ListActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setListAdapter(new MyAdapter(this,
android.R.layout.simple_list_item_2, R.id.textView_large,
getResources().getStringArray(R.array.questions)));
}
private class MyAdapter extends ArrayAdapter<String> {
public MyAdapter(Context context, int resource, int textViewResourceId,
String[] strings) {
super(context, resource, textViewResourceId, strings);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.list_item, parent, false);
String[] items = getResources().getStringArray(R.array.questions);
CheckBox chk = (CheckBox) row.findViewById(R.id.checkBxc);
TextView txtLarge = (TextView) row.findViewById(R.id.textView_large);
TextView txtSmall = (TextView) row.findViewById(R.id.textView_small);
txtLarge.setText(items[position]);
return row;
and it works correctly if I add an item from questions.xml, but it displays only 1 line, however I need to make a question as an item (i.e "Do you do some sports?") and some comment below (i.e "Check it if you do"). How could I do that?
You have to create a custom ArrayAdapter (a class that extends from ArrayAdapter) and set it for your list-view listView.setAdapter(yourAdapter)
In this adapter you have to override the getView method (to return your custom list-item-view).
See also this example:
http://android-codes-examples.blogspot.com/2011/03/customized-listview-items-selection.html
This piece:
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.list_item, parent, false);
is not good: you create new item view always, even if it already exists destroying excessive objects costs time. And you can use functionality of the ArrayAdapter itself. Look here.