Listview OnItemClickListener不适用于Tabs

时间:2014-07-14 19:18:22

标签: android onclicklistener

我无法在以下代码中使用OnItemClickListener

tab1.setIndicator(Content);
        tab1.setContent(R.id.tab1);
        tabHost.addTab(tab1);
        lv=(ListView)findViewById(R.id.listP);
        adp = new ArrayAdapter<>(getApplicationContext(),R.layout.customlayout1, strArr.get(position));
        lv.setAdapter(adp);
        lv.setItemsCanFocus(false);
        adp.notifyDataSetChanged();

        lv.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
                // Do something 
                View vr =  lv.getChildAt(position);
                Toast.makeText(getApplicationContext(), "HI", 1000).show();

............... Toast没有显示出来。有一点要注意上一课(它的来电者)有相同的吐司工作'

以下是这些课程 主

公共类MainActivity扩展了ActionBarActivity {

private ListView lv;
private ArrayList<String> strArr;
private ArrayAdapter<String> adp;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TabHost tabHost = (TabHost) findViewById(R.id.mytabhost);
    tabHost.setup();

    TabSpec tab1=tabHost.newTabSpec("Products");
    tab1.setIndicator("Products");
    tab1.setContent(R.id.tab1);
    tabHost.addTab(tab1);
    lv = (ListView)findViewById(R.id.listP);
    strArr = new ArrayList<String>();
    strArr.add("Mobiles");
    strArr.add("TV");
    strArr.add("AC");
    strArr.add("Fridge");

    adp = new ArrayAdapter<>(getApplicationContext(),R.layout.customlayout1, strArr);
    lv.setAdapter(adp);
    adp.notifyDataSetChanged();

    lv.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
            // Do something 
            View vr =  lv.getChildAt(position);
            Toast.makeText(getApplicationContext(), "Clicked at" + position+" id "+id+" Content "+lv.getAdapter().getItem(position).toString(), Toast.LENGTH_SHORT).show();
            Intent it = new Intent(MainActivity.this,ListDetails.class);
            it.putExtra("Content", lv.getAdapter().getItem(position).toString());
            //it.putExtra("Story", story.get(index));
            it.putExtra("id", id);
            it.putExtra("position",position);
            startActivity(it); 
        }
    });


    //tab 2 etc...
    TabSpec tab2 = tabHost.newTabSpec("Coupons");
    tab2.setIndicator("Coupons");
    tab2.setContent(R.id.tab2);
    tabHost.addTab(tab2);

    TabSpec tab3 = tabHost.newTabSpec("What's Hot");
    tab3.setIndicator("What's Hot");
    tab3.setContent(R.id.tab3);
    tabHost.addTab(tab3);


    }


@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

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

}

发生错误的类

public class ListDetails extends ActionBarActivity {
    private ListView lv;
    private ArrayList<ArrayList<String>> strArr;
    private ArrayList<String> detailsM;
    private ArrayList<String> detailsTV;
    private ArrayList<String> detailsAC;
    private ArrayList<String> detailsFridge;

    private ArrayAdapter<String> adp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TabHost tabHost = (TabHost) findViewById(R.id.mytabhost);
        tabHost.setup();
        Intent intent = getIntent();
        String Content  = intent.getExtras().getString("Content");
        //lo id = intent.getExtras().getInt("id");
        int position=intent.getExtras().getInt("position");

        detailsM = new ArrayList<String>(Arrays.asList("Samsung","Nokia","HTC","Sony Xperia","Karbon","Lava","Spice"));
        detailsFridge = new ArrayList<String>(Arrays.asList("Samsung","Whirlpool","LG","Godrej"));
        detailsAC = new ArrayList<String>(Arrays.asList("Samsung","Voltas","LG","Haier","Napolean"));
        detailsTV = new ArrayList<String>(Arrays.asList("Samsung","Panasonic","LG","Videocon","Haier"));
        strArr =new ArrayList<ArrayList<String>>(); 
        strArr.add(detailsM);
        strArr.add(detailsTV);
        strArr.add(detailsAC);
        strArr.add(detailsFridge);

        TabSpec tab1=tabHost.newTabSpec(Content);
        tab1.setIndicator(Content);
        tab1.setContent(R.id.tab1);
        tabHost.addTab(tab1);
        lv=(ListView)findViewById(R.id.listP);
        adp = new ArrayAdapter<>(getApplicationContext(),R.layout.customlayout1, strArr.get(position));
        lv.setAdapter(adp);
        adp.notifyDataSetChanged();
        lv.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
                // Do something 
                View vr =  lv.getChildAt(position);
                Toast.makeText(getApplicationContext(), "Clicked at" + position+" id "+id+" Content "+lv.getAdapter().getItem(position).toString(), Toast.LENGTH_SHORT).show();
                Intent it = new Intent(ListDetails.this,ListDetails.class);
                it.putExtra("Content", lv.getAdapter().getItem(position).toString());
                //it.putExtra("Story", story.get(index));
                it.putExtra("id", id);
                it.putExtra("position",position);
                startActivity(it); 



            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.list_details, menu);
        return true;
    }

}

XML 这个xml用于两个类

 <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/mytabhost"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >


        <LinearLayout android:id="@+id/LinearLayout01"
            android:orientation="vertical" android:layout_height="fill_parent"
            android:layout_width="fill_parent">

            <TabWidget android:id="@android:id/tabs" 
                android:layout_height="wrap_content" android:layout_width="fill_parent"></TabWidget>

            <FrameLayout android:id="@android:id/tabcontent" 
                android:layout_height="fill_parent" android:layout_width="fill_parent">
                <LinearLayout android:orientation="vertical"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent" android:id="@+id/tab1"
                    >
                   <ListView
            android:id="@+id/listP"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
    android:layout_gravity="center_horizontal"         >
        </ListView>


                    <!-- tab 1 content goes here -->
                </LinearLayout>
                <LinearLayout android:orientation="vertical"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent" android:id="@+id/tab2"
                    >
                    <!-- tab 2 content goes here -->
                     <ListView
            android:id="@+id/listC"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:cacheColorHint="#00000000"
            />

                </LinearLayout>
                <LinearLayout android:orientation="vertical"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent" android:id="@+id/tab3">
                    <!-- tab 3 content goes here -->
                     <ListView
            android:id="@+id/listD"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:cacheColorHint="#00000000"
             />
                </LinearLayout>

            </FrameLayout>

        </LinearLayout>

    </TabHost>

0 个答案:

没有答案