list view
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
//删除分割线
android:divider="@null"
>
</ListView>
自定义适配器
新建类
public class Fruit {
private String name;
private String name2;
public Fruit(String name, String name2) {
this.name = name;
this.name2 = name2;
}
public String getName() {
return name;
}
public String getName2() {
return name2;
}
}
自定义适配器
public class Main3Activity extends AppCompatActivity {
public class FruitAdapter extends ArrayAdapter{
private final int resourceId;
public FruitAdapter(Context context, int textViewResourceId, List<Fruit> objects) {
super(context, textViewResourceId, objects);
resourceId = textViewResourceId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Fruit fruit = (Fruit) getItem(position);
View view = LayoutInflater.from(getContext()).inflate(resourceId,parent,false);
TextView fruitName = (TextView) view.findViewById(R.id.TV_L);
TextView fruitName2 = (TextView) view.findViewById(R.id.TV_R);
fruitName2.setText(fruit.getName2());
fruitName.setText(fruit.getName());
return view;
}
}
private List<Fruit> fruitList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
initFruits();
FruitAdapter adapter = new FruitAdapter(Main3Activity.this, R.layout.item, fruitList);
ListView listView = (ListView) findViewById(R.id.list_view);
listView.setAdapter(adapter);
}
private void initFruits() {
Fruit apple = new Fruit("**东\n9328\n新民市新农村镇黄家岭村","**梅\n3569\n辽中县大黑镇马家村");
fruitList.add(apple);
Fruit banana = new Fruit("**东\n1734\n辽中县大黑镇三尖村","**东\n9044\n辽中县老大房镇后鸡鸭房村");
fruitList.add(banana);
Fruit orange = new Fruit("**东\n5507\n辽中县老大房镇佳和村","**东\n3697\n辽中县老大房镇老观村");
fruitList.add(orange);
Fruit watermelon = new Fruit("**东\n33992\n康平县北三家子乡苇塘村","**东\n4157\n康平县北四家子乡北四家子");
fruitList.add(watermelon);
Fruit pear = new Fruit("**东\n3969\n康平县北四家子乡北四家子","**东\n1632\n康平县北四家子乡北四家子");
fruitList.add(pear);
Fruit grape = new Fruit("**东\n9328\n新民市新农村镇黄家岭村","**梅\n3569\n辽中县大黑镇马家村");
fruitList.add(grape);
Fruit pineapple = new Fruit("**东\n1734\n辽中县大黑镇三尖村","**东\n9044\n辽中县老大房镇后鸡鸭房村");
fruitList.add(pineapple);
Fruit strawberry = new Fruit("**东\n5507\n辽中县老大房镇佳和村","**东\n3697\n辽中县老大房镇老观村");
fruitList.add(strawberry);
Fruit cherry = new Fruit("**东\n3992\n康平县北三家子乡苇塘村","**东\n4157\n康平县北四家子乡北四家子");
fruitList.add(cherry);
Fruit mango = new Fruit("**生\n5000\n康平县北四家子乡北四家子","**东\n0070\n康平县北四家子乡北四家子");
fruitList.add(mango);
}
}
spinner
<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp">
</Spinner>
spinner java代码
spinner = (Spinner) findViewById(R.id.spinner);
list = new ArrayList<String>();
list.add("2017");
list.add("2018");
list.add("2019");
list.add("2020");
ArrayAdapter适配器
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter1);
跳转
Intent intent = new Intent(Main3Activity.this, Main2Activity.class);
startActivity(intent);