将listview选中的文本传递给另一个活动

时间:2011-12-05 20:35:46

标签: android listview

抱歉,在执行此操作时遇到问题,在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();
        }



    });

2 个答案:

答案 0 :(得分:1)

使用

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

答案 1 :(得分:0)

看起来你永远不会设置“值”,所以它仍然是null。我想这可能是你的问题。您可以在调用putExtra之前尝试设置该String值吗?

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

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