【发布时间】:2013-02-22 01:07:07
【问题描述】:
我正在动态设置listView。我将从parse.com 收到的每行显示两件事。我希望第一件事左对齐和蓝色,第二件事右对齐和红色。我将有两个数组,firstArray 和 secondArray。我找到了很多方法来做到这一点,但没有一个是动态的。我应该怎么做,或者我可以在xml 做?
更新
这是我在@t0s 发布教程之后的xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="2dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/column1"
android:layout_width="50dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#0000FF"
android:gravity="left"
/>
<TextView
android:id="@+id/center"
android:text="VS"
android:textSize="15sp"
android:layout_width="50dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#FFFFFF"
android:gravity="center"
/>
<TextView
android:id="@+id/column2"
android:layout_width="50dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#FF0000"
android:gravity="right"
/>
</LinearLayout>
还有我的java
ListView list = (ListView) findViewById(R.id.mylist);
ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(MainActivity.this, R.layout.mylist, namesArray);
list.setAdapter(listAdapter);
//find my objects off parse, and call this method
private String display(Object name, Object record,
ArrayAdapter listAdapter) {
String fightCard = (name).toString();
namesArray.add((name).toString() +" " + (record).toString());
return fightCard;
}
【问题讨论】:
-
在 XML 中执行!容易得多:),请为您的列表视图行提供您的 xml
-
你有自定义行xml吗?
-
不,我不...什么是自定义行?
-
简单示例here。你需要使用SimpleAdapter
-
简而言之:创建一个代表每一行的新 xml 布局,例如,如果您连续有 3 个 TextView,您将在新的 xml 布局文件中添加它们。他们每个人都会有一个id。剩下的工作由 SimpleAdapter 完成。