0

我无法使用 putExtra() 方法在片段之间传递字符串。当我将一个字符串值从 listView 传递给另一个活动以在 textView 中查看该字符串值时,它什么也不显示。

这是我的代码。

public class As extends Fragment implements OnItemClickListener {

    String array[]= 
        {
            "Item 1 ...",
            "Item 2 ...",
            "Item 3 ...",
            "Item 4 ...",
            "Item 5 ...",
            "Item 6 ...",
            "Item 7 ...",
            "Item 8 ...",
            "Item 9 ..."
        };
    ListView lv;    

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.as, container, false);

        lv = (ListView)rootView.findViewById(R.id.item_list);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), R.layout.all_lists, R.id.list_text, array);
        lv.setAdapter(adapter); 
        lv.setOnItemClickListener(this);

        return rootView;
    }


    @Override
    public void onItemClick(AdapterView<?> adapt, View v, int position, long id) {


        Intent i = new Intent(getActivity(), ItemDetails.class).putExtra("TEXT", String.valueOf(position));
        startActivity(i);       
    }

}

现在这是我的第二个类,它使用字符串值在 TextView 中查看它。

public class ItemDetails extends Activity implements OnClickListener {

    TextView tv;
    ImageView right, left, back;

    public static String retrieve;

    String array[] = {
            "Item 1 ... Price: 2$ ... Available in NJ",
            "Item 2 ... Price: 3$ ... Available in NY",
            "Item 3 ... Price: 1$ ... Available in Australia",
            "Item 4 ... Price: 2.5$ ... Available in Paris",
            "Item 5 ... Price: 1.4$ ... Available in Melbourne",
            "Item 6 ... Price: 0.99$ ... Available in Cyprus",
            "Item 7 ... Price: 12$ ... Available in Carolina",
            "Item 8 ... Price: 3.6$ ... Available in Ohio",
            "Item 9 ... Price: 4$ ... Available Washington"
 };
    public static int IntValue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.as);


        tv = (TextView) findViewById(R.id.tv_item_details);
        if (tv == null) { Log.w("", "TextView is null");} 
        retrieve = getIntent().getStringExtra("TEXT");


        IntValue = Integer.parseInt(retreive);
        tv.setText(array[Integer.parseInt(retrieve)]);

        right = (ImageView) findViewById(R.id.right_arrow);
        left = (ImageView) findViewById(R.id.left_arrow);
        back = (ImageView) findViewById(R.id.back);

        right.setOnClickListener(this);
        left.setOnClickListener(this);
        back.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
        case R.id.right_arrow:

            IntValue = IntValue + 1;
            if(IntValue  == 9){
                IntValue = 0;
            }
            tv.setText(array[IntValue]);
            if (IntValue  == 8)
            {
                IntValue = -1;
            }

            break;

        case R.id.left_arrow:

            if(IntValue == 0){
                IntValue = 9;
            }
            IntValue = IntValue - 1;
            tv.setText(array[IntValue]);

            break;

        case R.id.back:
            this.finish();

            break;

        }
    }

}

当我单击一个列表项时,它应该出现在 ItemDetails 活动的 TextView“tv”中,但事实并非如此。它什么也没显示。任何想法 ?

4

2 回答 2

0

做这个

 @Override
public void onItemClick(AdapterView<?> adapt, View v, int position, long id) {


    Intent i = new Intent(getActivity(), ItemDetails.class).putExtra("TEXT", array[position]);
    startActivity(i);       
}
于 2013-07-17T13:52:09.100 回答
0

使用以下

Intent i = new Intent(getActivity(), ItemDetails.class).putExtra("TEXT", array[position].toString());
startActivity(i); 

取回

retrieve  = getIntent().getStringExtra("TEXT");
于 2015-06-02T14:59:54.923 回答