change image of listivew's item permanently

倖福魔咒の 提交于 2019-12-24 22:39:37

问题


i am trying to change the image of the listview's item,if user select the item,its image change permanently .but with my code only single item is changed when user click on other item the previously changed item get changed to default and new selected item get change and so on.how to make this change permanently..??

MainActivity

List<RowItem> rowItems;
    private BaseClass adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Singleton.populate = new ArrayList<Singleton.ProfileBean>();
        setContentView(R.layout.fragment_main);

        lay1 = (LinearLayout) findViewById(R.id.lay1);
        lay2 = (LinearLayout) findViewById(R.id.lay2);

        lay2.setVisibility(View.VISIBLE);
        img = R.drawable.praying_hands_normal;

        rowItems = new ArrayList<RowItem>();
        for (int i = 0; i < 50; i++) {
            RowItem item = new RowItem(Dias[i], Prayers[i], img);
            rowItems.add(item);
        }



        listView = (ListView) findViewById(R.id.listview);

        adapter = new BaseClass(this, rowItems);
        listView.setAdapter(adapter);


        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // TODO Auto-generated method stub

                pos = position;

                Intent i = new Intent(MainActivity.this, OpenActivity.class);
                startActivity(i);

            }
        });

    }

BaseClass

public class BaseClass extends BaseAdapter {
        Context context;
        List<RowItem> rowItems;

        /* private view holder class */
        public class ViewHolder {
            public ImageView imageView;
            public TextView txtDias;
            public TextView txtPrayers;

        }

        public BaseClass( Context context,List<RowItem> rowItems) {
            // TODO Auto-generated constructor stub
            this.context = context;
            this.rowItems = rowItems;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;

            LayoutInflater mInflater = (LayoutInflater) context
                    .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.list, null);
                holder = new ViewHolder();

                holder.txtDias = (TextView) convertView.findViewById(R.id.dias);
                holder.txtPrayers = (TextView) convertView
                        .findViewById(R.id.prayers);
                holder.imageView = (ImageView) convertView
                        .findViewById(R.id.symbol);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            RowItem rowItem = (RowItem) getItem(position);

            holder.txtPrayers.setText(rowItem.getDesc());
            holder.txtDias.setText(rowItem.getTitle());
            holder.imageView.setImageResource(R.drawable.praying_hands_normal);
          if(pos == position && OpenActivity.a + OpenActivity.b +
                  OpenActivity.c == 1 || OpenActivity.a + OpenActivity.b +
                  OpenActivity.c == 2)
          {
              holder.imageView.setImageResource(R.drawable.praying_hands_bookmark);

          } 

          else if(pos == position && OpenActivity.a + OpenActivity.b +
                  OpenActivity.c == 3 )
          {
              holder.imageView.setImageResource(R.drawable.praying_hands_selected);
          }


          return convertView;

        }

OpenActivity

public class OpenActivity extends Activity implements OnClickListener {
    Button btn1, btn2, btn3;
    public static int a= 0,b=0,c=0;
OpenActivity Instance;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.openactivity);

        btn1 = (Button) findViewById(R.id.button1);
        btn2 = (Button) findViewById(R.id.button2);
        btn3 = (Button) findViewById(R.id.button3);
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);




    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if (v == btn1) {
            Intent i = new Intent(this, DFamily.class);
            startActivity(i);
            a =1;

            Toast.makeText(getApplication(), "BTN", Toast.LENGTH_SHORT).show();

        } else if (v == btn2) {
            Intent a = new Intent(this, DIndividual.class);
            startActivity(a);
            b =1;
            Toast.makeText(getApplication(), String.valueOf(b), Toast.LENGTH_SHORT).show();
    } else if (v == btn3) {
            Intent x = new Intent(this, DGroup.class);
            startActivity(x);
            c =1;
            Toast.makeText(getApplication(), "BTN2", Toast.LENGTH_SHORT).show();
        }

    }

回答1:


I think you need a new adapter. Items that come from a source, that way you can set that items image using a setimage() method. That way its not the listviewitem that gets updated its the item itself. Do you understand?



来源:https://stackoverflow.com/questions/24781709/change-image-of-listivews-item-permanently

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!