views:

261

answers:

2

Trying to have a ListView (scrollable list) of rows made of two TextViews. I have a list of items from a database that I want to populating into the LinearLayout->ListView->TextView but can't get to the id...

Layout somewhat like this instructional link, but have backed away from RelativeLayout and using LinearLayout to get it working. Not even worried about how it looks yet; just can't get it wired together yet.

http://android-developers.blogspot.com/2009/02/android-layout-tricks-1.html

Have two XML files (very abbreviated details below) main.xml and stuff.xml

main.xml has

...TextView

...ListView

stuff.xml has

... android:id="@+id/firstLine"

... android:id="@+id/secondLine"

YES, I do setContentView(R.layout.main); right after onCreate.

Getting null on findViewByID(firstLine) and findViewID(secondLine).

I have an ArrayAdapter where I inflate the stuffView. My thinking and understanding of other examples is it's not inflated (this nested stuffView) until I purposely inflate it. That all works fine but when I do the findViewById it returns null and thus I can't setText().

epic Fail due to complete ignorance/newbieness on my part. Note: I've pored through what I can of Reto's book, especially a simliar example on Page 163 but fail fail fail...

Can some kind soul point me in the right direction?

Must I inflate this nested view? (Reto's example does). If so, what am I missing? I'm hoping someone can point me to a better example. My code's probably too involved at this point to post and a bit proprietary.

Thanks

main.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"
 >
 <TextView 
android:id="@+id/identText"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:text="some text here"

  />
 <ListView 
    android:id="@+id/myListView"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
  />

 </LinearLayout>

thing_item.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"
  >
<TextView 
android:id="@+id/identText"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:text="some text here"

  />
 <ListView 
    android:id="@+id/myListView"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
  />

</LinearLayout>

A pojo called Thingy (not copying Thingy.java here - very simple)

The main class: ShowLayoutNicely.java

package com.blap.test;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.TextView;

public class ShowLayoutNicely extends Activity {
 ListView myListView;
 TextView myTextView;

ArrayList<Thingy> thingys;

ThingyAdapter aa;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    myListView = (ListView) findViewById(R.id.myListView);
    myTextView = (TextView) findViewById(R.id.identText);

    thingys = new ArrayList<Thingy>();

    aa = new ThingyAdapter(this, android.R.layout.simple_list_item_1, thingys);
    myListView.setAdapter(aa);

// real deal has a call to go find items from database, populate array and notify of change.      
    Thingy thing1 = new Thingy("first", "first row");
    thingys.add(thing1);
// sadly - this isn't triggering getView() which I've overriden...
    aa.notifyDataSetChanged();

    Thingy thing2 = new Thingy("second", "second row");
    thingys.add(thing2);
    aa.notifyDataSetChanged();

}
}

The adapter override class ThingyAdapter.java

package com.blap.test;

import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;


public class ThingyAdapter extends ArrayAdapter<Thingy> {

int resource;

public ThingyAdapter (Context _context, int _resource, List<Thingy> _items){
super( _context, _resource, _items);
resource = _resource;

}

@Override
public View getView(int position, View convertView, ViewGroup parent)  {
LinearLayout thingView;

Thingy thingItem = getItem(position);

String identString = thingItem.getIdent();
String nameString =thingItem.getName();

if (convertView == null){
    thingView= new LinearLayout(getContext());
    LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View nuView = vi.inflate(resource, thingView, true);

}
else{
    thingView = (LinearLayout) convertView;

}
//here's the problem - these calls return nulls.
TextView row1 = (TextView)thingView.findViewById(R.id.firstLine);
TextView row2 = (TextView)thingView.findViewById(R.id.secondLine);

//and naturally these puke ingloriously....
row1.setText(thingItem.getIdent());
row2.setText(thingItem.getName());
return thingView;
}
}

So this code is in essence what I'm looking for help on; neutered the names to call it Thingy.... This sample isn't triggering the getView(). That's a secondary problem I have to sort out. More importantly, your help on the findViewById failure and if I've got the XML right would help a bunch.

A: 

Where you are inflating your layout you can use findViewById on the layout that you are inflating, as I don't know what your code is like here is an example of what I would do:

 LayoutInflater li = LayoutInflater.from(mContext);
 LinearLayout mLayout = (LinearLayout) li.inflate(R.layout.stuff,null);
 View mView = mLayout.findViewById(R.id.firstLine);

You probably want your null to be the parent that you want your inflated layout to have. Hope this helps!

Update

I'd try replacing what you have as:

thingView= new LinearLayout(getContext());
LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View nuView = vi.inflate(resource, thingView, true);

with:

LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
thingView = vi.inflate(R.layout.thing_item, null);

As you seem to make a new linearlayout for no reason, as your inflation will already be have a linear layout from your xml file. This is pretty much what I said above, and this works for me :)

stealthcopter
Yeah, I already had the inflate there, though done a bit differently. All the calls are working (or so they seem), ie: mLayout returns with a value. It's just the call findViewById that returns null. So in your example above, mView == null.
teachableMe
So have you got it working?
stealthcopter
nope; I'm gutting this down to bare essentials so I can post the code. Kind of hard for folks to help without some concrete code I think.
teachableMe
see post; edited to include code.
teachableMe
updated answer to revise your code
stealthcopter
Stealthcopter - finally - sorted out a typo I had in my XML, integrated YOUR suggestion and then made sure and changed the width and height to "wrap_content", worked!Thank You!So I'm pretty confident your advice fixed my original problem.The problem of not firing the getView() was my XML indicating "fill_parent". Others - see this link! http://androidforums.com/android-developers/55750-listview-arrayadapter-getview.html
teachableMe
A: 

I'm having a nearly identical problem. I've been scouring the internet for about 4 hours trying to figure this one out. If you discover what's going on here, it'd be great if you posted back with the solution.

dfetter88
this should be a comment
stealthcopter