我想在列表视图中显示具有四个属性的项目:id,name,marks和date。它工作正常,但当我滚动列表视图并选择第一项时,它也选择第16项,有时应用程序崩溃。我搜索了很多,但无法理解问题是什么。
//Adapter class
public class Adapter extends ArrayAdapter<Item> {
private Activity activity;
int id;
ArrayList<Item> items;
public Adapter(Activity context, int resource, ArrayList<Item> objects) {
super(context, resource, objects);
this.activity=context;
this.id=resource;
this.items=objects;
}
@Override
public int getViewTypeCount() {
return getCount();
}
@Override
public int getItemViewType(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = activity.getLayoutInflater();
convertView = inflater.inflate(id, null);
}
Item item = items.get(position);
TextView tv_id = (TextView) convertView.findViewById(R.id.tv_id);
TextView tv_name = (TextView) convertView.findViewById(R.id.tv_name);
TextView tv_marks = (TextView) convertView.findViewById(R.id.tv_marks);
TextView tv_date = (TextView) convertView.findViewById(R.id.tv_date);
tv_id.setText(item.getId());
tv_name.setText(item.getName());
tv_marks.setText(item.getMarks());
tv_date.setText(item.getDat());
return convertView;
}
//item class-----------
public class Item {
private String id;
private String name;
private String surname;
private String marks;
private String dat;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public String getMarks() {
return marks;
}
public void setMarks(String marks) {
this.marks = marks;
}
public String getDat() {
return dat;
}
public void setDat(String dat) {
this.dat = dat;
}
}
// List view Class
public class List_Viwer extends AppCompatActivity {
Context ctx = this;
ListView listView;
Adapter adapter;
CheckBox checkBox;
DatabaseHelper myDb = new DatabaseHelper(this);
ArrayList<Item> arrayList=new ArrayList<Item>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list__viwer);
//checkBox.setVisibility(View.GONE);
Cursor cursor = myDb.getAllData();
if(cursor!= null) {
if (cursor.moveToFirst()){
do{
Item item = new Item();
item.setId(cursor.getString(0));
item.setName(cursor.getString(1));// gets id fromm database
item.setSurname(cursor.getString(2)); //gets title from database
item.setMarks(cursor.getString(3));
item.setDat(cursor.getString(4));// gets content / body from database
arrayList.add(item);
}while (cursor.moveToNext());
}
adapter = new Adapter(this, R.layout.custom_list_item, arrayList);
listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(adapter);
}
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView temp_txt = (TextView) view.findViewById(R.id.tv_id);
String tempID = temp_txt.getText().toString();
// Toast.makeText(ctx, ID, Toast.LENGTH_LONG).show();
Intent i = new Intent(ctx, View_Person.class);
i.putExtra("ID", tempID);
ctx.startActivity(i);
}
});
//-----------------Long Clikced Listener Starts from here-----------------------------
listView.setAdapter(adapter);
listView.setChoiceMode(listView.CHOICE_MODE_MULTIPLE_MODAL);
listView.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
ArrayList<String> list_ids = new ArrayList<String>();
int count = 0;
@Override
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
String data = adapter.getItem(position).getId();
Toast.makeText(ctx, data, Toast.LENGTH_SHORT).show();
if (list_ids.contains(data)) {
for (int i = 0; i < list_ids.size(); i++) {
// String temp = list_ids.get(i);
if (data == list_ids.get(i)) {
count--;
mode.setTitle(count + " Items Selected");
list_ids.remove(i);
listView.getChildAt(position).setBackgroundResource(R.drawable.bg1);
}
}
}
else {
// listView.getChildAt(pos).setBackgroundColor(Color.YELLOW);
count++;
mode.setTitle(count + " Items Selected");
listView.getChildAt(position).setBackgroundResource(R.drawable.bg2);
list_ids.add(data);
}
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater inflator = mode.getMenuInflater();
inflator.inflate(R.menu.menu_delete, menu);
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()){
case R.id.action_delete:
for (int i=0; i<list_ids.size(); i++)
{
String temp = list_ids.get(i);
myDb.deleteData(temp);
}
Toast.makeText(ctx, count+" Items Deleted.", Toast.LENGTH_SHORT).show();
mode.finish();
count = 0;
list_ids.clear();
arrayList.clear();
refreshdata(); //gets data from method and Sets to list View item
//listView.clearChoices();
//adapter.notifyDataSetChanged();
//break;
default:
return false;
//break;
}
//return false;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(ctx);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("checkBox", "false");
editor.apply();
count = 0;
list_ids.clear();
arrayList.clear();
refreshdata();
// listView.clearChoices();
listView.setBackgroundColor(Color.TRANSPARENT);
// listView.clearChoices();
// listView.setBackgroundColor(Color.TRANSPARENT);
}
}); //-----------Lon clik listener ends---------------------------------
} //-------------On Create bundel method ends------------------------------------
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_search, menu);
MenuItem item = menu.findItem(R.id.menuSearch);
SearchView searchView = (SearchView)item.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
// adapter.getFilter().filter(newText);
// getPlanets(newText);
arrayList.clear(); // MUST CLEAR THE PREVIOUS DATA FROM LIST
Cursor cursor = myDb.SearchData(newText);
if (cursor != null) {
if (cursor.moveToFirst()) {
do {
Item item = new Item();
item.setId(cursor.getString(0));
item.setName(cursor.getString(1));// gets id fromm database
item.setSurname(cursor.getString(2)); //gets title from database
item.setMarks(cursor.getString(3));
item.setDat(cursor.getString(4));// gets content / body from database
// item.setCheckBox();
arrayList.add(item);
} while (cursor.moveToNext());
}
listView.setAdapter(adapter);
}
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
public void refreshdata(){
final Cursor cursor = myDb.getAllData();
if(cursor!= null) {
if (cursor.moveToFirst()){
do{
Item item = new Item();
item.setId(cursor.getString(0));
item.setName(cursor.getString(1));// gets id fromm database
item.setSurname(cursor.getString(2)); //gets title from database
item.setMarks(cursor.getString(3));
item.setDat(cursor.getString(4));// gets content / body from database
arrayList.add(item);
}while (cursor.moveToNext());
}
adapter = new Adapter(this, R.layout.custom_list_item, arrayList);
listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(adapter);
}
}
}
/// my log cat
12-10 03:48:51.453 6587-6587/com.hexa.adnan.mysqllitedemo E/AndroidRuntime: FATAL EXCEPTION: main
12-10 03:48:51.453 6587-6587/com.hexa.adnan.mysqllitedemo E/AndroidRuntime: java.lang.NullPointerException
12-10 03:48:51.453 6587-6587/com.hexa.adnan.mysqllitedemo E/AndroidRuntime: at com.hexa.adnan.mysqllitedemo.List_Viwer$2.onItemCheckedStateChanged(List_Viwer.java:101)
12-10 03:48:51.453 6587-6587/com.hexa.adnan.mysqllitedemo E/AndroidRuntime: at android.widget.AbsListView$MultiChoiceModeWrapper.onItemCheckedStateChanged(AbsListView.java:6077)