我已经编写了一个程序,以listview的形式将json数据插入到sqlite数据库中,该视图工作正常,并且在每个项目上都有上网点击,详细信息也会在下一个活动中正确显示。 但是,一旦加载数据并关闭互联网以通过sqlite数据库加载数据,列表视图就会正确显示,但在每个项目上单击下一个活动的详细信息始终是上次查看的项目。
cursor = listHelper.getAllData();
String[] columns = new String[] { DbListHelper.ARTWORK_IMAGE,
DbListHelper.WRAPPER_TYPE,
DbListHelper.ARTIST_NAME, DbListHelper.COLLECTION_NAME,
DbListHelper.TRACK_NAME,
DbListHelper.COLLECTION_PRICE, DbListHelper.TRACK_ID };
int[] to = new int[] { R.id.artworkImage, R.id.wrapperType,
R.id.artistName, R.id.collectionName,
R.id.trackName, R.id.collectionPrice, R.id.trackId };
dataAdapter = new CustomImageTextCursorAdapter(getBaseContext(),
R.layout.custom_row, cursor, columns, to);
lv.setAdapter(dataAdapter);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int
position, long id) {
Cursor cursor = (Cursor) parent.getItemAtPosition(position);
final int item_id =
cursor.getInt(cursor.getColumnIndex(DbListHelper.UID));
String item_content1 =
cursor.getString(cursor.getColumnIndex
(cursor.getColumnName(0)));
String item_content2 =
cursor.getString(cursor.getColumnIndex
(cursor.getColumnName(1)));
String item_content3 =
cursor.getString(cursor.getColumnIndex
(cursor.getColumnName(2)));
String item_content4 =
cursor.getString(cursor.getColumnIndex
(cursor.getColumnName(3)));
String item_content5 =
cursor.getString(cursor.getColumnIndex
(cursor.getColumnName(4)));
String item_content6 =
cursor.getString(cursor.getColumnIndex
(cursor.getColumnName(5)));
String item_content7 =
cursor.getString(cursor.getColumnIndex
(cursor.getColumnName(6)));
String item_content8 =
cursor.getString(cursor.getColumnIndex
(cursor.getColumnName(7)));
Intent intent = new Intent(MainActivity.this,
intent.putExtra("trackId", item_content8);
SingleTunesDetails.class);
startActivity(intent);
}
});
CustomImageTextCursorAdapter.java:
public class CustomImageTextCursorAdapter extends SimpleCursorAdapter {
private LayoutInflater mLayoutInflater;
private Context context;
private SqliteListHelper listHelper;
String extStorageDirectory;
File folder;
File file;
ImageLoader imageLoader;
int layout;
ConnectionDetector connectionDetector;
FileCache fileCache;
private class ViewHolder {
TextView wrapperType, artistName, collectionName, trackName, collectionPrice,trackId;
ImageView artworkImage;
ViewHolder(View v) {
wrapperType = (TextView) v.findViewById(R.id.wrapperType);
artistName = (TextView) v.findViewById(R.id.artistName);
collectionName = (TextView) v.findViewById(R.id.collectionName);
trackName = (TextView) v.findViewById(R.id.trackName);
collectionPrice = (TextView) v.findViewById(R.id.collectionPrice);
artworkImage = (ImageView) v.findViewById(R.id.artworkImage);
trackId = (TextView) v.findViewById(R.id.trackId);
listHelper = new SqliteListHelper(context);
listHelper.open(context);
}
}
public CustomImageTextCursorAdapter(Context context, int layout, Cursor c,
String[] from, int[] to) {
super(context, layout, c, from, to);
this.context = context;
this.layout = layout;
mLayoutInflater = LayoutInflater.from(context);
imageLoader = new ImageLoader(context);
connectionDetector = new ConnectionDetector(context);
fileCache = new FileCache(context);
}
public View newView(Context ctx, Cursor cursor, ViewGroup parent) {
View itemView = mLayoutInflater.inflate(R.layout.custom_row, parent,
false);
itemView.setTag(new ViewHolder(itemView));
return itemView;
}
@Override
public void bindView(View v, Context context, Cursor c) {
// you might want to cache these too
int index = c.getColumnIndex(DbListHelper.UID);
int cid = c.getInt(index);
int index0 = c.getColumnIndex(DbListHelper.ARTWORK_IMAGE);
String cid0 = c.getString(index0);
int index1 = c.getColumnIndex(DbListHelper.WRAPPER_TYPE);
String cid1 = c.getString(index1);
int index2 = c.getColumnIndex(DbListHelper.ARTIST_NAME);
String cid2 = c.getString(index2);
int index3 = c.getColumnIndex(DbListHelper.COLLECTION_NAME);
String cid3 = c.getString(index3);
int index4 = c.getColumnIndex(DbListHelper.TRACK_NAME);
String cid4 = c.getString(index4);
int index5 = c.getColumnIndex(DbListHelper.COLLECTION_PRICE);
String cid5 = c.getString(index5);
int index6 = c.getColumnIndex(DbListHelper.TRACK_ID);
String cid6 = c.getString(index6);
ViewHolder vh = (ViewHolder) v.getTag();
vh.wrapperType.setText(cid1);
vh.artistName.setText(cid2);
vh.collectionName.setText(cid3);
vh.trackName.setText(cid4);
vh.collectionPrice.setText(cid5);
vh.trackId.setText(cid6);
vh.trackId.setVisibility(View.GONE);
if (connectionDetector.isConnectingToInternet()) {
imageLoader.DisplayImage(cid0, vh.artworkImage);
} else {
File file = fileCache.getFile(cid0);
Log.i("TAG", "file_ path :" + file.getPath());
Bitmap myBitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
vh.artworkImage.setImageBitmap(myBitmap);
}
}
SingleItemDetails.java:在这个类中,我将显示来自url的json数据,其中参数trackId是从上一个活动传递的。
public class SingleItemDetails extends Activity {
// URL to get contacts JSON
private static String url = "";
// JSON Node names
static final String TAG_RESULT = "results";
static final String TAG_ARTWORK_IMAGE = "artworkUrl100";
static final String TAG_WRAPPER_TYPE = "wrapperType";
static final String TAG_ARTIST_NAME = "artistName";
static final String TAG_COLLECTION_NAME = "collectionName";
static final String TAG_TRACK_NAME = "trackName";
static final String TAG_COLLECTION_PRICE = "collectionPrice";
static final String TAG_TRACK_ID = "trackId";
// contacts JSONArray
JSONArray tracks = null;
// Hashmap for ListView
ArrayList<HashMap<String, String>> singleTrackDetails;
String passedData1;
TextView wrapperTypeText, artistNameText, collectionNameText, trackNameText,
collectionPriceText;
ImageView trackImage;
String artworkImage, wrapperType, artistName, collectionName, trackName,
collectionPrice, trackId;
private SqliteListHelper listHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.single_track);
singleTrackDetails = new ArrayList<HashMap<String, String>>();
wrapperTypeText = (TextView) findViewById(R.id.wrapperType1);
artistNameText = (TextView) findViewById(R.id.artistName1);
collectionNameText = (TextView) findViewById(R.id.collectionName1);
trackNameText = (TextView) findViewById(R.id.trackName1);
collectionPriceText = (TextView) findViewById(R.id.collectionPrice);
passedData1 = getIntent().getStringExtra("trackId");
listHelper = new SqliteListHelper(getBaseContext());
listHelper.open(getBaseContext());
url = "https://itunes.apple.com/lookup?id=" + passedData1;
// Calling async task to get json
new GetSingleTrackDetails().execute();
}
class GetSingleTrackDetails extends AsyncTask<Void, Void, Void> {
private JSONObject jsonObj;
@Override
protected Void doInBackground(Void... params) {
// Creating service handler class instance
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
tracks = jsonObj.getJSONArray(TAG_RESULT);
// looping through All Products
for (int i = 0; i < tracks.length(); i++) {
JSONObject c = tracks.getJSONObject(i);
artworkImage = c.getString("artworkUrl100");
wrapperType = c.getString("wrapperType");
artistName = c.getString("artistName");
collectionName = c.getString("collectionName");
trackName = c.getString("trackName");
collectionPrice = c.getString("collectionPrice");
trackId = c.getString("trackId");
// tmp hashmap for single contact
HashMap<String, String> tunesMap = new HashMap<String,
String>();
// adding each child node to HashMap key => value
// contact.put(TAG_ID, firstname);
tunesMap.put(TAG_ARTWORK_IMAGE, artworkImage);
tunesMap.put(TAG_WRAPPER_TYPE, wrapperType);
tunesMap.put(TAG_ARTIST_NAME, artistName);
tunesMap.put(TAG_COLLECTION_NAME, collectionName);
tunesMap.put(TAG_TRACK_NAME, trackName);
tunesMap.put(TAG_COLLECTION_PRICE, collectionPrice);
tunesMap.put(TAG_TRACK_ID, trackId);
// adding contact to contact list
singleTrackDetails.add(tunesMap);
long id = listHelper.insertData2(artworkImage,
wrapperType, artistName, collectionName,
trackName, collectionPrice, trackId);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
Cursor cursor = listHelper.getAllData2();
if (cursor.moveToFirst()) {
do{
String item_content1 = cursor.getString(cursor.getColumnIndex
(cursor.getColumnName(0)));
String item_content2 = cursor.getString(cursor.getColumnIndex
(cursor.getColumnName(1)));
String item_content3 = cursor.getString(cursor.getColumnIndex
(cursor.getColumnName(2)));
String item_content4 = cursor.getString(cursor.getColumnIndex
(cursor.getColumnName(3)));
String item_content5 = cursor.getString(cursor.getColumnIndex
(cursor.getColumnName(4)));
String item_content6 = cursor.getString(cursor.getColumnIndex
(cursor.getColumnName(5)));
String item_content7 = cursor.getString(cursor.getColumnIndex
(cursor.getColumnName(6)));
String item_content8 = cursor.getString(cursor.getColumnIndex
(cursor.getColumnName(7)));
wrapperTypeText.setText(item_content3);
artistNameText.setText(item_content4);
collectionNameText.setText(item_content5);
trackNameText.setText(item_content6);
collectionPriceText.setText(item_content7);
}while(cursor.moveToNext());
}
cursor.close();
}
}
有谁能建议我哪里出错了? 谢谢