1

我正在listView动态设置。我将在每行显示两件我将从中接收到的东西parse.com。我希望第一件事左对齐和蓝色,第二件事右对齐和红色。我将有两个数组firstArray, 和secondArray。我找到了很多方法来做到这一点,但没有一个是动态的。我应该怎么做,或者我可以做到xml

更新

这是我xml在@t0s 发布的那个教程之后

<?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;
}
4

2 回答 2

1

那么我的代码是一团糟。检查这里的教程是[非常简单]

好的乍得检查这里的代码:

主要活动 :

package org.example.chad;

import java.util.ArrayList;

import android.app.ListActivity;
import android.os.Bundle;

public class MainActivity extends ListActivity {


private ArrayList<DataItem> data = new ArrayList<DataItem>();

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

    //create Objects
    DataItem obj1 = new DataItem("name1", "record1");
    DataItem obj2 = new DataItem("name2", "record2");
    DataItem obj3 = new DataItem("name3", "record3");
    DataItem obj4 = new DataItem("name4", "record4");
    DataItem obj5 = new DataItem("name5", "record5");
    //add the to ArrayList
    data.add(obj1);
    data.add(obj2);
    data.add(obj3);
    data.add(obj4);
    data.add(obj5);


        CustomAdapter adapter = new CustomAdapter(this, R.layout.row, data);
        setListAdapter(adapter);        

}

}

主.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

 <ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

</RelativeLayout>

数据项

package org.example.chad;

public class DataItem {
private String name;
private String record;

public DataItem(){

}

public DataItem(String n, String r ){
    this.name = n;
    this.record = r;
}

public String getname() {
    return name;
}

public void setname(String name) {
    this.name = name;
}

public String getrecord() {
    return record;
}

public void setrecord(String record) {
    this.record = record;
}

}

自定义适配器:

package org.example.chad;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class CustomAdapter extends ArrayAdapter<DataItem> {
   private ArrayList<DataItem> objects;

   public CustomAdapter(Context context, int textViewResourceId, ArrayList<DataItem>     objects) {
        super(context, textViewResourceId, objects);
        this.objects = objects;
}

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {

  View v = convertView;
  if (v == null) {
        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.row, null);
    }


  DataItem i = objects.get(position);

if (i != null) {
    TextView nameView = (TextView) v.findViewById(R.id.name);
    TextView recordView = (TextView) v.findViewById(R.id.record);

    if (nameView != null){
        nameView.setText(i.getname());
    }
    if (recordView != null){
        recordView.setText(i.getrecord());
    }

}

return v;
  }
} 

row.xml

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


    <TextView
      android:id="@+id/name"
      android:layout_width="100sp"
      android:layout_height="20sp"
      android:textSize="16sp"
      android:layout_gravity="left|center_vertical"
      android:layout_marginLeft="10sp"/>

    <TextView
        android:id="@+id/record"
        android:layout_width="100sp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10sp"
        android:layout_gravity="right"
        android:textSize="16sp" />

</LinearLayout>

几个小时后,我将添加一些评论。结果在这里 在此处输入图像描述

于 2013-02-22T02:17:37.233 回答
0

如果我没记错的话,您必须扩展BaseAdapter类,以便为您的 ListView 提供一个自定义适配器,如果不创建自定义适配器,就不可能将两个数组项添加到 ListView。

这是一个教程。

并不像看起来那么难。希望能帮助到你。

于 2013-02-22T01:19:48.333 回答