我正在尝试显示自定义列表视图。每行包含一个 textview 和一个 imageview。我在这段代码上做了很多尝试,但仍然无法理解这个问题。问题是,当我滚动列表时,它会选择列表中的其他项目。简而言之,它不存储 imageview 的先前状态。我需要这种机制,因为我想让我的活动像“通过从列表中选择来删除多个项目”一样工作。如果有人可以帮助我解决这个问题,请...
package com.walletapp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.ActivityInfo;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class DeleteData extends Activity {
Cursor cursor;
ListView lv_delete_data;
static ArrayList<Integer> listOfItemsToDelete;
SQLiteDatabase database;
private Category[] categories;
ArrayList<Category> categoryList;
private ArrayAdapter<Category> listAdapter;
ImageView iv_delete;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_delete_data);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
manage_reload_list();
listOfItemsToDelete = new ArrayList<Integer>();
iv_delete = (ImageView) findViewById(R.id.imageViewDeleteDataDelete);
iv_delete.setClickable(true);
iv_delete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (listOfItemsToDelete.isEmpty()) {
Toast.makeText(getBaseContext(), "No items selected.",
Toast.LENGTH_SHORT).show();
}
else {
showDialog(
"Warning",
"Are you sure you want to delete these categories ? This will erase all records attached with it.");
}
}
});
lv_delete_data = (ListView) findViewById(R.id.listViewDeleteData);
lv_delete_data.setAdapter(listAdapter);
}
private void showDialog(String title, String message) {
AlertDialog.Builder adb = new Builder(DeleteData.this);
adb.setTitle(title);
adb.setMessage(message);
adb.setIcon(R.drawable.ic_launcher);
String btn = "Ok";
if (title.equals("Warning")) {
btn = "Yes";
adb.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
}
adb.setPositiveButton(btn, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
for (int i : listOfItemsToDelete) {
// -------first delete from category table-----
database.delete("category", "cat_id=?", new String[] { i
+ "" });
// ---------delete from category_fields
database.delete("category_fields", "cat_id=?",
new String[] { i + "" });
// ---------delete from records_data-------
database.delete("records_data", "cat_id=?",
new String[] { i + "" });
}
database.close();
manage_reload_list();
lv_delete_data.setAdapter(listAdapter);
}
});
AlertDialog ad = adb.create();
ad.show();
}
protected void manage_reload_list() {
loadDatabase();
categoryList = new ArrayList<Category>();
// ------first fetch all categories name list-------
cursor = database.query("category", new String[] { "cat_id",
"cat_description" }, null, null, null, null, null);
if (cursor.getCount() == 0) {
showDialog("Error", "No categories found.");
cursor.close();
} else {
while (cursor.moveToNext()) {
categories = new Category[] { new Category(cursor.getInt(0),
cursor.getString(1)) };
categoryList.addAll(Arrays.asList(categories));
}
cursor.close();
}
listAdapter = new CategoryArrayAdapter(this, categoryList);
}
public static class Category {
String cat_name = "";
int cat_id = 0;
Boolean checked = false;
Category(int cat_id, String name) {
this.cat_name = name;
this.cat_id = cat_id;
}
public int getId() {
return cat_id;
}
public String getCatName() {
return cat_name;
}
public Boolean getChecked() {
return checked;
}
public void setChecked(Boolean checked) {
this.checked = checked;
}
public boolean isChecked() {
return checked;
}
public void toggleChecked() {
checked = !checked;
}
}
private static class CategoryViewHolder {
private ImageView imageViewCheck;
private TextView textViewCategoryName;
public CategoryViewHolder(ImageView iv_check, TextView tv_category_name) {
imageViewCheck = iv_check;
textViewCategoryName = tv_category_name;
}
public ImageView getImageViewCheck() {
return imageViewCheck;
}
public TextView getTextViewCategoryName() {
return textViewCategoryName;
}
}
private static class CategoryArrayAdapter extends ArrayAdapter<Category> {
private LayoutInflater inflater;
public CategoryArrayAdapter(Context context, List<Category> categoryList) {
super(context, R.layout.single_row_delete_data,
R.id.textViewSingleRowDeleteData, categoryList);
inflater = LayoutInflater.from(context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Category category = (Category) this.getItem(position);
final ImageView imageViewCheck;
final TextView textViewCN;
if (convertView == null) {
convertView = inflater.inflate(R.layout.single_row_delete_data,
null);
imageViewCheck = (ImageView) convertView
.findViewById(R.id.imageViewSingleRowDeleteData);
textViewCN = (TextView) convertView
.findViewById(R.id.textViewSingleRowDeleteData);
convertView.setTag(new CategoryViewHolder(imageViewCheck,
textViewCN));
}
else {
CategoryViewHolder viewHolder = (CategoryViewHolder) convertView
.getTag();
imageViewCheck = viewHolder.getImageViewCheck();
textViewCN = viewHolder.getTextViewCategoryName();
}
imageViewCheck.setFocusable(false);
imageViewCheck.setFocusableInTouchMode(false);
imageViewCheck.setClickable(true);
imageViewCheck.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ImageView iv = (ImageView) v;
Category category = (Category) iv.getTag();
if (category.getChecked() == false) {
imageViewCheck.setImageResource(R.drawable.set_check);
listOfItemsToDelete.add(category.getId());
category.setChecked(true);
} else {
imageViewCheck
.setImageResource(R.drawable.set_basecircle);
listOfItemsToDelete.remove((Integer) category.getId());
category.setChecked(false);
}
}
});
imageViewCheck.setTag(category);
textViewCN.setText(category.getCatName());
return convertView;
}
}
private void loadDatabase() {
database = openOrCreateDatabase("WalletAppDatabase.db",
SQLiteDatabase.OPEN_READWRITE, null);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
这是我的 logcat 输出:
04-11 08:32:47.083: D/dalvikvm(1261): GC_FOR_ALLOC freed 735K, 29% free 3332K/4672K, paused 199ms, total 205ms
04-11 08:32:47.362: E/ActivityThread(1261): Failed to find provider info for com.google.plus.platform
04-11 08:32:47.632: I/Ads(1261): adRequestUrlHtml: <html><head><script src="http://media.admob.com/sdk-core-v40.js"></script><script>AFMA_getSdkConstants();AFMA_buildAdURL({"preqs":18,"session_id":"5201479258687953639","seq_num":"19","slotname":"a1512f50d8c3692","u_w":320,"msid":"com.walletapp","cap":"m,a","adtest":"on","js":"afma-sdk-a-v6.3.0","bas_off":0,"net":"ed","app_name":"1.android.com.walletapp","hl":"en","gnt":3,"carrier":"310260","u_audio":4,"kw":[],"u_sd":1,"simulator":1,"ms":"5j2LTo7Vhgatbr2zw9EGXbWGmAc8VRgJRTZpfRmRxDNZMDEtohADcdSvAbGyjHBAH6KuyP48dzaU-_Gfkq8adRJMFxu5Oq_46jWdTHBUpf_Fgm5xN1ehJ8NcEhqNpN7oHsV38ukGuVQUNKofxsk0Ij8cY8l0_QddW8iKCAXhz80Ujr2xbTEzUd9nV9NGfq1EBiaZjk732PJPl7Z2Myy1oX61Bk7VZzpSyCtlX5XIzdTM8iyqsNk2yTmjG6sflKdqXykfd1oXnrlrxaDLHnq1C4cSceFeDQZ0Lv-oxG2Xzr76GT9BhyUEWzE7pTYJQ70-KpUOV2Il56LykY3wCH2j2Q","isu":"B3EEABB8EE11C2BE770B684D95219ECB","format":"320x50_mb","oar":0,"ad_pos":{"height":0,"visible":0,"y":0,"x":0,"width":0},"u_h":480,"pt":1,"bas_on":0,"ptime":1851610});</script></head><body></body></html>
04-11 08:32:48.321: D/webviewglue(1261): nativeDestroy view: 0x2a368740
04-11 08:32:48.392: I/Choreographer(1261): Skipped 93 frames! The application may be doing too much work on its main thread.
04-11 08:32:49.052: I/Choreographer(1261): Skipped 396 frames! The application may be doing too much work on its main thread.
04-11 08:32:55.001: I/Choreographer(1261): Skipped 50 frames! The application may be doing too much work on its main thread.
04-11 08:33:29.022: E/Ads(1261): JS: Uncaught ReferenceError: AFMA_getSdkConstants is not defined (http://media.admob.com/:1)
04-11 08:33:29.022: E/Web Console(1261): Uncaught ReferenceError: AFMA_getSdkConstants is not defined at http://media.admob.com/:1
04-11 08:33:47.529: I/Ads(1261): AdLoader timed out after 60000ms while getting the URL.
04-11 08:33:47.531: D/webviewglue(1261): nativeDestroy view: 0x2a368740
04-11 08:33:47.541: I/Ads(1261): onFailedToReceiveAd(A network error occurred.)
04-11 08:34:47.581: I/Ads(1261): Refreshing ad.
04-11 08:34:47.622: E/ActivityThread(1261): Failed to find provider info for com.google.plus.platform
04-11 08:34:47.762: I/Ads(1261): adRequestUrlHtml: <html><head><script src="http://media.admob.com/sdk-core-v40.js"></script><script>AFMA_getSdkConstants();AFMA_buildAdURL({"preqs":19,"session_id":"5201479258687953639","seq_num":"20","slotname":"a1512f50d8c3692","u_w":320,"msid":"com.walletapp","cap":"m,a","adtest":"on","js":"afma-sdk-a-v6.3.0","bas_off":0,"net":"ed","app_name":"1.android.com.walletapp","hl":"en","gnt":3,"carrier":"310260","u_audio":4,"kw":[],"u_sd":1,"simulator":1,"ms":"Trn0wt2c5XiO7OmFyXqrKSLD7ZgZh_ZbxneDNRsJ5MwCcYQbDy7isjpET_A9DTjPoNNPXVDn41qPQDD5Msbcr6ieK1Rchi6ctbf3FTZcO5QSlr054zlHBS2qx7jL-T9VwVNde2CNfbgy_6_w5Ww85XfPo9L2TaZqmg49knQiBxg_gw3fijmphjz0LYqrhjuc8XLVG2K9z27UwQA4_2KGoiM5iwxXsXO0L2LT-3BXz49GyD3r9XZ8qHI4nrIQ2pPsEWUhgznx4I24PAvvwmOEcF0DVLzndhPwaj-HlI5f6-vXHGFQ6hxU6iV58DytEWEbF9eI1Ierr_4Eue0rqFkoyw","isu":"B3EEABB8EE11C2BE770B684D95219ECB","format":"320x50_mb","oar":0,"ad_pos":{"height":0,"visible":0,"y":480,"x":0,"width":0},"u_h":480,"pt":1,"bas_on":0,"ptime":1971778});</script></head><body></body></html>
04-11 08:35:28.222: E/Ads(1261): JS: Uncaught ReferenceError: AFMA_getSdkConstants is not defined (http://media.admob.com/:1)
04-11 08:35:28.222: E/Web Console(1261): Uncaught ReferenceError: AFMA_getSdkConstants is not defined at http://media.admob.com/:1
04-11 08:35:47.714: I/Ads(1261): AdLoader timed out after 60000ms while getting the URL.
04-11 08:35:47.714: D/webviewglue(1261): nativeDestroy view: 0x2a240398
04-11 08:35:47.741: I/Ads(1261): onFailedToReceiveAd(A network error occurred.)
我没有收到任何错误。我是新手,但我仍然尽力在我的代码中找到错误,但没有成功。请帮我...