如何在ListView中打印对象

时间:2018-04-20 10:48:42

标签: java android-studio

我正在尝试使用Android Studio打印ListView。我成功地在列表的每个元素中打印了一个字符串,但是现在我想打印一个名为User的对象,其中包含两个字符串(用户名和无线电名称)。

这是我的主要活动:

Application.ThisWorkbook.Worksheets.Add

我的主要布局:

public class UserActivity extends AppCompatActivity implements 
AdapterView.OnItemClickListener {

//Variables
private User User1 = new User("User1","Radio1");
private User User2 = new User("User2","Radio2");
private User User3 = new User("User3","Radio3");
private User[] mUsers = {User1, User2, User3};

//GUI Widgets
ListView mListUsers = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_user);

    //Bind GUI Widgets
    mListUsers = (ListView) findViewById(R.id.usersListView);
    ArrayAdapter<String> myAdapter = new ArrayAdapter<String> (this, R.layout.users_list, mUsers.getName()); //The getName() is not found here
    mListUsers.setAdapter(myAdapter);
    mListUsers.setOnItemClickListener(this);
    }

users_list.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:id="@+id/usersListView"
        android:layout_width="wrap_content"
        android:layout_height="450dp"
        android:paddingTop="4dip"
        android:paddingBottom="3dip" />

    </RelativeLayout>

你能给我一些提示吗?

0 个答案:

没有答案