【问题标题】:How to run time update data from edit-text and spinner to database如何运行时将数据从编辑文本和微调器更新到数据库
【发布时间】:2016-04-23 05:40:25
【问题描述】:

我正在尝试将数据从编辑文本和微调器更新到我的数据库。但我在滚动列表视图并更新数据时发现了问题。我的数据滚动后波动,所以启用保存更新的数据。

下面是我的演示代码..请指出我哪里错了。

活动代码:(MainActivity.java)

public class MainActivity extends AppCompatActivity {

    private ListView listview;
    private MainActivity context;
    private newCustomDBAdapter adpter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String main[] = {"A", "B", "C", "D", "E", "F", "G", "H","i", "j", "k", "l"};
        String main1[] = {"Aa", "Bbb", "Cccc", "Ddddd", "Eeeeeee", "Fff", "G", "H","Eeeeeee", "Fff", "G", "H"};

        String values[] = {"A1", "B2", "C3", "D4", "E4", "F6", "G7", "H8","E4", "F6", "G7", "H8"};
        String values_temp[] = {"A1", "B2"};
        String valuesSpin[][] = {main, main, main, main1, main1, main1, main1, main,values_temp,values_temp,values_temp,values_temp};

        ArrayList<String> arraylist_item = new ArrayList<String>(Arrays.<String>asList(main));
        ArrayList<String> arraylist_item_values = new ArrayList<String>(Arrays.<String>asList(values));
        ArrayList<String> arraylist_item1 = new ArrayList<String>(Arrays.<String>asList(main1));
        List<List<String>> listOfLists = new ArrayList<List<String>>();

        listOfLists.add(arraylist_item1);
        listview = (ListView) findViewById(R.id.listview);
        context = this;
      adpter=  new newCustomDBAdapter(context, arraylist_item, arraylist_item_values, listOfLists,valuesSpin);
        listview.setAdapter(adpter);
    }

    public void Save(View view)
    {
        String s=adpter.main_hash_map.toString();
        Log.i("Valiue of has map- "," "+s);

    }

}

适配器代码:(newCustomDBAdapter.java)

public class newCustomDBAdapter extends BaseAdapter {


    private final String[][] valuesSpin;
    public boolean checkData;
    public int counter = 0;

    private String LVL;
    private Context CONTEXT;
    private List<String> ITEMS = new ArrayList<>();
    public static HashMap<Integer, String> main_hash_map= new HashMap<>();
    private List<List<String>> VALUES = new ArrayList<>();
    private List<String> TYPES = new ArrayList<>();

    private List<String> DATATITLE = new ArrayList<>();
    private List<String> DATAVALUE = new ArrayList<>();
    private String finalvalueInValue = "";
    private boolean checkUserComesInv;
    private String spinnerValue = "";
    private String sizeValue = "";
    private String final_value;
    private HashMap<Integer, Integer> mapRowSpinnerPos = new HashMap<>();


    public newCustomDBAdapter(Context context, List<String> items, List<String> type, List<List<String>> spinner_values, String[][] valuesSpin) {



        this.ITEMS = items;
        this.CONTEXT = context;

        this.valuesSpin = valuesSpin;
        this.TYPES = type;


        checkData = true;
        counter = 0;

    }


    @Override
    public int getCount() {

        if (ITEMS != null && ITEMS.size() != 0) {
            return ITEMS.size();
        }

        return 0;
    }

    @Override
    public Object getItem(int position) {
        return ITEMS.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

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

        final ViewHolder holder;
        String itemlevel = "";

        if (convertView == null) {

            holder = new ViewHolder();

            LayoutInflater newInflate = (LayoutInflater) CONTEXT.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = newInflate.inflate(R.layout.custom_new_db_adapter_layout, null);

            holder.editMain = (EditText) convertView.findViewById(R.id.custum_main_edit);
            holder.imgMain = (ImageView) convertView.findViewById(R.id.custum_main_img);

            holder.spinMain = (Spinner) convertView.findViewById(R.id.custum_main_spinner);
            holder.txtMain = (TextView) convertView.findViewById(R.id.custum_main_desc);

            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if ((ITEMS.get(position).equals("A") || ITEMS.get(position).equals("D") || ITEMS.get(position).equals("i") || ITEMS.get(position).equals("l"))) {
            holder.editMain.setVisibility(View.GONE);
            holder.editMain.setEnabled(false);
            holder.spinMain.setVisibility(View.VISIBLE);
            holder.spinMain.setEnabled(true);
        } else if (ITEMS.get(position).equals("B")) {
            holder.spinMain.setVisibility(View.VISIBLE);
            holder.editMain.setEnabled(true);
            holder.editMain.setVisibility(View.VISIBLE);
            holder.spinMain.setEnabled(true);
        } else {
            holder.editMain.setVisibility(View.VISIBLE);
            holder.editMain.setEnabled(true);            holder.spinMain.setVisibility(View.GONE);
            holder.spinMain.setEnabled(false);
        }


        holder.txtMain.setText(ITEMS.get(position));
        holder.editMain.setText(TYPES.get(position));
        holder.editMain.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int arg1, int arg2, int arg3) {


            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                                          int arg3) {
            }

            @Override
            public void afterTextChanged(Editable arg0) {
                main_hash_map.put(position, arg0.toString());
                Log.i("value edit- ",arg0.toString()+" position- "+position);

            }
        });
      /*  if (main_hash_map.containsKey(position)) {
            holder.editMain.setText(main_hash_map.get(position));
        }*/

        holder.spinMain.setAdapter(new ArrayAdapter<>(CONTEXT, android.R.layout.simple_spinner_dropdown_item, valuesSpin[position]));

        holder.spinMain.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int spinnerPosition, long id) {
                mapRowSpinnerPos.put(position, spinnerPosition);
                String textSpin = holder.spinMain.getSelectedItem().toString();
                main_hash_map.put(position,textSpin
                );

                Log.i("value in spinner- ",textSpin+" position- "+position);
            }


            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        if (mapRowSpinnerPos.containsKey(position)) {
            holder.spinMain.setSelection(mapRowSpinnerPos.get(position));
        }
        return convertView;
    }


    public class
    ViewHolder {

        private EditText editMain;

        private ImageView imgMain;
        private TextView txtMain;
        private Spinner spinMain;


        String typeValue = "";
    }
}

custom_new_db_adapter_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_marginTop="5dp"
        android:id="@+id/custum_main_lin_layout"
        android:layout_width="match_parent"
        android:background="@color/colorAccent"
        android:layout_height="55dp">


        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.3"
            android:id="@+id/custum_main_desc"
            android:textSize="18sp"

            android:layout_margin="5dp"
            android:textColor="#fff"
            android:text="  ggggg"
            android:layout_gravity="center_vertical"/>
        <EditText android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.4"
            android:singleLine="true"
            android:lines="1"
            android:inputType="text"
            android:id="@+id/custum_main_edit"
            android:layout_gravity="center_vertical"
            android:maxLines="1"
            android:background="#fff"
        android:textColor="#000"
            android:layout_margin="5dp"/>


        <Spinner android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.7"
            android:layout_margin="5dp"
android:visibility="visible"
            android:background="@drawable/btn_dropdown_selected"
            android:id="@+id/custum_main_spinner"
            android:layout_gravity="center_vertical"/>



        <ImageView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.1"
            android:id="@+id/custum_main_img"
            android:layout_gravity="center_vertical"
            android:src="@drawable/icon_forward_10"/>


    </LinearLayout>





</RelativeLayout>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.newf.phoenixbd.demoaccornford.MainActivity">

    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"

        android:layout_height="400dp"
        android:layout_above="@+id/button"></ListView>

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Save"
        android:id="@+id/button"
        android:onClick="Save"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

对不起我的英语。

提前致谢。

【问题讨论】:

    标签: java android android-layout listview textwatcher


    【解决方案1】:

    您可以使用接口将更新后的值从适配器传递给您的活动。我用这个解决了我的问题:

    类:

    public class YourActivity extends ActionBarActivity implements GetClickedItem {
    
        Context mContext;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_home);
    
            Context mContext = this;
            ListView Your_List = findViewById(R.id.listview);
    
            Your_Adapter adptr = new Your_Adapter(mContext, (GetClickedItem) this);
            Your_List.setAdapter(adptr);
        }
    
        @Override
        public void getPostID(int position, String any_data) {
    
            Toast(context, "position: " + position + " any_data: " + any_data).show();
        }
    
    }
    

    和适配器:

    public class Your_Adapter extends BaseAdapter{
    
        private Context mContext;
        private GetClickedItem mGetClickedItem;
        Holder holder;
    
        // Constructor
        public Your_Adapter(Context context, GetClickedItem itemclickreference) {
            mContext = context;
            this.mGetClickedItem = itemclickreference;
        }
    
        public int getCount() {
            return your_data_size;
        }
    
        public Object getItem(int position) {
            return your_data.get(position);
        }
    
        public long getItemId(int position) {
            return position;
        }
    
        public View getView(final int position, View convertView, ViewGroup parent) {
    
           holder = new Holder();
    
           if (convertView == null) {
               LayoutInflater mInflater = (LayoutInflater) mContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
               convertView = mInflater.inflate(R.layout.xml_layout, null);
    
               holder.mButton = (Button) convertView.findViewById(R.id.button);
    
               convertView.setTag(holder);
    
           }else {
               holder = (Holder) convertView.getTag();
           } 
    
           holder.mButton.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    mGetClickedItem.getPostID(position, your_data.get(position));
                }
           });
    
            return convertView;
        }
    
        public class Holder{
            Button mButton;
        }
    
        public interface GetClickedItem{
           public void getPostID(int position, String any_other_data);
        }
    }
    

    您可以从适配器访问任何数据,您所要做的就是在适配器布局文件中包含按钮。

    【讨论】:

    • 我正在使用这种方法,但没有运气
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-20
    相关资源
    最近更新 更多