自定义列表视图编辑值

时间:2013-03-19 10:15:06

标签: android listview

enter image description here enter image description here我在第二个活动中有EditText。这里输入的值将被添加到第一个活动的自定义列表视图中。

我在列表中的第一个活动我有textview,复选框和按钮(编辑)。 textview将来自第二个活动的edittext数据。所以这里如果我点击编辑然后它导航到特定数据的第二个活动.am现在得到所有这些..在第二个活动我想编辑文本域值。所以它必须在listview中显示编辑的值与此数据特别的一排。

public class MyApplication extends Application{


 ArrayList<String> arryList = new ArrayList<String>();  
  String cardNumberData=null;

}


public class Second extends Activity{

    EditText cardNumber;
    String cardNumberReceived;
    MyApplication app;  

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

    cardNumberReceived = getIntent().getStringExtra("cardwithoutstring");

    System.out.println("cardWithOutStringReceived"+cardNumberReceived);


 app = ((MyApplication) getApplicationContext());


 cardNumber =(EditText)findViewById(R.id.cardnumber); 

 cardNumber.setText(cardNumberReceived);     

Button save =(Button)findViewById(R.id.save);
    save.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

                app.cardNumberData =cardNumber.getText().toString();                
                System.out.println("Gotcardname"+app.cardNumberData);           


                 app.arryList.add(app.cardNumberData);


             System.out.println("Array List Size "+app.arryList.size());
             System.out.println("Array List Size "+app.cardTypeList.size());

            Intent saveIntent =new Intent(Second.this,First.class);

            startActivity(saveIntent);  

        }
    });


     }

}



public class First extends Activity{


    protected ListItemsState[] mDeletedItemsStates;
    protected ArrayAdapter<ListItemsState> mListAdapter;
    protected ListView mFoldersListView;
    protected Context mContext; 
    LayoutInflater lay;
    MyApplication app;  



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);     

        setContentView(R.layout.newcard);

        app = ((MyApplication) getApplicationContext());



        mDeletedItemsStates = (ListItemsState[])getLastNonConfigurationInstance();

        if (mDeletedItemsStates == null) {
            mDeletedItemsStates = new ListItemsState[app.arryList.size()];
            for (int i = 0; i < app.arryList.size(); i++) {


            mDeletedItemsStates[i] = new ListItemsState(app.arryList.get(i),i);

            }
        }
        ArrayList<ListItemsState> gridItemsList = new ArrayList<ListItemsState>();
        gridItemsList.addAll(Arrays.asList(mDeletedItemsStates));

        mListAdapter = new DeletedItemsStateArrayAdapter(this, gridItemsList);
        mFoldersListView.setAdapter(mListAdapter);
        mFoldersListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

        mFoldersListView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                Toast.makeText(getApplicationContext(), "am thelist",
                        Toast.LENGTH_LONG).show();
            }
        });




    }


    private static class ListItemsState {
        private String produ = "";  
        private boolean checked = false;
        private int position;       

        public ListItemsState(String produ, int position) {     
            this.position = position;           

        }

        public String getProdu() {
            return produ;
        }


        public int getPosition() {
            return position;
        }

        public boolean isChecked() {
            return checked;
        }

        public void setChecked(boolean checked) {
            this.checked = checked;
        }


    }

    /** Holds child views for one row. */
    private static class ListItemsStateViewHolder {
        private RadioButton checkBox;
        private TextView produ;
            private Button edit;        

        public TextView getProdu() {
            return produ;
        }               

        public Button getEdit() {
            return edit;
        }

        public RadioButton getCheckBox() {
            return checkBox;
        }


    }


    private class DeletedItemsStateArrayAdapter extends
            ArrayAdapter<ListItemsState> {

        private int mSelectedPosition = -1;
        private RadioButton mSelectedRB;

        private LayoutInflater inflater;
        public DeletedItemsStateArrayAdapter(Context context,
                List<ListItemsState> sentItemsStateList) {
            super(context, R.layout.customlist, R.id.card,
                    sentItemsStateList);

            // Cache the LayoutInflate to avoid asking for a new one each time.
            inflater = LayoutInflater.from(context);
        }

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


            final ListItemsState deletedItemsState = (ListItemsState) this
                    .getItem(position);


            ListItemsStateViewHolder viewHolder = new ListItemsStateViewHolder();
            // Create a new row view
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.customlist, null);

                convertView.setTag(new ListItemsStateViewHolder());


            }

            else {

                viewHolder = (ListItemsStateViewHolder) convertView.getTag();
                viewHolder.checkBox = viewHolder.getCheckBox();

                viewHolder.produ = viewHolder.getProdu();

                viewHolder.edit = viewHolder.getEdit();


            }


              viewHolder.produ = (TextView) convertView.findViewById(R.id.card);           
                      viewHolder.checkBox = (RadioButton) convertView.findViewById(R.id.radioButton1);                           

               viewHolder.edit=(Button)convertView.findViewById(R.id.editbutton);       


            try {
                viewHolder.checkBox.setTag(deletedItemsState);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }       



                viewHolder.edit.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View arg0) {


                Intent edit =new Intent(getApplicationContext(), Second.class);             

                  edit.putExtra("cardNumberSending",app.arryList.get(position));                    

                edit.putExtra("Indexvalue",mFoldersListView.getItemIdAtPosition(position));


        System.out.println("Index value :::::::::   "+mFoldersListView.getItemIdAtPosition(position));

                            startActivity(edit);                            

                    }
                });

            viewHolder.produ.setText(deletedItemsState.getProdu());     


            return convertView;
        }

    }

    public Object onRetainNonConfigurationInstance() {
        return mDeletedItemsStates;
    }
}

1 个答案:

答案 0 :(得分:0)

您正在将已修改的数据添加到ArrayList再次在Second Activity

中避免使用
app.cardNumberData = cardNumber.getText().toString();
if(arryList.indexOf(cardNumberReceived) != -1)
{
    app.arryList.set(arryList.indexOf(cardNumberReceived), app.cardNumberData);
}else
{
    app.arryList.add(app.cardNumberData);
}

在您的第二个Activity执行此操作onClick