How do you inflate an XML layout in android?

人走茶凉 提交于 2019-12-25 05:16:28

问题


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?


回答1:


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;
}



回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/7485284/how-do-you-inflate-an-xml-layout-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!