本文介绍了CheckedTextView设置为列表视图中选中的OnCreate?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个shoppinglist,其中CheckedTextView进行检查,如果你已经选定了项目previously即项目都有一个布尔值设置为true,如果采摘。

我已经重写了getview方法用的setText()和工程变更CheckedTextView文本,但设置在CheckedTextView的setChecked(true)方法犯规不让它检查。

我知道,我不是回收getview(任何意见),这将是该项目以后的项目中,右键知道我需要看到一个绿色的对勾=)

有什么想法?请大家帮忙。

我的XML文件:

的test.xml    

 < CheckedTextView的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:ID =@ + ID / text1中
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =机器人:ATTR /列表preferredItemHeight
    机器人:textAppearance =机器人:ATTR / textAppearanceLarge
    机器人:重力=center_vertical
    机器人:对号=机器人:ATTR / textCheckMark
    机器人:以下属性来=5dip
    机器人:paddingRight =50dip
    机器人:文字颜色=#000000
    机器人:后台=#FFFFFF
    机器人:标签=text1中
    机器人:文本=testtestetstt
    机器人:可聚焦=FALSE/>
 

listfippel.java

 包nu.listfippel.mitt;

进口的java.util.ArrayList;
进口java.util.Arrays中;
进口的java.util.List;

进口android.app.ListActivity;
进口android.content.Context;
进口android.graphics.Color;
进口android.graphics.Typeface;
进口android.os.Bundle;
进口android.text.Spannable;
进口android.text.style.StrikethroughSpan;
进口android.text.style.StyleSpan;
进口android.view.Gravity;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.AdapterView;
进口android.widget.ArrayAdapter;
进口android.widget.BaseAdapter;
进口android.widget.CheckBox;
进口android.widget.CheckedTextView;
进口android.widget.ListAdapter;
进口android.widget.TextView;
进口android.widget.Toast;
进口android.widget.AdapterView.OnItemClickListener;

公共类listfippel扩展ListActivity {

    // ArrayList的< Ingred>荷兰国际集团;
    Ingred的i1 =新Ingred(巴南,2,1,真);
    Ingred I2 =新Ingred(苹果,3,1,FALSE);
    Ingred I3 =新Ingred(päron,3,1,真正的);
    Ingred I4 =新Ingred(Grot的,3,1,FALSE);
    Ingred I5 =新Ingred(布罗德,3,1,FALSE);
    Ingred I6 =新Ingred(kyckling,3,1,FALSE);
    Ingred I7 =新Ingred(罗德红辣椒,3,1,FALSE);
    Ingred I8 =新Ingred(tårta,3,1,FALSE);
    Ingred I9 =新Ingred(bappelsin,3,1,FALSE);
    Ingred I10 =新Ingred(gurka,3,1,真正的);
    CheckedTextView CTV;
    Ingred [] ingarray = {I1,I2,I3,I4,I5,I6,I7,I8,I9,I10};


    Ingadapter适配器;
    Ingred温度;

    / **第一次创建活动时调用。 * /
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.list);


        ArrayList的< Ingred> ING =新的ArrayList< Ingred>(Arrays.asList(ingarray));

        适配器=新Ingadapter(这一点,R.layout.test,荷兰国际集团);
        //适配器=新IngArrayAdapter(荷兰国际集团);

        OnItemClickListener IL =新OnItemClickListener(){

            @覆盖
            公共无效onItemClick(适配器视图<>母公司,查看ARG1,INT指数,
                    长ARG3){

                临时=(Ingred)parent.getItemAtPosition(指数);


                如果(temp.getCheck()){
                    吐司面包= Toast.makeText(getApplicationContext()
                            杜avmarkerade nyss书房\ nklarmarkerade瓦兰+ parent.getItemAtPosition(指数)的ToString()
                            Toast.LENGTH_LONG);
                    toast.setGravity(Gravity.CENTER,0,0);
                    toast.show();
                }

                如果(!temp.getCheck()){
                    temp.setCheck(真正的);

                    //ctv.setChecked(true);
                }
                其他 {
                    temp.setCheck(假);
                }
                adapter.notifyDataSetChanged();
            }
        };


        setListAdapter(适配器);
        //setTitleColor(Color.RED);
        getListView()setOnItemClickListener(IL)。
        。getListView()setChoiceMode(2);



    }
    类Ingadapter扩展ArrayAdapter< Ingred> {

        公共Ingadapter(上下文的背景下,INT textViewResourceId,
                名单< Ingred>对象){
            超(背景下,textViewResourceId,对象);
            // TODO自动生成构造函数存根
        }


        @覆盖
        公共查看getView(INT位置,查看convertView,ViewGroup中父){
            // TODO自动生成方法存根
            LayoutInflater充气= getLayoutInflater();
            查看排= inflater.inflate(R.layout.test,NULL);
            row.setTag(R.id.text1,row.findViewById(R.id.text1));

            CheckedTextView CTV =(CheckedTextView)row.getTag(R.id.text1);

            Ingred临时=(Ingred)((适配器视图<>)母公司).getItemAtPosition(位置);

            如果(temp.getCheck()){
                ctv.setChecked(真正的);
            }

            ctv.setText(temp.toString());

            返回(行);
        }

    }

    }
 

Ingred.java

 包nu.listfippel.mitt;

公共类Ingred {

    私人字符串NAMN;
    @燮pressWarnings(未使用)
    私人诠释volym;
    私人双人安塔尔;
    私人布尔查= FALSE;
    私人字符串retstring;

    公共Ingred(字符串NAMN,双安塔尔,INT volym,布尔检查){
        this.namn = NAMN;
        this.antal =安塔尔;
        this.volym = volym;
        this.check =检查;
    }

    公共字符串的toString(){

        如果(!this.check){
        retstring = NAMN +\ N(+安塔尔+behövs);
        }
        其他 {
            retstring = NAMN;
        }
        返回retstring;
    }

    公共布尔getCheck(){
        返回this.check;
    }

    公共无效setCheck(布尔检查){
        this.check =检查;
    }
}
 

解决方案

如果您要使用 setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); ,则需要使用 setSelection()的ListView方法来表示其接受检查,不能处理自己在行结合。

I am trying to make a shoppinglist, where the CheckedTextView is checked if you have already picked your item previously ie the item has a boolean set to true if picked.

I have overriden the getview method to change the CheckedTextView text with setText() and that works but setting the setChecked(true) method in CheckedTextView doesnt not make it checked.

I know that I am not recycling any views in getview(), that will be a later project in the project, right know I need to see a green checkmark =)

Any thoughts? please help.

my xml files:

test.xml

 <CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text1"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:gravity="center_vertical"
    android:checkMark="?android:attr/textCheckMark"
    android:paddingLeft="5dip"
    android:paddingRight="50dip"
    android:textColor="#000000"
    android:background="#ffffff"
    android:tag="text1"
    android:text="testtestetstt"
    android:focusable="false"/>

listfippel.java

package nu.listfippel.mitt;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import android.app.ListActivity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.Spannable;
import android.text.style.StrikethroughSpan;
import android.text.style.StyleSpan;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CheckedTextView;
import android.widget.ListAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class listfippel extends ListActivity {

    //ArrayList<Ingred> ing;
    Ingred i1 = new Ingred("banan", 2, 1, true);
    Ingred i2 = new Ingred("äpple", 3, 1, false);
    Ingred i3 = new Ingred("päron", 3, 1, true);
    Ingred i4 = new Ingred("gröt", 3, 1, false);
    Ingred i5 = new Ingred("bröd", 3, 1, false);
    Ingred i6 = new Ingred("kyckling", 3, 1, false);
    Ingred i7 = new Ingred("röd paprika", 3, 1, false);
    Ingred i8 = new Ingred("tårta", 3, 1, false);
    Ingred i9 = new Ingred("bappelsin", 3, 1, false);
    Ingred i10 = new Ingred("gurka", 3, 1, true);
    CheckedTextView ctv;
    Ingred[] ingarray = { i1, i2, i3, i4, i5, i6, i7, i8, i9, i10 };


    Ingadapter adapter;
    Ingred temp;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list);


        ArrayList<Ingred> ing = new ArrayList<Ingred>(Arrays.asList(ingarray));

        adapter = new Ingadapter(this, R.layout.test, ing);
        //adapter = new IngArrayAdapter(ing);

        OnItemClickListener il = new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View arg1, int index,
                    long arg3) {

                temp = (Ingred)parent.getItemAtPosition(index);


                if (temp.getCheck()){
                    Toast toast = Toast.makeText(getApplicationContext(),
                            "Du avmarkerade nyss den\nklarmarkerade varan " + parent.getItemAtPosition(index).toString(),
                            Toast.LENGTH_LONG);
                    toast.setGravity(Gravity.CENTER, 0, 0);
                    toast.show();
                }

                if (!temp.getCheck()){
                    temp.setCheck(true);

                    //ctv.setChecked(true);
                }
                else {
                    temp.setCheck(false);
                }
                adapter.notifyDataSetChanged();
            }
        };


        setListAdapter(adapter);
        //setTitleColor(Color.RED);
        getListView().setOnItemClickListener(il);
        getListView().setChoiceMode(2);



    }
    class Ingadapter extends ArrayAdapter<Ingred>{

        public Ingadapter(Context context, int textViewResourceId,
                List<Ingred> objects) {
            super(context, textViewResourceId, objects);
            // TODO Auto-generated constructor stub
        }


        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            LayoutInflater inflater=getLayoutInflater();
            View row = inflater.inflate(R.layout.test, null);
            row.setTag(R.id.text1, row.findViewById(R.id.text1));

            CheckedTextView ctv =(CheckedTextView)row.getTag(R.id.text1);

            Ingred temp = (Ingred)((AdapterView<?>) parent).getItemAtPosition(position);

            if(temp.getCheck()){
                ctv.setChecked(true);
            }

            ctv.setText(temp.toString());

            return (row);
        }

    }

    }

Ingred.java

package nu.listfippel.mitt;

public class Ingred {

    private String namn;
    @SuppressWarnings("unused")
    private int volym;
    private double antal;
    private boolean check = false;
    private String retstring;

    public Ingred(String namn, double antal, int volym, boolean check){
        this.namn = namn;
        this.antal = antal;
        this.volym = volym;
        this.check = check;
    }

    public String toString(){

        if (!this.check){
        retstring = namn + " \n(" + antal + " behövs)";
        }
        else {
            retstring = namn;
        }
        return retstring;
    }

    public boolean getCheck(){
        return this.check;
    }

    public void setCheck(boolean check){
        this.check = check;
    }
}
解决方案

If you are going to use setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);, you need to use the setSelection() method on ListView to indicate which get checked, not handle that yourself during row binding.

这篇关于CheckedTextView设置为列表视图中选中的OnCreate?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-13 20:27