问题出在switch
声明中,因为我无法让ListView
项目位置显示任何内容。然后我必须使用 webview 来使用ListView
. 我的代码有什么问题?
public class Main extends Activity {
private ListView mListView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayList<MyData> myDataList = new ArrayList<MyData>();
MyData one = new MyData("Text one", 1);
MyData two = new MyData("Text two", 2);
MyData three = new MyData("Text three", 3);
MyData four = new MyData("Text four", 4);
MyData five = new MyData("Text five", 5);
MyData six = new MyData("Text six", 6);
MyData seven = new MyData("Text seven", 7);
MyData eight = new MyData("Text eight", 8);
myDataList.add(one);
myDataList.add(two);
myDataList.add(three);
myDataList.add(four);
myDataList.add(five);
myDataList.add(six);
myDataList.add(seven);
myDataList.add(eight);
mListView = (ListView) findViewById(R.id.listview_);
mListView.setAdapter(new MyListAdapter(this, R.layout.row, myDataList));
}
private class MyListAdapter extends ArrayAdapter<MyData> implements
OnClickListener {
public ArrayList<MyData> items;
public MyListAdapter(Context context, int textViewResourceId,
ArrayList<MyData> items) {
super(context, textViewResourceId, items);
this.items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.row, null);
}
MyData myData = items.get(position);
if (myData != null) {
TextView textViewTwo = (TextView) v
.findViewById(R.id.text_view_two);
if (textViewTwo != null) {
textViewTwo.setText(myData.getText());
// put the id to identify the item clicked
textViewTwo.setTag(myData.getId());
textViewTwo.setOnClickListener(this);
}
}
return v;
}
@SuppressLint("ShowToast")
@Override
public void onClick(View v) {
this switch statement is the real problem
switch (v.getId()) {
case 1:
Toast.makeText(v.getContext(), "text 1", Toast.LENGTH_SHORT);
break;
default:
Toast.makeText(getBaseContext(), "default", Toast.LENGTH_SHORT);
break;
}
}
}
private class MyData {
private String text;
private int id;
public MyData(String text, int id) {
this.text = text;
this.id = id;
}
public String getText() {
return text;
}
public int getId() {
return id;
}
}
}