我尝试将数据从edit-text和spinner更新到我的数据库。但我在滚动列表视图时也发现了问题并且还更新了数据。滚动后我的数据会出现波动,因此可以保存更新的数据。
下面是我的演示代码..请问我错在哪里。
活动代码:(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>
抱歉我的英文。
提前感谢。
答案 0 :(得分:0)
您可以使用界面将适配器的更新值传递给活动。我用这个解决了我的问题:
类别:
{ address: { loc: [ 37.0814402, 15.287517 ], value: 'viale tica' },
tags: [ 'Primi', 'Secondi' ],
__v: 0,
cap: 96100,
ad_number: 15,
business_email: 'prova@provbhifbvvyvg',
_id: 571b3b78249a2e160b4eee3f }
和适配器:
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();
}
}
您可以从适配器访问任何数据,您只需在适配器布局文件中包含该按钮。