How do you inflate an XML layout in android?

2019-08-29 02:49发布

I'm writing an android application which starts of as a listview, for which I've written a class that extends ArrayAdapter, populating each row from a database. When an Item in the list is clicked, it fires an intent to another class which I want to list the details of the item clicked, so I can inflate the view with the item details. For now I just want to inflate one TextView as follows.

<TextView 
    android:id="@+id/name"
    android:textColor="#FFFFFF"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
/>

How would I go about writing the class for a details page which only establishes itself after receiving an intent? Do I need to extend something to override the getView method?

3条回答
ゆ 、 Hurt°
2楼-- · 2019-08-29 03:18

Generally what you want to do is write another Activity (a details Activity). You will have a corresponding view (XML) to go with this activity. You pass the id of the item clicked through a Bundle. Here is another question which shows hows to pass data to another Activity (through an Intent).

So you would build a new view to show the details. In your new DetailsActivity.java that extends Activity, in your onCreate you would call setContent(R.layout.detailsView) - where R.layout.detailsView corresponds to an xml you created.

查看更多
beautiful°
3楼-- · 2019-08-29 03:24

For inflating you can use

public View myView()
{
    View v;
    LayoutInflater inflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    v = inflater.inflate(R.layout.prob3_layout, null);
    return v;
}
查看更多
倾城 Initia
4楼-- · 2019-08-29 03:31

You need to create Custom Adapter to show your Values in List View. Check out this Custom List in Android

Let me know if you find any difficulty.

查看更多
登录 后发表回答