通过Adapter根据类对象的里的数组属性的长度为LinearLayout动态加载TextView,使用BaseAdapter
基本功能:右侧显示Name,左侧排列显示value[]
item_list_view.xml:
<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:layout_width="80dp"
android:layout_height="wrap_content"
android:id="@+id/name_textView"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textLinearLayout"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
创建一个SerialClass:我想实现左侧显示key,右侧对应多个values的情况,但不知道其他有什么合适的工具,查了下List、Map都不行,也可能可以用但我不知道(如果有知道的老师,望告知,谢谢)。
类包含两个变量:一个String name,另一个是数组String[] snValue
下面是代码:
public class SerialClass {
private String name;
private String[] snValue;
public SerialClass(){};
public SerialClass(String _name, String[] _snValue) {
this.name = _name;
this.snValue = _snValue;
}
public String getName() {
return name;
}
public String[] getSnValue() {
return snValue;
}
public void setName(String _name) {
this.name = _name;
}
public void setSnValue(String[] _snValue) {
this.snValue = _snValue;
}
}
MyAdapter:为LinearLayout动态添加TextView
public class MyAdapter extends BaseAdapter {
private SerialClass[] data;
private Context context;
private static final String ACTIVITY_TAG = "MyAdapter";
public MyAdapter(Context _context,SerialClass[] _data) {
this.context = _context;
this.data = _data;
}
@Override
public int getCount() {
return data.length;
}
public View getView(int position, View convertView, ViewGroup parent) {
View view = LayoutInflater.from(context).inflate(R.layout.item_list_view,null);
TextView nameView = (TextView) view.findViewById(R.id.name_textView);
nameView.setText(data[position].getName());
LinearLayout linearLayout = (LinearLayout) view.findViewById(R.id.textLinearLayout);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
for(int i=0;i<data[position].getSnValue().length;i++) {//根据snValue数组的长度动态加载TextView
TextView valueView = new TextView(context);
valueView.setText(data[position].getSnValue()[i]);
valueView.setLayoutParams(layoutParams);
linearLayout.addView(valueView);
}
String msg = "" + data[position].getSnValue().length;//int转String
Log.i(ACTIVITY_TAG,msg);
return view;
}
public long getItemId(int position) {
return 0;
}
public Object getItem(int position) {
return null;
}
}
MainActivity:
public class MainActivity extends AppCompatActivity {
private Context context;
private SerialClass[] snData = new SerialClass[3];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = MainActivity.this;
String[] str01 = {"0f:12:22:a1:ee"};
String[] str02 = {"test01","test02","test03"};
String[] str03 = {"str2","str1"};
for(int i=0;i<snData.length;i++) {//不知道有没有其他比较好的方式创建对象数组,采用了这种笨方法
snData[i] = new SerialClass();
}
snData[0].setName("mac:");
snData[0].setSnValue(str01);
snData[1].setName("sn:");
snData[1].setSnValue(str02);
snData[2].setName("android_id:");
snData[2].setSnValue(str03);
ListView listView = (ListView) findViewById(R.id.list_view);
MyAdapter myAdapter = new MyAdapter(context,snData);
listView.setAdapter(myAdapter);
}
}
activity_main:
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/list_view />