从当前类的内部类中的方法访问导入到当前类中的类的方法

时间:2015-06-29 18:41:24

标签: java android

listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String str = ((TextView)view).getText().toString();
            int x = Integer.parseInt((String)listOutput.getText());
            listOutput.setText(Integer.toString(x+1));
            TextView t = (TextView)findViewById(R.id.textView);
            t.setText("hello");
            //Intent openTabbedActivity = new Intent(this,New_tabbed_activity.cl);
        }
      });

我想在setText()的{​​{1}}内使用TextView类的Intent类或Intent构造函数的onItemClick()方法AdapterView.OnItemClickListener

中的类

但Android工作室说 MainActivity 同样显示cannot resolve method setText

2 个答案:

答案 0 :(得分:0)

试试这段代码:

listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
        @Override 
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String str = ((TextView)view).getText().toString();
            int x = Integer.parseInt((String)listOutput.getText());
            view.listOutput.setText(Integer.toString(x+1));
            TextView t = (TextView)view.findViewById(R.id.textView);
            view.t.setText("hello");
            //Intent openTabbedActivity = new Intent(this,New_tabbed_activity.cl); 
        } 
      }); 

答案 1 :(得分:0)

通过浏览一些示例,我发现问题是context 在意图中,构造函数的第一个参数应该是MainActivity.this而不仅仅是this

listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String str = ((TextView)view).getText().toString();
        int x = Integer.parseInt((String)listOutput.getText());
        listOutput.setText(Integer.toString(x+1));
        TextView t = (TextView)findViewById(R.id.textView);
        MainActiviy.this.t.setText("hello");
        Intent openTabbedActivity = new Intent(MainActivity.this,New_tabbed_activity.cl);
    }
  });