简体   繁体   中英

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. 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)?

Thanks in advance,

Jacob

Should be able to do this with an adapter and listview:

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)

<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)

<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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM