0

I have created following adapter:

package org.virtualbet.gui.util;

import java.util.List;


import org.virtualbet.R;
import org.virtualbet.model.Tip;


import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class TipListAdapter extends ArrayAdapter<Tip> {
    private final Context context;
    private List<Tip> values;

    public TipListAdapter(Context context, int textViewResourceId, List<Tip> objects) {
        super(context, textViewResourceId, objects);
        //super(context, R.layout.rowlayout, values);
        this.context = context;
        this.values = objects;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        try {
            View v = convertView;
            if (v == null) {
                LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = inflater.inflate(R.layout.row_tip, null);
            }
            Tip tip = values.get(position);
            if (tip!=null) {
                ((TextView)v.findViewById(R.id.textviewtipid)).setText((CharSequence)tip.getTipNumber());
                ((TextView)v.findViewById(R.id.textviewtiprepresentation)).setText((CharSequence)tip.getTipRepresentation());
                ((TextView)v.findViewById(R.id.textviewtipresult)).setText((CharSequence)tip.getDetails());
                ((TextView)v.findViewById(R.id.textviewtipid)).setText((CharSequence)tip.getTipOptions());
                return v;
            }
            return null;
        } catch (Throwable t) {
            Log.e("A", t.toString());
            return null;
        }
    }


}

ListView xml looks like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
    <TextView android:text="TextView" 
    android:id="@+id/textviewtipid" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    >
    </TextView>
    <TextView android:text="TextView" 
    android:id="@+id/textviewtiprepresentation" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content">
    </TextView>
     <TextView android:text="TextView" 
    android:id="@+id/textviewtipresult" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content">
    </TextView>
    <TextView android:text="TextView" 
    android:id="@+id/textviewtipplayed" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content">
    </TextView>

</LinearLayout>

getView method is called...all textviews change their values...and after last change (no matter which one is it) program goes into catch part???

Why? Did anyone have problems like this? I've read somewhere that's a project (eclipse) issue....

Oh yes, place where I am changing model is:

package org.virtualbet.gui;

import java.util.ArrayList;
import java.util.List;

import org.virtualbet.R;
import org.virtualbet.bll.TipBLL;
import org.virtualbet.gui.util.TipListAdapter;
import org.virtualbet.model.Ticket;
import org.virtualbet.model.Tip;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class FollowTicket extends Activity {

    private static final String TAG = "FollowTicket";

    private TipBLL bll;
    private TipListAdapter tipListAdapter;
    private ListView lv;
    private List<Tip> tips;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.follow_ticket);

        bll = new TipBLL();
        tips = new ArrayList<Tip>();
        tipListAdapter = new TipListAdapter(this.getApplicationContext(), R.layout.row_tip, tips);
        lv = ((ListView)findViewById(R.id.listViewTips));
        lv.setAdapter(tipListAdapter);
        ((Button)findViewById(R.id.buttonSearchForTicket)).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                searchTicket(((EditText)findViewById(R.id.editTextSerialNumber)).getText().toString());
            }
        });
    }

    private void searchTicket (String serialNumber) {
        Log.i(TAG, "Searching ticket " + serialNumber);
        Ticket ticket = bll.getTicket(serialNumber);
        if (ticket != null){
            Log.i(TAG, "Found it!");
            tips.clear();
            for (Tip tip : ticket.getTips()) {
                Log.i(TAG, "Adding tip " + tip.getTipNumber());
                tips.add(tip);
            }
            tipListAdapter.notifyDataSetChanged();
        } else {
            Log.i(TAG, "Didn't find it!");
        }
    }

}
3
  • Can you see the "Adding tip #" lines in your logcat? Commented Mar 12, 2012 at 22:02
  • Yes I can..and when I try to debug, it calls notifyDataSetChanged()... :/ Commented Mar 12, 2012 at 22:08
  • And what's the most interesting, Throwable t is empty (I've also put Exception to catch but it was also empty).... Commented Mar 12, 2012 at 22:31

2 Answers 2

1

FIXED IT! Really stupid mistake. In my layout where I was holding list to populate I had

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent" <-------------ERROR!!!!!!
    android:background="#880000"
    android:orientation="horizontal" >

.............some elements

</LinearLayout>
<ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
</ListView>

Where ERROR!!! is pointing it should be wrap_content because this layout pushes out ListView out of the scope and it doesn't know that there is ListView (in runtime, while coding compiler won't complain)......really stupid!!!

Thanks on help anyway....

Sign up to request clarification or add additional context in comments.

Comments

0

May be you want to have a look at this part again in your code:

        if (tip!=null) {
            ((TextView)v.findViewById(R.id.textviewtipid)).setText((CharSequence)tip.getTipNumber());
            ((TextView)v.findViewById(R.id.textviewtiprepresentation)).setText((CharSequence)tip.getTipRepresentation());
            ((TextView)v.findViewById(R.id.textviewtipresult)).setText((CharSequence)tip.getDetails());
            ((TextView)v.findViewById(R.id.textviewtipid)).setText((CharSequence)tip.getTipOptions());
            return v;
        }

I think you are trying R.id.textviewtipid two times.

        if (tip!=null) {
            ((TextView)v.findViewById(R.id.textviewtipid)).setText((CharSequence)tip.getTipNumber());
            ((TextView)v.findViewById(R.id.textviewtiprepresentation)).setText((CharSequence)tip.getTipRepresentation());
            ((TextView)v.findViewById(R.id.textviewtipresult)).setText((CharSequence)tip.getDetails());
            ((TextView)v.findViewById(R.id.textviewipplayed)).setText((CharSequence)tip.getTipOptions());
            return v;
        }

This could be the reason

1 Comment

Thanks for notifying this, but no, it didn't help :/...I'm really not sure what I am doing wrong :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.