code不延长ListViewActivity,但是拥有一个ListView [英] Code does not extend ListViewActivity, but does have a listview

查看:164
本文介绍了code不延长ListViewActivity,但是拥有一个ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有很多关于中的列表视图复选框职位,我可以诚实地说,读一大部分这些之后,就已经离开我困惑了伤害大脑。

I know that there are a lot of posts about checkboxes within Listviews and I can honestly say that after reading a large majority of these, it has left me confused with a hurting brain.

我的code不延长ListViewActivity,但确实有一个列表视图作为相对布局的tabhost内它坐落的一部分。

My code does not extend the ListViewActivity but does have a listview as part of a relative layout which sits within a tabhost.

我已经试过了可调焦=假都反对的TextView和复选框,TextView的行为,但仍复选框覆盖ListView项的焦点。

I have tried the focusable=false both against the textview and the checkbox, the textview behaves but the checkbox still overrides the focus of the listview item.

如果我只需要一个TextView的话,我会去为建于布局的机器人,但我需要至少6 textviews每行一个checkox。

If I only needed one textview then I would have gone for androids built in layout but I require at least 6 textviews with a checkox on each line.

我main.xml中是这样的:

My Main.xml is this :

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/tabhost_master"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent">
  <TabWidget 
    android:id="@android:id/tabs"
    android:layout_width="fill_parent"
    android:layout_height="65px"
     />  

<FrameLayout
   android:id="@android:id/tabcontent"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:paddingTop="65px">
<RelativeLayout
    android:id="@+id/tab1"
    android:orientation="vertical"
    android:layout_height="fill_parent"
    android:weightSum="1" android:gravity="top" android:layout_width="fill_parent">
    <TextView android:text="Morning Set Up" android:textAppearance="?android:attr/textAppearanceMedium" android:id="@+id/textView1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true"></TextView>
    <Button android:text="Set Time On" android:layout_height="wrap_content" android:id="@+id/startb" android:layout_width="wrap_content" android:layout_below="@+id/textView1" android:layout_alignParentLeft="true"></Button>
    <Button android:text="Set Time Off" android:layout_height="wrap_content" android:id="@+id/stopb" android:layout_width="wrap_content" android:layout_alignTop="@+id/startb" android:layout_alignParentRight="true"></Button>
    <Button android:layout_width="wrap_content" android:text="Set Days" android:id="@+id/mset_days" android:layout_height="wrap_content" android:layout_below="@+id/startb" android:layout_centerHorizontal="true"></Button>
    <Button android:id="@+id/confirm_m" android:layout_height="wrap_content" android:text="Confirm" android:layout_width="wrap_content" android:layout_alignTop="@+id/mset_days" android:layout_alignParentLeft="true"></Button>
    <Button android:id="@+id/cancel_m" android:layout_height="wrap_content" android:text="Cancel" android:layout_width="wrap_content" android:layout_alignTop="@+id/mset_days" android:layout_alignParentRight="true"></Button>
    <ListView android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/listView1" android:layout_below="@+id/mset_days" android:layout_alignParentLeft="true" android:layout_marginTop="34dp"></ListView>
</RelativeLayout>
<RelativeLayout
    android:id="@+id/tab2"
    android:orientation="vertical"
    android:layout_height="fill_parent"
    android:weightSum="1" android:gravity="top" android:layout_width="fill_parent">
    <TextView android:text="A Set Up" android:textAppearance="?android:attr/textAppearanceMedium" android:id="@+id/textView1_a" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true"></TextView>
    <Button android:text="Set Time On" android:layout_height="wrap_content" android:id="@+id/startb_a" android:layout_width="wrap_content" android:layout_below="@+id/textView1_a" android:layout_alignParentLeft="true"></Button>
    <Button android:text="Set Time Off" android:layout_height="wrap_content" android:id="@+id/stopb_a" android:layout_width="wrap_content" android:layout_alignTop="@+id/startb_a" android:layout_alignParentRight="true"></Button>
    <Button android:layout_width="wrap_content" android:text="Set Days" android:id="@+id/mset_days_a" android:layout_height="wrap_content" android:layout_below="@+id/startb_a" android:layout_centerHorizontal="true"></Button>
    <TextView android:layout_height="wrap_content" android:text="Date / Time : \n Day \n Time" android:id="@+id/dandt_a" android:layout_width="wrap_content" android:layout_below="@+id/mset_days_a" android:layout_alignRight="@+id/mset_days_a" android:layout_marginTop="30dp"></TextView>
    <TextView android:id="@+id/dandt2_a" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Along with this one as well" android:layout_below="@+id/dandt_a" android:layout_alignParentLeft="true" android:layout_marginLeft="14dp" android:layout_marginTop="44dp"></TextView>
    <TextView android:layout_height="wrap_content" android:text="To see if these text boxes clash" android:id="@+id/dandt3_a" android:layout_width="wrap_content" android:layout_centerVertical="true" android:layout_alignParentLeft="true"></TextView>
    <Button android:id="@+id/confirm_after_a" android:layout_height="wrap_content" android:text="Confirm" android:layout_width="wrap_content" android:layout_alignTop="@+id/mset_days_a" android:layout_alignParentLeft="true"></Button>
    <Button android:id="@+id/cancel_after_a" android:layout_height="wrap_content" android:text="Cancel" android:layout_width="wrap_content" android:layout_alignTop="@+id/mset_days_a" android:layout_alignParentRight="true"></Button>

</RelativeLayout>


</FrameLayout>
</TabHost>

我为ListView布局是这样的:

My layout for the listview is this :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
    <CheckBox android:text="Cancel ?" android:focusable="false"  android:checked="false" android:layout_width="wrap_content" android:id="@+id/checkBox1" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentLeft="true"></CheckBox>
    <TextView android:layout_width="wrap_content" android:focusable="false" android:id="@+id/time_disp" android:layout_height="wrap_content" android:text="TextView" android:layout_alignParentTop="true" android:layout_toRightOf="@+id/checkBox1" android:layout_marginLeft="16dp" android:layout_marginTop="15dp"></TextView>
    <CheckedTextView android:layout_width="wrap_content" android:focusable="false"  android:layout_height="wrap_content" android:text="CheckedTextView" android:id="@+id/checkedTextView1" android:layout_alignTop="@+id/time_disp" android:layout_alignParentRight="true" android:layout_marginRight="18dp"></CheckedTextView>

</RelativeLayout>

我的活动code是这样的:

My activity code is this :

package co.uk.sheildsatmax.layout_tester;



import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.Toast;
import android.widget.CheckBox;
public class Layout_testerActivity extends Activity  {
    /** Called when the activity is first created. */
    private String[] times = {"09:10","10:10","11:20","09:10","12:20","15:30","10:12"};
    ListView lv;
    CheckBox ticked;
    @Override

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TabHost tabhost=(TabHost)findViewById(R.id.tabhost_master);
        tabhost.setup();

        TabSpec tab1 = tabhost.newTabSpec("Tab1");
        morning_tab.setContent(R.id.tab1);
        morning_tab.setIndicator("Tab1 Setup");

        TabSpec tab2 = tabhost.newTabSpec("tab2");
        after_tab.setContent(R.id.tab2);
        after_tab.setIndicator("Tab2 Setup");

        tabhost.addTab(tab1b);
        tabhost.addTab(tab2);
        lv = (ListView)findViewById(R.id.listView1);
       // ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,R.layout.,R.id.time_disp,times);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.list_lay,R.id.time_disp,times);
        lv.setItemsCanFocus(false);        // this was another test attempt
        lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); // tried this and then realised it needed androids own layout to work

        lv.setAdapter(adapter);       

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                // TODO Auto-generated method stub
                Toast.makeText(getApplication().getApplicationContext(), "ID :"+Long.toString(arg3), Toast.LENGTH_LONG).show();


            }
        });




    }


}

现在我一直在阅读关于适配器,使正确的复选框的工作创造一个单独的类,但这个要求,如果是可能有人请张贴一个基本的例子对我来说,所有这些链接我看过有使用真糊涂我:

Now I have been reading about creating a separate class for the adapter to make the checkbox work correctly, but is this required and if so could someone please post a basic example for me to use as all of these links I have read have really confused me:

  • Question about onListItemClick in ListActivity
  • http://android-codes-examples.blogspot.com/2011/03/multiple-listview-and-custom-listview.html
  • Question about onListItemClick in ListActivity
  • http://www.anddev.org/extended_checkbox_list__extension_of_checkbox_text_list_tu-t5734.html
  • How to get Selected items from Multi Select List View
  • http://www.mubasheralam.com/tutorials/android/how-create-multiple-choice-list
  • http://appfulcrum.com/2010/09/12/listview-example-3-simple-multiple-selection-checkboxes/
  • http://www.anddev.org/advanced-tutorials-f21/listview-and-multiple-selection-t48402.html
  • Android ListView with Checkbox and all clickable
  • http://appfulcrum.com/2010/09/12/listview-example-3-simple-multiple-selection-checkboxes/
  • http://www.mousetech.com/blog/?p=74
  • http://www.vogella.de/articles/AndroidListView/article.html#singlemulti
  • http://www.vogella.de/articles/AndroidListView/article.html

正如你可以看到我已经看了不少,但他们更加困惑了我,没有人可以不listactivies例子。我在Android 1.6的写这个

As you can see I have read quite a few, but they have puzzled me even more and none seem to have examples without listactivies. I am writing this in android 1.6

推荐答案

现在修正这个问题,部分我的误解上的复选框如何与ListView和理解也是在ListView的行为也是如此。

Fixed this issue now , partly my misunderstanding on how the checkbox works with the listview and also understanding the behavior of the listview as well.

我最终使用在这个论坛上的这个线程中给出的例子 - > 管理多个复选框

I ended up using the example given on this thread at this forum -> managing multiple checkboxes

通过这个线程在这个论坛的帮​​助=> [点击列表视图线的时候滴答复选框] [2]

With the help of this thread on this forum => [Ticking checkboxes when clicking a listview line][2]

和下关闭屏幕滚动起来使用=>的保持在列表视图回收复选框状态

Finally got the listview to remember which checkbox is tick when scrolling up and down off the screen using => Keeping the checkbox state on listview recycle

这篇关于code不延长ListViewActivity,但是拥有一个ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆