简体   繁体   English

如何为列表中的每个项目添加文本视图?

[英]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.

相关问题 如何在我的 Recycler 视图中的每一行列表中添加图像? - How to add images in my Recycler view with in every line of list? 每当我向JTextArea中添加文本时,如何阻止我的Swing程序重新调整组件的大小 - How can I stop my Swing program from resizing components every time I add text to my JTextArea 如何在recyclerview项目中获取文本视图的值? - how can i get the value of text view in recyclerview item? 如何为我的列表项添加附加值 - How do I add additional value to my list item 如何在Java中的每个第x个项目之后将项目添加到列表 - How to add an item to a list after every xth item in Java 如何将项目添加到链接列表的末尾? - How can I add an item to the end the a Linked List? 我不能 select 列表视图中的项目 - I can't select an item in list view 如何在回收者视图的每个项目中使用地图 - How to use map in every item of my recycler view 如何在Android应用程序中的每个ListView项目文本之前添加图标? - How to add an icon before every ListView item text in an Android Application? 如何使用正则表达式将列表中的每个项目解析为单独的组? - How can I use Regular Expressions to parse every item from the list to a separate group?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM