从adroidstudio中的2个listviews中提取2个字符串

时间:2015-02-27 16:40:06

标签: android

我在1个活动中有2个列表视图(屏幕右侧左侧1个)。我希望从两个列表视图中提取2个字符串,并在代码中使用其他位置(在相同的活动中但不同的类或不同的活动)。我已经尝试将点击的项目分配给公共变量,然后将它们发布到文本视图位,我什么都没看到。请帮助或建议另一种更好的方法。以下是我的代码示例:

public class Tabs extends Activity {    
String CF =""; //Convert To
String CT =""; //Convert From
populateListView(); //function that populates my listview (not shown here)

ListView listMassFrom = (ListView) findViewById(R.id.ListViewMassFrom);
ListView listMassTo = (ListView) findViewById(R.id.ListViewMassTo);

listMassFrom.setOnItemClickListener(new AdapterView.OnItemClickListener(){
        @Override
        public void onItemClick(AdapterView<?> parent, View viewClicked, int position, long id) {

            String convFrom = ((TextView) viewClicked).getText().toString();
            CF = convFrom;
        }
    });



listMassTo.setOnItemClickListener(new AdapterView.OnItemClickListener(){

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

            String convTo = ((TextView) viewClicked).getText().toString();
            CT = convTo;
        }
    });


//Test to see if the two string were extracted from the onItemClick method:
    TextView t1 = (TextView) findViewById(R.id.test1);
    t1.setText(CT);

    TextView t2 = (TextView) findViewById(R.id.test2);
    t2.setText(CF);
   }
}

1 个答案:

答案 0 :(得分:0)

您应使用参数position通过适配器访问项目。

假设您的适配器包含Strings(否则使其适应您的数据模型):

public void onItemClick(AdapterView<?> parent, View viewClicked, int position, long id) {
    CF = <listMassFrom_adapter>.getItem(position);
}

编辑:

测试和工作的示例。选择项目时,您可以看到祝酒词。

package com.example;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.example.testbutton.R;

public class Tabs extends Activity {
    final static String[] MassUnits = { "kg", "g", "mg", "lb", "lbm", "slug" };
    String CF = ""; // Convert To
    String CT = ""; // Convert From
    ListView listMassFrom, listMassTo;
    ListAdapter adapterFrom, adapterTo;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tabs);

        // Find views
        listMassFrom = (ListView) findViewById(R.id.lvFrom);
        listMassTo = (ListView) findViewById(R.id.lvTo);

        // Create list of items
        adapterFrom = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, MassUnits);
        listMassFrom.setAdapter(adapterFrom);

        adapterTo = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, MassUnits);
        listMassTo.setAdapter(adapterTo);

        listMassFrom
            .setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent,
                        View viewClicked, int position, long id) {

                    String convFrom = (String) adapterFrom
                            .getItem(position);
                    Toast.makeText(getApplicationContext(),
                            "From: " + convFrom, Toast.LENGTH_SHORT).show();
                }
            });

        listMassTo
            .setOnItemClickListener(new AdapterView.OnItemClickListener() {

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

                    String convTo = (String) adapterTo.getItem(position);
                    Toast.makeText(getApplicationContext(),
                            "To: " + convTo, Toast.LENGTH_SHORT).show();
                }
            });
    }
}

布局tabs.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ListView
        android:id="@+id/lvFrom"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <ListView
        android:id="@+id/lvTo"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

</LinearLayout>

我认为最好使用微调器,但如果情况如此,你可以调整代码。