【问题标题】:Pass listview selected text to another activity将列表视图选定的文本传递给另一个活动
【发布时间】:2012-01-13 13:25:09
【问题描述】:

抱歉,伙计们无法正常工作,在 onclick 事件期间发生错误。我想将选定的文本传递回启动活动。我是新手,所以请放轻松:)

public class selectTee extends ListActivity{

    String[] tees_list;
    String value = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        tees_list = getResources().getStringArray(R.array.tees_array);
        setListAdapter(new ArrayAdapter<String>(this, R.layout.list_view, tees_list));
        final ListView teelist = getListView();
        teelist.setChoiceMode(1);
        teelist.setTextFilterEnabled(false);
        teelist.setOnItemClickListener(new OnItemClickListener(){

            public void onItemClick(AdapterView<?> teeAdapter, View arg1, int selectedInt, long selectedLong) {

                //Error occurs during the onclick event
                Intent data = new Intent();
                data.putExtra(value, selectedInt);
                setResult(RESULT_OK, data);
                finish();
            }
        });
    }
}

好的,我让它工作了,我更改了以下代码:

public void onActivityResult(int requestCode, int resultCode, Intent data) {

            if (requestCode == request_Code) {
            if (resultCode == RESULT_OK){
                Button revisedButton = (Button) findViewById(R.id.button1);
                String btext = data.getData().toString(); 
                revisedButton.setText((CharSequence) btext);

            }

            }
        }

以及返回活动

 public void onItemClick(AdapterView<?> teeAdapter, View arg1, int selectedInt, long selectedLong) {
            String selection  =((TextView) arg1).getText().toString();
            System.out.println(selection);
            Intent data = new Intent();
            Uri uri = Uri.parse(selection);
            System.out.println(uri);
            data.setData(uri);
            setResult(RESULT_OK, data);
            finish();
        }



    });

【问题讨论】:

  • 错误是什么。日志会有所帮助。
  • 12-05 17:47:40.781: ERROR/AndroidRuntime(1310): java.lang.RuntimeException: 传递结果失败 ResultInfo{who=null, request=1, result=-1, data= Intent { cmp=com.prophecysoftware.GolfProphecy/.SetupNewCourse (has extras) }} 到活动 {com.prophecysoftware.GolfProphecy/com.prophecysoftware.GolfProphecy.SetupNewCourse}: android.content.res.Resources$NotFoundException: String resource ID # 0xffffffff

标签: android listview


【解决方案1】:

使用

Intent intent = new Intent(getBaseContext(), YOUCLASS.class);
intent.putExtra("TEXT", selectedInt);
//startActivity(intent) if you want to start an activity when its clicked

【讨论】:

    【解决方案2】:

    看起来你从来没有设置“值”,所以它仍然是空的。我认为这可能是你的问题。您可以在调用 putExtra 之前尝试设置该字符串值吗?

    另外,从单击的视图中获取选定的文本。执行以下操作:

    String textOfSelectedItem  =((TextView) arg1).getText().toString();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多