【问题标题】:Android studio adapter align part of the text on the right sideAndroid studio 适配器在右侧对齐部分文本
【发布时间】:2017-01-05 16:38:46
【问题描述】:

我正在尝试使用适配器在 android studio 中制作排行榜,但这工作得很好,如下所示:

但我想让分数显示在右侧。所以我做了这个,但是滚动进来时出现问题:

因为你可以滚动它们,所以名称和分数不匹配。

我怎么能做到这一点,因为只有一个卷轴,或者不知何故,名字在左边,分数在右边。

// Get ListView object from xml
final ListView nameView = (ListView) findViewById(R.id.nameView);
final ListView scoreView = (ListView) findViewById(R.id.scoreView);

// Create a new Adapter
final ArrayAdapter<String> nameAdapter = new ArrayAdapter<>(this,
        android.R.layout.simple_list_item_1, android.R.id.text1);
final ArrayAdapter<String> scoreAdapter = new ArrayAdapter<>(this,
        android.R.layout.simple_list_item_1, android.R.id.text1);

// Assign adapter to ListView
nameView.setAdapter(nameAdapter);
scoreView.setAdapter(scoreAdapter);

// Ordering with score and adding key values as string to nameList
highscoreRef.orderByChild("score").addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(DataSnapshot snapshot, String previousChildKey) {
        nameList.push(snapshot.child("name").getValue().toString());
        scoreList.push(snapshot.child("score").getValue().toString());
    }
    @Override
    public void onChildChanged(DataSnapshot dataSnapshot, String s) {

    }
    @Override
    public void onChildRemoved(DataSnapshot dataSnapshot) {

    }
    @Override
    public void onChildMoved(DataSnapshot dataSnapshot, String s) {

    }
    @Override
    public void onCancelled(DatabaseError databaseError) {
        Toast.makeText(getApplicationContext(), "Error sending data.", Toast.LENGTH_LONG).show();
    }

});

//Collections.reverse(nameList);

highscoreRef.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {

        for (String name : nameList) {
            nameAdapter.add(name);
        }
        for (String score : scoreList) {
            scoreAdapter.add(score);
        }

        nameList.clear();
        scoreList.clear();
    }
}


<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:text="Huippupisteet"
    android:id="@+id/highscore_text"
    android:layout_gravity="center"
    android:textSize="25dp"
    android:layout_weight="0"/>


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">


    <ListView
        android:id="@+id/nameView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        >

    </ListView>
</LinearLayout>

【问题讨论】:

  • 你需要 2 个单独的列表视图吗?
  • 嗯,实际上不是。重点是将用户名放在左边,分数放在右边。

标签: android listview firebase firebase-realtime-database


【解决方案1】:

您需要将用户名和分数字段添加到唯一的列表项,然后使用单个 ListView。然后在您的适配器中,您可以设置这两个值。 . 为此,请根据此示例修改您的 simple_list_item_1:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="100dp">

    <TextView
        android:id="@+id/username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@+id/score"
        android:ellipsize="end"
        android:maxLines="1"
        android:text="Roger" />

    <TextView
        android:id="@+id/score"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true" />

</RelativeLayout>

删除两个 ListView 中的一个,扩展另一个以获取所有必要的宽度并修改您的适配器以设置这两个值,现在将两者都呈现到单个膨胀视图中。

您必须使用扩展 BaseAdapter 的适配器。你可以找到一个很好的例子here

【讨论】:

  • 如何定位 id 分数或用户名?
  • 抱歉,我没有看到您使用的是 ArrayAdapter。我会编辑我的答案
  • 好的,我对适配器很陌生,所以如果有更好的选择,我会全力以赴
【解决方案2】:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"`enter code here`
    android:layout_height="100dp"`enter code here`
    android:background="@android:color/white"
    tools:context="com.example.anuragsingla.myapplication.MainActivity">

    <TextView`enter code here`
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="10dp"
        android:gravity="center_vertical|left"
        android:layout_marginStart="10dp"
        android:text="Jejeje" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="10dp"
        android:layout_marginEnd="10dp"
        android:gravity="center_vertical|right"
        android:text="3" />
</RelativeLayout>

使用上面的布局。将其替换为 R.layout.simple_list_item_1。

删除 Listview 之一。使用单个列表视图并制作自定义适配器并膨胀上述布局。我使用了两个 TextViews 。为 name 设置一个 textview,为 number 设置另一个。

http://www.journaldev.com/10416/android-listview-with-custom-adapter-example-tutorial

【讨论】:

  • 如何在用户名 id 中保存一些字符串?比如 android:id="@+id/username"
  • TextView mUsernameTv = mView.findViewById(R.id.username); mUmUsernameTv.setText("你的用户名");请在自定义适配器类中编写此代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-04-06
  • 2021-08-26
  • 2012-11-26
  • 1970-01-01
  • 2018-05-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多