元素的ListView的每一行内preserve状态 [英] preserve state of elements inside listview's each row

查看:133
本文介绍了元素的ListView的每一行内preserve状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用打气筒充气每一行设计了一个ListView和我已经改变convertView的元素的颜色在他们的单击事件。但滚动列表之后的元素亏这些变化。我如何preserve自己的状态,并重新present他们?

和还有一个查询在我的脑海里,点击提交按钮的时候,我怎么能计算出没有被点击了哪个时listItems的总数是多少?


下面是我的code:

 包scf.login;
进口android.app.Activity;
进口android.app.AlertDialog;
进口android.content.DialogInterface;
进口android.content.Intent;
进口android.graphics.Color;
进口android.os.Bundle;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.view.View.OnClickListener;
进口android.widget.BaseAdapter;
进口android.widget.Button;
进口android.widget.ListView;
进口android.widget.TextView;
进口android.widget.Toast;公共类firstsubActivity扩展活动
{//变量声明
ListView的lvSubCat;
意向行动;
按钮btnBack,btnSubmit按钮,btnNext;
布尔提交= FALSE;
整数选中= 0;
的String []子状态= {,OK,,,,OK,,,警报,,,,经过, ,,OK,,,警报,};//定义上的点击监听器
android.view.View.OnClickListener lstnr =新android.view.View.OnClickListener()
{    @覆盖
    公共无效的onClick(视图v)
    {
    开关(v.getId()){    案例R.id.btnBack:
            Toast.makeText(getApplicationContext(),后退按钮点击Toast.LENGTH_SHORT).show();
        打破;
    案例R.id.btnSubmit:
            如果(!提交)
            {                checkcompletion();                如果(未选中大于0)
                {
                    Toast.makeText(firstsubActivity.this,完整的precheking,Toast.LENGTH_LONG +未选中+灰色颜色突出显示的干预(S)).show();
                }
                其他
                {
                    Toast.makeText(firstsubActivity.this,提交成功!,Toast.LENGTH_SHORT).show();
                    提交= TRUE;
                }            }
            其他
            {
                updatecofirmation();
            }
        打破;
    案例R.id.btnNext:
            如果(提交)
            {
                nextconfirmation();
            }
            其他
            {
                Toast.makeText(firstsubActivity.this,提交答案,Toast.LENGTH_SHORT).show();
            }
        打破;    }}    私人无效nextconfirmation()
    {
        AlertDialog.Builder建设者=新AlertDialog.Builder(firstsubActivity.this);
        builder.setCancelable(假);
        builder.setTitle(确认消息);
        builder.setMessage(你要离开这个页面吗?);
        builder.setPositiveButton(是,新DialogInterface.OnClickListener(){            @覆盖
            公共无效的onClick(DialogInterface对话,诠释它){
                // TODO自动生成方法存根                意图动作=新意图(firstsubActivity.this,cat_subcatActivity.class);
                startActivity(动作);
                }
        });
        builder.setNegativeButton(否,新DialogInterface.OnClickListener(){            @覆盖
            公共无效的onClick(DialogInterface对话,诠释它){
                // TODO自动生成方法存根
                Toast.makeText(firstsubActivity.this,不点击Toast.LENGTH_SHORT).show();            }
        });        builder.create()显示()。
    }
    私人无效updatecofirmation()
    {
        AlertDialog.Builder builder2 =新AlertDialog.Builder(firstsubActivity.this);
        builder2.setCancelable(假);
        builder2.setTitle(确认消息);
        builder2.setMessage(你要更新的答案?);
        builder2.setPositiveButton(是,新DialogInterface.OnClickListener(){            @覆盖
            公共无效的onClick(DialogInterface对话,诠释它){
                // TODO自动生成方法存根
                提交= FALSE;
                btnSubmit.performClick();
            }
        });
        builder2.setNegativeButton(否,新DialogInterface.OnClickListener(){            @覆盖
            公共无效的onClick(DialogInterface对话,诠释它){
                // TODO自动生成方法存根
                Toast.makeText(firstsubActivity.this,不点击Toast.LENGTH_SHORT).show();            }
        });        builder2.create()显示()。
    }
    私人无效checkcompletion()
    {
        未选中= subStatus.length;
        尝试
        {
            对于(整数j = 0; J< subStatus.length; J ++)
            {
                // CONFUSED
            }        }
        赶上(例外五){
            e.printStackTrace();
        }
        最后
        {        }    }
};@覆盖
保护无效的onCreate(捆绑savedInstanceState)
{
    // TODO自动生成方法存根
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.firstsub);    //捆绑
    lvSubCat =(ListView控件)findViewById(R.id.lvSubCat);
    btnBack =(按钮)findViewById(R.id.btnBack);
    btnSubmit按钮=(按钮)findViewById(R.id.btnSubmit);
    btnNext =(按钮)findViewById(R.id.btnNext);    //设置监听器
    btnBack.setOnClickListener(lstnr);
    btnSubmit.setOnClickListener(lstnr);
    btnNext.setOnClickListener(lstnr);
    //子类别的数据,并从数据库及其状态
    的String [] = SUBCAT {\"zero\",\"first\",\"second\",\"third\",\"forth\",\"fifth\",\"sixth\",\"seventh\",\"eighth\",\"ninth\",\"tenth\",\"eleventh\",\"twelfth\",\"thirteenth\",\"forteenth\",\"fifteenth\",\"sixteenth\",\"seventeenth\",\"eighteenth\",\"ninteenth\"};
    的String []子状态= {,OK,,,,OK,,,警报,,,,经过, ,,OK,,,警报,};
    / *
    对于(整数i = 0; I< subStatus.length;我++)
    {
        设置每个子类别的状态
    }
    * /    //设置适配器和结合衔接的意见
    MySimpleAdapter适配器1 =新MySimpleAdapter(这一点,SUBCAT);
    lvSubCat.setAdapter(适配器1);
}//定义firstsubActivity类内部定制MySimpleAdapter类
公共类MySimpleAdapter延伸BaseAdapter
{
    私人最终活动范围内;
    私人最终的String [] SUBCAT;
    公共MySimpleAdapter(Activity上下文,字符串[] SUBCAT)
    {
        超();
        this.context =背景;
        this.subCat = SUBCAT;
    }
/ *
    保护类viewHolder
    {
        TextView的tvOk;
        按钮btnChecked,btnAlert;
    }
* /
    @覆盖
    公众诠释的getCount(){
        // TODO自动生成方法存根
        返回subCat.length;
    }
    @覆盖
    公共对象的getItem(INT为arg0){
        // TODO自动生成方法存根
        返回null;
    }
    @覆盖
    众长getItemId(INT为arg0){
        // TODO自动生成方法存根
        返回0;
    }    @覆盖
    公共查看getView(最终诠释的立场,观点convertView,父母的ViewGroup)
    {//最后viewHolder持有人=新viewHolder();        //膨胀
        LayoutInflater吹气= context.getLayoutInflater();
        convertView = inflater.inflate(R.layout.lstinfl,NULL);
        //捆绑
            最终的TextView tvOk =(TextView中)convertView.findViewById(R.id.tvOk);
            最终按钮btnChecked =(按钮)convertView.findViewById(R.id.btnChecked);
            最终按钮btnAlert =(按钮)convertView.findViewById(R.id.btnAlert);        //插入子
        tvOk.setText(SUBCAT [位置]);        //重新$ P $根据自己的状态psenting小类
        尝试
        {
            对于(整数k = 0; K< subStatus.length; k ++)
            {
                // CONFUSED            }        }
        赶上(例外五){
            // TODO:处理异常
        }
        最后
        {        }        //监听器来改变颜色和状态
        tvOk.setOnClickListener(新OnClickListener(){            @覆盖
            公共无效的onClick(查看为arg0){
                tvOk.setClickable(假);
                btnChecked.setClickable(真);
                btnAlert.setClickable(真);
                tvOk.setBackgroundColor(Color.GREEN);
                btnChecked.setBackgroundColor(Color.WHITE);
                btnAlert.setBackgroundColor(Color.WHITE);
                子状态[位置] =确定;
            }
        });        btnChecked.setOnClickListener(新OnClickListener(){            @覆盖
            公共无效的onClick(视图v){
                tvOk.setClickable(真);
                btnChecked.setClickable(假);
                btnAlert.setClickable(真);
                tvOk.setBackgroundColor(Color.BLACK);
                btnChecked.setBackgroundColor(Color.YELLOW);
                btnAlert.setBackgroundColor(Color.WHITE);
                子状态[位置] =选中;
//动作=新意图(getApplicationContext(),subdescActivity.class);
// startActivity(动作);            }
        });        btnAlert.setOnClickListener(新OnClickListener(){            @覆盖
            公共无效的onClick(视图v){
                tvOk.setClickable(真);
                btnChecked.setClickable(真);
                btnAlert.setClickable(假);
                tvOk.setBackgroundColor(Color.BLACK);
                btnChecked.setBackgroundColor(Color.WHITE);
                btnAlert.setBackgroundColor(Color.RED);
                子状态[位置] =警报;
//动作=新意图(getApplicationContext(),subdescActivity.class);
// startActivity(动作);
            }
        });
        返回convertView;
    }}
}


解决方案

好吧,你节省了阵列两种状态(文本和状态),所以至少这部分是好的。现在,你需要检查状态[] 在getView()来设置根据它的按钮正确的外观和感觉,是这样的:

 如果(子状态[位置] .equals(OK)){
            tvOk.setClickable(假);
            btnChecked.setClickable(真);
            btnAlert.setClickable(真);
            tvOk.setBackgroundColor(Color.GREEN);
            btnChecked.setBackgroundColor(Color.WHITE);
            btnAlert.setBackgroundColor(Color.WHITE);
            子状态[位置] =确定;
          }
          ....

然后当你提交你可以通过这些状态的项目,看看有什么被点击或没有。

I have designed a listview by inflating each row using inflater and I have changed color of convertView's elements on their click events. But after scrolling the list the elements loss these changes. How can I "preserve" their state and represent them?

And there is one more query in my mind, when clicking the submit button, how can I calculate the total number of "listitems" which had not been clicked?

Here is my code:

package scf.login;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class firstsubActivity extends Activity
{

//variable declaration
ListView lvSubCat;
Intent action;
Button btnBack,btnSubmit,btnNext;
Boolean submission=false;
Integer unchecked=0;
String[] subStatus = {"","Ok","","","","Ok","","","Alert","","","","Checked","","","Ok","","","Alert",""};

// defining on click listener
android.view.View.OnClickListener lstnr = new android.view.View.OnClickListener() 
{

    @Override
    public void onClick(View v) 
    {
    switch (v.getId()) {

    case R.id.btnBack:
            Toast.makeText(getApplicationContext(), "Back button clicked", Toast.LENGTH_SHORT).show();
        break;
    case R.id.btnSubmit:
            if(!submission)
            {

                checkcompletion();

                if(unchecked>0)
                {
                    Toast.makeText(firstsubActivity.this, "Complete the precheking of "+unchecked+" intervention(s) highlighted by Gray colour", Toast.LENGTH_LONG).show();
                }
                else
                {
                    Toast.makeText(firstsubActivity.this,"Successfully submitted !!",Toast.LENGTH_SHORT).show();                        
                    submission=true;
                }

            }
            else
            {
                updatecofirmation();
            }
        break;
    case R.id.btnNext:
            if(submission)
            {
                nextconfirmation();
            }
            else
            {
                Toast.makeText(firstsubActivity.this,"Submit the answers",Toast.LENGTH_SHORT).show();
            }
        break;

    }

}

    private void nextconfirmation() 
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(firstsubActivity.this);
        builder.setCancelable(false);
        builder.setTitle("Confirmation message");
        builder.setMessage("Do you want to leave this page ?");
        builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub

                Intent action = new Intent(firstsubActivity.this,cat_subcatActivity.class);
                startActivity(action);      
                }
        });
        builder.setNegativeButton("No", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                Toast.makeText(firstsubActivity.this, "No clicked", Toast.LENGTH_SHORT).show();

            }
        });

        builder.create().show();
    }


    private void updatecofirmation() 
    {
        AlertDialog.Builder builder2 = new AlertDialog.Builder(firstsubActivity.this);
        builder2.setCancelable(false);
        builder2.setTitle("Confirmation message");
        builder2.setMessage("Do you want to update answers ?");
        builder2.setPositiveButton("Yes", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                submission=false;
                btnSubmit.performClick();
            }
        });
        builder2.setNegativeButton("No", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                Toast.makeText(firstsubActivity.this, "No clicked", Toast.LENGTH_SHORT).show();

            }
        });

        builder2.create().show();
    }


    private void checkcompletion() 
    {
        unchecked=subStatus.length;
        try
        {
            for(Integer j=0;j<subStatus.length;j++)
            {
                // CONFUSED
            }

        }


        catch (Exception e) {
            e.printStackTrace();
        }                   
        finally
        {

        }



    }


};

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

    //Binding
    lvSubCat = (ListView)findViewById(R.id.lvSubCat);
    btnBack = (Button)findViewById(R.id.btnBack);
    btnSubmit=(Button)findViewById(R.id.btnSubmit);
    btnNext = (Button)findViewById(R.id.btnNext);

    //setting listeners
    btnBack.setOnClickListener(lstnr);
    btnSubmit.setOnClickListener(lstnr);
    btnNext.setOnClickListener(lstnr);


    //data of subcategories and their status from database
    String[] subCat = {"zero","first","second","third","forth","fifth","sixth","seventh","eighth","ninth","tenth","eleventh","twelfth","thirteenth","forteenth","fifteenth","sixteenth","seventeenth","eighteenth","ninteenth"};
    String[] subStatus = {"","Ok","","","","Ok","","","Alert","","","","Checked","","","Ok","","","Alert",""};
    /*
    for(Integer i=0;i<subStatus.length;i++)
    {
        set the status of each subcategory
    }
    */

    //setting adapter and binding adapter to the views
    MySimpleAdapter adapter1 = new MySimpleAdapter(this,subCat);
    lvSubCat.setAdapter(adapter1);
}

//defining customised MySimpleAdapter class inside firstsubActivity class
public class MySimpleAdapter extends BaseAdapter 
{
    private final Activity context;
    private final String[] subCat;


    public MySimpleAdapter(Activity context, String[] subCat) 
    {
        super();
        this.context = context;
        this.subCat = subCat;
    }
/*      
    protected class viewHolder
    {
        TextView tvOk;
        Button btnChecked,btnAlert;
    }
*/
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return subCat.length;
    }
    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }
    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) 
    {

//      final viewHolder holder = new viewHolder();

        //inflating
        LayoutInflater inflater = context.getLayoutInflater();
        convertView = inflater.inflate(R.layout.lstinfl,null);


        //binding
            final TextView tvOk=(TextView)convertView.findViewById(R.id.tvOk);
            final Button btnChecked=(Button)convertView.findViewById(R.id.btnChecked);
            final Button btnAlert=(Button)convertView.findViewById(R.id.btnAlert);

        //inserting subcategories
        tvOk.setText(subCat[position]);

        //representing subcategories according to their status
        try
        {
            for(Integer k=0;k<subStatus.length;k++)
            {
                // CONFUSED

            }

        }
        catch (Exception e) {
            // TODO: handle exception
        }
        finally
        {

        }

        //listeners to change the Color and Status
        tvOk.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                tvOk.setClickable(false);
                btnChecked.setClickable(true);
                btnAlert.setClickable(true);
                tvOk.setBackgroundColor(Color.GREEN);
                btnChecked.setBackgroundColor(Color.WHITE);
                btnAlert.setBackgroundColor(Color.WHITE);
                subStatus[position]="Ok";
            }
        });

        btnChecked.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                tvOk.setClickable(true);
                btnChecked.setClickable(false);
                btnAlert.setClickable(true);
                tvOk.setBackgroundColor(Color.BLACK);
                btnChecked.setBackgroundColor(Color.YELLOW);
                btnAlert.setBackgroundColor(Color.WHITE);
                subStatus[position]="Checked";
//                  action = new Intent(getApplicationContext(), subdescActivity.class);
//                  startActivity(action);

            }
        });

        btnAlert.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                tvOk.setClickable(true);
                btnChecked.setClickable(true);
                btnAlert.setClickable(false);
                tvOk.setBackgroundColor(Color.BLACK);
                btnChecked.setBackgroundColor(Color.WHITE);
                btnAlert.setBackgroundColor(Color.RED);
                subStatus[position]="Alert";
//                  action = new Intent(getApplicationContext(), subdescActivity.class);
//                  startActivity(action);
            }
        });


        return convertView;


    }   

}
}

解决方案

Well, you're saving both states (text and status) in arrays, so at least that part is good. Now you need to check the states[] in your getView() to set the correct look and feel of your buttons according to it, something like:

          if (subStatus[position].equals("Ok")) {
            tvOk.setClickable(false);
            btnChecked.setClickable(true);
            btnAlert.setClickable(true);
            tvOk.setBackgroundColor(Color.GREEN);
            btnChecked.setBackgroundColor(Color.WHITE);
            btnAlert.setBackgroundColor(Color.WHITE);
            subStatus[position]="Ok";
          }
          ....

Then when you submit you can go through these status items and see what was clicked or not.

这篇关于元素的ListView的每一行内preserve状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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