我的布局有 13 个 TextView,点击后会更改 ListView 项。
这是我的活动:
public class ExampleActivity extends ListActivity implements
OnClickListener {
private String[] sa = new String[100];
private ListView lv;
private Context context = this;
private ArrayAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new LongOperation().execute("1");
lv = getListView();
}
private class LongOperation extends AsyncTask<String, Void, String> {
private ProgressDialog dialog = new ProgressDialog(
ExampleActivity.this);
@Override
protected String doInBackground(String... params) {
int i = Integer.parseInt(params[0]);
for (int n = 0; n < 100; n++) {
if (i != 5 && i != 10) {
sa[n] = "Item" + i;
} else {
}
}
return params[0];
}
@Override
protected void onPostExecute(String result) {
adapter = new ArrayAdapter<Object>(context,
android.R.layout.simple_list_item_1, sa);
lv.setAdapter(adapter);
this.dialog.dismiss();
}
@Override
protected void onPreExecute() {
this.dialog.setMessage("Please wait");
this.dialog.show();
}
@Override
protected void onProgressUpdate(Void... values) {
}
}
public void onClick(View v) {
Log.d("onClick", v.getId() + "**");
int id = v.getId();
switch (id) {
case R.id.tv1: {
new LongOperation().execute("1");
}
case R.id.tv2: {
new LongOperation().execute("2");
}
case R.id.tv3: {
new LongOperation().execute("3");
}
case R.id.tv4: {
new LongOperation().execute("4");
}
case R.id.tv5: {
new LongOperation().execute("5");
}
case R.id.tv6: {
new LongOperation().execute("6");
}
case R.id.tv7: {
new LongOperation().execute("7");
}
case R.id.tv8: {
new LongOperation().execute("8");
}
case R.id.tv9: {
new LongOperation().execute("9");
}
case R.id.tv10: {
new LongOperation().execute("10");
}
case R.id.tv11: {
new LongOperation().execute("11");
}
case R.id.tv12: {
new LongOperation().execute("12");
}
case R.id.tv13: {
new LongOperation().execute("13");
}
}
}
}
当我启动应用程序时,listView 填充为 item1。但是当我单击任何 TextViews 时,不会触发 onClick 方法。我使用日志检查了它。
谢谢你。