onclick无法在listview中工作:切换案例错误

时间:2013-05-12 06:38:51

标签: android onclick listviewitem

问题出在switch语句中,因为我无法获得ListView项目位置来显示任何内容。然后,我必须使用webview使用ListView打开html文档。我的代码出了什么问题?

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;
    }
}
}

4 个答案:

答案 0 :(得分:1)

ListView中的

使用 OnItemClickListener 来实现列表项单击:

YourList.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3) 
{
   //Your Code Here
}
});

答案 1 :(得分:1)

更改

switch (v.getId()) {
       case 1:
                    Toast.makeText(v.getContext(), "text 1", Toast.LENGTH_SHORT);
                    break;


                default:
                    Toast.makeText(getBaseContext(), "default", Toast.LENGTH_SHORT);
                    break;
            }

这个

switch (v.getId()) {
                case R.id.text_view_two:
                    Toast.makeText(getBaseContext(), "text 1", Toast.LENGTH_SHORT);
                    break;                    
            }
像这样。因为v.getId()始终返回View的ID。因此,您必须使用您的View ID进行检查。

我希望这会对你有所帮助。

答案 2 :(得分:1)

这个非常好的问题......所有其他人的答案都是错误的...任何人都给出了正确的答案,否则就不会给像这样的废话

switch(postion)
{
case 1:
...
Case 2:
.....
}

答案 3 :(得分:0)

您应首先为视图设置ID:

@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);

        v.setId(items.get(position).getId()); //<- this
    }
}