2

问题出在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;
    }
}
}
4

4 回答 4

1

在 ListView 中使用OnItemClickListener来实现 List Item Click:

YourList.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3) 
{
   //Your Code Here
}
});
于 2013-05-12T06:42:47.763 回答
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

我希望这能帮到您。

于 2013-05-12T06:47:15.720 回答
1

这个很好的问题....所有其他人的答案都是错误的...任何人都可以为这个问题提供正确的答案,否则不要像这样胡说八道

switch(postion)
{
case 1:
...
Case 2:
.....
}
于 2013-06-05T11:42:20.430 回答
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
    }
}
于 2013-05-12T07:16:50.950 回答