【问题标题】:Android List with two elements per row每行两个元素的Android列表
【发布时间】:2012-03-12 17:01:40
【问题描述】:

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

param1_name 值1

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 的数据通过蓝牙来自设备。

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      您可以在 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 会有所帮助。

      【讨论】:

        【解决方案3】:

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

        这里是那个例子,

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

        希望你能得到答案

        【讨论】:

          【解决方案4】:

          在这段代码中,我使用了 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>
          

          ma​​in.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);
              }
          }
          

          【讨论】:

            猜你喜欢
            • 2012-12-28
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-06-16
            • 2013-04-08
            • 2016-04-13
            • 1970-01-01
            • 2018-11-27
            相关资源
            最近更新 更多