[英]How can I add a text view for every item in my list?
I am making a yelp-like app in which users can leave reviews for various places, but I want other users to be able to see all of the reviews left. 我正在制作一个类似yelp的应用程序,用户可以在其中留下有关各个地方的评论,但我希望其他用户能够看到所有剩余的评论。 Currently, I am using a list to store all of my reviews in, but I can't seem to figure out how I can make a textview for each item in the list, seeing as the length of the list may vary.
目前,我正在使用列表存储所有评论,但由于列表的长度可能有所不同,我似乎无法弄清楚如何为列表中的每个项目制作文本视图。 Also, piggy backing on this question, would there be anyway to format the textview (ie layoutbelow or margins)?
另外,支持这个问题,是否仍然可以格式化textview的格式(例如,布局下方或边距)?
Thanks in advance, 提前致谢,
Jacob 雅各
Should be able to do this with an adapter and listview: 应该能够使用适配器和listview做到这一点:
Activity.java: Activity.java:
public class YourActivity extends Activity
{
ArrayList<String> reviewsArray = new ArrayList<String>();
ListView reviewList;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.youractivity);
reviewList = (ListView) findViewById(R.id.yourListViewId);
//fill your reviewsArray...
ArrayAdapter<String> reviewsAdapter = new ArrayAdapter<String>(context, R.layout.reviewLayout, reviewsArray);
reviewsList.setAdapter(reviewsAdapter);
}
}
activity_layout.xml: (change size of the listview here) activity_layout.xml :(在此更改列表视图的大小)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/yourListViewId"
android:layout_height="wrap_content"
android:layout_width="match_parent">
</ListView>
</LinearLayout>
reviewLayout.xml (change the reviews padding etc here) reviewLayout.xml(在此处更改评论填充等)
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+android:id/gridCell"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minHeight="?android:attr/listPreferredItemHeight"
android:paddingStart="1dp"
android:paddingEnd="1dp"
android:textAppearance="?android:attr/textAppearanceSmall" />
Without seeing your code it's hard to give you an exact example, but this contains all the basics. 没有看到您的代码,很难给您一个确切的例子,但这包含了所有基础知识。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.