ListView

public class MainActivity extends Activity implements OnItemClickListener {
    private ArrayAdapter<CharSequence> adapter;
    private ListView lv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv = (ListView) findViewById(R.id.lv);
        adapter = ArrayAdapter.createFromResource(this, R.array.test,
                android.R.layout.simple_list_item_single_choice);
        lv.setAdapter(adapter);
        lv.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        // TODO Auto-generated method stub
        Toast.makeText(getApplicationContext(),
                lv.getItemAtPosition(arg2).toString(), Toast.LENGTH_SHORT)
                .show();
    }

}
自定义ListItem每一行格式,注意此处自定义的listitem.xml中只能有一个textview用于输出显示数组,
public class MainActivity extends Activity implements OnItemClickListener {
    private ArrayAdapter<CharSequence> adapter;
    private ListView lv;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv = (ListView) findViewById(R.id.lv);
        adapter = new ArrayAdapter<CharSequence>(getApplicationContext(),
                R.layout.listitem, R.id.tv, getResources().getTextArray(
                        R.array.test));
        lv.setAdapter(adapter);
        lv.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        // TODO Auto-generated method stub
        Toast.makeText(getApplicationContext(),
                lv.getItemAtPosition(arg2).toString(), Toast.LENGTH_SHORT)
                .show();
    }

}

 



posted @ 2012-12-03 14:16  Mr.haox  阅读(340)  评论(2编辑  收藏  举报