2

I connent an addevent.java with addevent.xml is content a listview, the listview take its element from another XML file "item.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" >

        <Button
            android:id="@+id/d"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:drawablePadding="0dip"
            android:drawableRight="@drawable/icon_name" />

        <EditText
            android:id="@+id/en"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_toLeftOf="@+id/d" >
        </EditText>

</LinearLayout>

now I need to findviewbyId not from basic xml file but form another "item.xml"

I tried LayoutInflater as this, code but it isn't work:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.addevent);
        myList = (ListView) findViewById(R.id.MyList);
        myList.setItemsCanFocus(true);
        myAdapter = new MyAdapter();
        myList.setAdapter(myAdapter);


        final LayoutInflater factory = getLayoutInflater();

        final View textEntryView = factory.inflate(R.layout.item, null);

        tvDisplayDate = (EditText)textEntryView.findViewById(R.id.editd);
        btnChangeDate=(Button)textEntryView.findViewById(R.id.d);

any help please

SWE
  • 131
  • 2
  • 7
  • 20

2 Answers2

6

Try this View view = LayoutInflater.from(getApplication()).inflate(R.layout.item, null);

How you can see, static from() method takes 1 argument(Context) and you obtains LayoutInflater from given Context, in your case it will be your Activity where you want to inflate View.

Hope it helps!

Simon Dorociak
  • 33,374
  • 10
  • 68
  • 106
1

From insinde your MyAdapter class you can override the getView method (that's the one that produces the item renderers on demand).

After recycling / inflating the proper layout for the current item (say convertView), you can access all of its children using the findViewById method called on the convertView:

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        // initialize, inflate convertView from your layout if needed; 
        tvDisplayDate = (EditText)convertView.findViewById(R.id.editd);
        // populate convertView with the item's details
    }
rekaszeru
  • 19,130
  • 7
  • 59
  • 73
  • 2
    @Akashkumar see the edited response with the full method signature: `convertView` is the second parameter of the `getView` method inside the `Adapter` implementation; – rekaszeru May 30 '16 at 09:53
  • I just spent an hour resolving this error. The only thing I missed was calling findViewById on convertView. Thanks for making me realize this! – waseefakhtar Nov 27 '17 at 00:24