Android Spinner
Android
package com.example.java.m0926a;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout rl = new RelativeLayout(this);
Spinner sp = new Spinner(this);
List<String> list = new ArrayList<String>();
list.add(new String("日本語"));
list.add(new String("中国語"));
list.add(new String("韓国語"));
list.add(new String("英語"));
System.out.println(list.indexOf("b"));
mya mm = new mya(MainActivity.this, list);
sp.setAdapter(mm);
int a = ViewGroup.LayoutParams.MATCH_PARENT;
int b = ViewGroup.LayoutParams.WRAP_CONTENT;
rl.addView(sp, new RelativeLayout.LayoutParams(a,b));
// rl.addView(sp);
setContentView(rl);
}
public class abc {
View v = new RelativeLayout(MainActivity.this);
}
public class mya extends BaseAdapter{
private Context context = null;
private List<String>list = null;
public mya(Context context, List<String> list){
this.context = context;
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout ll = new LinearLayout(context);
TextView tx = new TextView(context);
tx.setText(list.get(position));
TextView tx2 = new TextView(context);
tx2.setText(" index = " + position);
ll.addView(tx);
ll.addView(tx2);
return ll;
}
}
}