Java 每行两个元素的Android列表
我想在Android应用程序中创建一个如下所示的列表: 参数1_名称值1 参数2_名称值2 参数2_名称值2 其中,参数名称仅为字符串,但每个值都来自一个不同的LinkedList 谁能给我一些建议吗?这是我到目前为止所做的,所以不太多:Java 每行两个元素的Android列表,java,android,android-layout,linked-list,android-listview,Java,Android,Android Layout,Linked List,Android Listview,我想在Android应用程序中创建一个如下所示的列表: 参数1_名称值1 参数2_名称值2 参数2_名称值2 其中,参数名称仅为字符串,但每个值都来自一个不同的LinkedList 谁能给我一些建议吗?这是我到目前为止所做的,所以不太多: import android.app.Activity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.ListView; pub
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);
}
}
导入android.app.Activity;
导入android.os.Bundle;
导入android.widget.ArrayAdapter;
导入android.widget.ListView;
公共类MyListActivity扩展活动{
专用ArrayAdapter参数ArrayAdapter;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.param_列表);
字符串paramNames[]={“param1”、“param2”、“param3”};
paramArrayAdapter=新的ArrayAdapter(这是android.R.layout.simple_list_item_1,paramNames);
ListView参数ListView=(ListView)findViewById(R.id.params);
setAdapter(paramArrayAdapter);
}
}
我忘了告诉您列表中的值必须不时更改。LinkedList的数据来自蓝牙设备。我认为用标准的
阵列适配器无法做到这一点。您需要创建自己的适配器类,并在其getView
类中创建一个简单的LinearLayout
,其中有两个TextView
,每个都显示您行的相应部分,然后返回该线性布局。这是一个相当简单的练习。您可以在ListActivity中使用自己的布局。然后使用类似“CustomAdapter=new CustomAdapter(this,R.layout.event_row,R.id.event_row_lbl_title,allEvents);”的内容代替ArrayAdapter
我想这会有所帮助。在我使用的这段代码中有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>
<?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>
main.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>
<?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);
}
}
import java.util.ArrayList;
导入java.util.HashMap;
导入android.app.ListActivity;
导入android.os.Bundle;
导入android.widget.simpledapter;
公共类MainActivity扩展了ListActivity{
静态最终ArrayList列表=
新的ArrayList();
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
SimpleAdapter适配器=新SimpleAdapter(
这
列表
R.layout.main,
新字符串[]{“等级”、“型号”、“公司”},
新的int[]{R.id.text1,R.id.text2,R.id.text3}
);
大众主义者();
setListAdapter(适配器);
}
私有void populateList(){
HashMap=newHashMap();
地图放置(“排名”,“1”);
地图放置(“模型”,“三星银河Nexus”);
map.put(“公司”、“三星”);
列表。添加(地图);
map=新的HashMap();
地图放置(“排名”,“2”);
地图放置(“模型”,“三星Epic Touch 4G”);
map.put(“公司”、“三星”);
列表。添加(地图);
map=新的HashMap();
地图放置(“等级”,“3”);
地图放置(“模型”,“HTC Evo 3D”);
map.put(“公司”、“HTC”);
列表。添加(地图);
map=新的HashMap();
地图放置(“排名”,“4”);
地图放置(“模型”,“T-Mobile MyTouch 4G幻灯片”);
map.put(“公司”、“HTC”);
列表。添加(地图);
map=新的HashMap();
地图放置(“排名”,“5”);
地图放置(“模型”,“三星Galaxy S II”);
map.put(“公司”、“三星”);
列表。添加(地图);
map=新的HashMap();
地图放置(“排名”,“6”);
地图放置(“型号”,“苹果iPhone4S16GB”);
map.put(“公司”、“苹果”);
列表。添加(地图);
map=新的HashMap();
地图放置(“排名”,“7”);
地图放置(“模型”,“摩托罗拉机器人Razr”);
map.put(“公司”、“摩托罗拉”);
列表。添加(地图);
map=新的HashMap();
地图放置(“排名”,“8”);
地图放置(“模型”,“摩托罗拉机器人仿生”);
map.put(“公司”、“摩托罗拉”);
列表。添加(地图);
map=新的HashMap();
地图放置(“排名”,“9”);
地图放置(“模型”,“三星焦点S”);
map.put(“公司”、“三星”);
列表。添加(地图);
map=新的HashMap();
地图放置(“排名”,“10”);
地图放置(“模型”,“三星焦点闪光灯”);
map.put(“公司”、“三星”);
列表。添加(地图);
}
}
您需要使用另一个线性列表和两个文本视图创建一个新适配器,并将其膨胀到原始列表视图,然后根据膨胀的列表视图获取值
我举个例子
希望你能得到你的答案