我是初学者,所以请放轻松。我尝试显示列表视图,当我点击此列表视图中的一个项目时显示为已选中。为什么listview没有出现,以及我怎么做 解决这个问题 。谢谢!
MainActivity.java:
public class MainActivity extends Activity {
CheckListActivity ck;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ck=new CheckListActivity();
}
CheckListActivity.java:
public class CheckListActivity extends ListActivity{
/**
* @param args
*/
String[] presidents = {
"Dwight D. Eisenhower",
"John F. Kennedy",
"Lyndon B. Johnson",
"Richard Nixon",
"Gerald Ford",
"Jimmy Carter",
"Ronald Reagan",
"George H. W. Bush",
"Bill Clinton",
"George W. Bush",
"Barack Obama"
};
ListView listview;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//list_view_rows
listview = (ListView)findViewById(R.id.listafisiere);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.list_view_rows, R.id.ListMea, presidents);
listview.setAdapter(adapter);
}
@Override
protected void onListItemClick(ListView parent, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(parent, v, position, id);
Toast.makeText(this, "You have selected " + presidents[position],
Toast.LENGTH_SHORT).show();
}
}
答案 0 :(得分:0)
你不需要2个活动来做到这一点。请尝试以下,它应该足够了:
public class MainActivity extends ListActivity {
String[] presidents = {
"Dwight D. Eisenhower",
"John F. Kennedy",
"Lyndon B. Johnson",
"Richard Nixon",
"Gerald Ford",
"Jimmy Carter",
"Ronald Reagan",
"George H. W. Bush",
"Bill Clinton",
"George W. Bush",
"Barack Obama"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.list_view_rows, R.id.ListMea, presidents);
setListAdapter(adapter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Toast.makeText(this, "You have selected " + presidents[position],
Toast.LENGTH_SHORT).show();
}
}
修改强>
从另一个开始一项活动:
Intent intent = new Intent(this, NewActivity.class);
startActivity(intent);
答案 1 :(得分:0)
我建议像这样设置适配器。
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, presidents);
listview.setAdapter(adapter);