Android列表,每行两个元素

时间:2012-03-12 17:01:41

标签: java android android-layout linked-list android-listview

我想在Android应用程序中创建一个如下所示的列表:

  

param1_name value1

     

param2_name value2

     

param2_name value2

...

其中param_names只是字符串,但每个值都来自一个不同的LinkedList。

任何人都可以给我任何建议吗?这是我到目前为止所做的,所以不是很多:

import android.app.Activity;
import android.os.Bundle; 
import android.widget.ArrayAdapter; 
import android.widget.ListView;

public class MyListActivity extends Activity {

    private ArrayAdapter<String> paramArrayAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.param_list);

        String paramNames[] = { "param1", "param2", "param3" };

        paramArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, paramNames);

        ListView paramListView = (ListView) findViewById(R.id.params);
        paramListView.setAdapter(paramArrayAdapter);
    }
}

我忘了告诉你列表中的值必须不时更改。 LinkedLists有来自设备的数据通过蓝牙。

4 个答案:

答案 0 :(得分:1)

我认为你不能用标准的ArrayAdapter做到这一点。您需要创建自己的适配器类,并在其getView类中创建一个简单的LinearLayout,其中两个TextView分别显示您行的相应部分 - 然后返回该线性布局。这是一个相当简单的练习。

答案 1 :(得分:1)

您可以在ListActivity中使用自己的布局。然后你使用类似“CustomAdapter adapter = new CustomAdapter(this,R.layout.event_row,R.id.event_row_lbl_title,allEvents);”而不是你的ArrayAdapter。

我认为http://www.codeproject.com/Articles/183608/Android-Lists-ListActivity-and-ListView-II-Custom会有所帮助。

答案 2 :(得分:0)

在此代码中,我使用了3列,您将根据自己的需要进行更改..

<强> listview.xml

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@id/android:list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#bfffdf"
    android:drawSelectorOnTop="false">
</ListView>

<强> main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:paddingTop="4dip"
     android:paddingBottom="6dip"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:orientation="horizontal">
     <TextView android:id="@+id/text1"
        android:textColor="#00ff00" 
        android:textSize="18sp"
        android:layout_width="30dip"
        android:layout_height="wrap_content"/>
     <TextView android:id="@+id/text2"
        android:textColor="#ff0000"
        android:textSize="18sp"
        android:layout_width="110dip"
        android:layout_height="wrap_content" 
        android:layout_weight="1"/>
     <TextView android:id="@+id/text3"
        android:textColor="#0000ff"
        android:textSize="14sp"
        android:layout_width="40dip"
        android:layout_height="wrap_content"  
        android:layout_weight="1"/>
</LinearLayout>

<强> MainActivity.java

import java.util.ArrayList;
import java.util.HashMap;

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

public class MainActivity extends ListActivity {

    static final ArrayList<HashMap<String,String>> list = 
        new ArrayList<HashMap<String,String>>();

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listview);
        SimpleAdapter adapter = new SimpleAdapter(
                this,
                list,
                R.layout.main,
                new String[] {"rank","model","company"},
                new int[] {R.id.text1,R.id.text2, R.id.text3}
        );     
        populateList();
        setListAdapter(adapter);    
    }

       private void populateList() {
        HashMap map = new HashMap();
        map.put("rank", "1");
        map.put("model", "Samsung Galaxy Nexus");
        map.put("company", "Samsung");
        list.add(map);
        map = new HashMap();
        map.put("rank", "2");
        map.put("model", "Samsung Epic Touch 4G");
        map.put("company", "Samsung");
        list.add(map);
        map = new HashMap();
        map.put("rank", "3");
        map.put("model", "HTC Evo 3D");
        map.put("company", "HTC");
        list.add(map);
        map = new HashMap();
        map.put("rank", "4");
        map.put("model", "T-Mobile MyTouch 4G Slide");
        map.put("company", "HTC");
        list.add(map);
        map = new HashMap();
        map.put("rank", "5");
        map.put("model", "Samsung Galaxy S II");
        map.put("company", "Samsung");
        list.add(map);
        map = new HashMap();
        map.put("rank", "6");
        map.put("model", "Apple iPhone 4S 16GB");
        map.put("company", "Apple");
        list.add(map);
        map = new HashMap();
        map.put("rank", "7");
        map.put("model", "Motorola Droid Razr");
        map.put("company", "Motorola");
        list.add(map);
        map = new HashMap();
        map.put("rank", "8");
        map.put("model", "Motorola Droid Bionic");
        map.put("company", "Motorola");
        list.add(map);
        map = new HashMap();
        map.put("rank", "9");
        map.put("model", "Samsung Focus S");
        map.put("company", "Samsung ");
        list.add(map);
        map = new HashMap();
        map.put("rank", "10");
        map.put("model", "Samsung Focus Flash");
        map.put("company", "Samsung");
        list.add(map);
    }
}

答案 3 :(得分:0)

您需要使用另一个带有2个文本视图的线性列表创建一个新适配器,并将其扩展到原始列表视图,并根据膨胀的列表视图获取值。

这里是id的例子,

http://www.vogella.de/articles/AndroidListView/article.html#adapterperformance

希望你能得到答案

相关问题