问题
I have made a
listview
with image and text as elements. If the data is text it displays text and if it is image it displays image.
When i click button from MainActivity
to the listview, the listview is displayed as needed, i.e images for image and text for text data.
The problem occurs when the listview activity is opened and any new data is inserted in db and the listview is refreshed. The data in the view is completely messed up. it shows image in the some of the text data.
But when i go back to the main activity and again come back to listview everything is perfect.
Can anyone please help me whats going wrong here.
I am using SimpleCursorAdaptor, CursorLoader.
MainActivity.java
public class MainActivity extends ActionBarActivity implements LoaderCallbacks<Cursor>{
private TextView from, data;
private SimpleCursorAdapter dataAdapter;
private String TO_USER;
ConnectionService connService;
boolean mBounded;
@Override
public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
String[] projection = {MessageHelper.COLUMN_ID,
MessageHelper.COLUMN_FROM,
MessageHelper.COLUMN_DATA};
String args[] ={ "cid","data"};
CursorLoader cursorLoader = new CursorLoader(this,
MyContentProvider.CONTENT_URI, projection,
MessageHelper.COLUMN_CONVERSATION_ID + " = ?" + " AND " +
MessageHelper.COLUMN_EXIN + " = ?" ,args,
MessageHelper.COLUMN_RECIEVED_TIMESTAMP);
return cursorLoader;
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter();
getLoaderManager().initLoader(0, null, this);
}
private void setListAdapter() {
String[] columns = new String[] {
MessageHelper.COLUMN_FROM,
MessageHelper.COLUMN_DATA
};
int[] to = new int[] {
R.id.data
};
Cursor cursor = loadMessage();
cursor.moveToFirst();
dataAdapter = new CustomCursorAdapter(
this,
R.layout.linear_listitem,
cursor,
columns,
to,
0);
listview.setAdapter(dataAdapter);
.
}
public void onLoaderReset(Loader<Cursor> loader) {
dataAdapter.swapCursor(null);
}
protected void onResume() {
super.onResume();
getLoaderManager().restartLoader(0, null, this);
}
protected void onRestart(){
super.onRestart();
}
CursorAdapter.java
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ClipDrawable;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
//import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import com.expert.sqllite.MessageChat;
import com.expert.sqllite.MessageHelper;
public class CustomCursorAdapter extends SimpleCursorAdapter {
private LayoutInflater mInflater;
private Map<Integer, ArrayList<MessageChat>> chatMapConId = new HashMap<Integer,
ArrayList<MessageChat>>();
public CustomCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags) {
super(context, layout, c, from, to,flags);
mInflater = (LayoutInflater)context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
MessageChat msgchat = new MessageChat();
msgchat.setData(cursor.getString(cursor.getColumnIndex(MessageHelper.COLUMN_DATA)));
msgchat.setFrom(cursor.getString(cursor.getColumnIndex(MessageHelper.COLUMN_FROM)));
ViewHolder holder;
if(!msgchat.getData().contains("-image-")){
holder = (ViewHolder) view.getTag();
}
else{
holder = (ViewHolder) view.getTag();
}
TextView dataMsg =(TextView)view.findViewById(R.id.data);
RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams
(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
ImageView imgMsg =null;
if(msgchat.getFrom().equalsIgnoreCase("me"))
{
if(!msgchat.getData().contains("-image-")){
dataMsg.setBackgroundResource
(R.drawable.speech_bubble_green);
dataMsg.setText(msgchat.getData());
}
else
{
String[] imageSplit=msgchat.getData().split("-image-");
Uri imgUri=Uri.parse(imageSplit[1]);
try {
imgMsg (ImageView)view.findViewById
(R.id.chat_image);
imgMsg.setImageBitmap(getThumbnail
(imgUri,context));
dataMsg.setVisibility(View.INVISIBLE);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
lp1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
}
else
{
if(!msgchat.getData().contains("-image-")){
dataMsg.setBackgroundResource
(R.drawable.speech_bubble_orange);
dataMsg.setText(msgchat.getData());
}
else{
String[] imageSplit=msgchat.getData().split("-image-");
Uri imgUri=Uri.parse(imageSplit[1]);
try {
imgMsg =(ImageView)view.findViewById
(R.id.chat_image);
imgMsg.setImageBitmap(getThumbnail
(imgUri,context));
imgMsg.setBackgroundResource
(R.drawable.speech_bubble_orange);
dataMsg.setVisibility(View.INVISIBLE);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
lp1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
}
if(!msgchat.getData().contains("-image-")){
dataMsg.setLayoutParams(lp1);
dataMsg.setTextColor(R.color.textColor);
}
else{
imgMsg.setLayoutParams(lp1);
}
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
// TODO Auto-generated method stub
File imageFile;
View rowView = mInflater.inflate(R.layout.linear_listitem, parent, false);
View rowViewImage = mInflater.inflate(R.layout.linear_listitem_image, parent, false);
ViewHolder holder = null;
ViewHolderImage holderImage = null;
String data = cursor.getString(cursor.getColumnIndex(MessageHelper.COLUMN_DATA));
String path = null;
if(data.contains("-image-")){
path = data.split("-image-")[1];
}
holder = new ViewHolder();
holder.data = (TextView)rowView.findViewById(R.id.data);
holder.imageHolder = (ImageView)rowViewImage.findViewById(R.id.chat_image);
rowView.setTag(holder);
return rowView;
}
class ViewHolder
{
TextView data,from;
ImageView imageHolder ;
}
class ViewHolderImage
{
ImageView imageHolder ;
}
public static Bitmap getThumbnail(Uri uri,Context context) throw FileNotFoundException,IOException{
InputStream input = context.getContentResolver().openInputStream(uri);
BitmapFactory.Options onlyBoundsOptions = new BitmapFactory.Options();
onlyBoundsOptions.inJustDecodeBounds = true;
onlyBoundsOptions.inDither=true;//optional
onlyBoundsOptions.inPreferredConfig=Bitmap.Config.ARGB_8888;//optional
BitmapFactory.decodeStream(input, null, onlyBoundsOptions);
input.close();
if ((onlyBoundsOptions.outWidth == -1) || (onlyBoundsOptions.outHeight == -1))
return null;
int originalSize = (onlyBoundsOptions.outHeight > onlyBoundsOptions.outWidth) ?
onlyBoundsOptions.outHeight : onlyBoundsOptions.outWidth;
// double ratio = (originalSize > THUMBNAIL_SIZE) ? (originalSize / THUMBNAIL_SIZE): 1.0;
double ratio = 1.0;
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
//bitmapOptions.inSampleSize = getPowerOfTwoForSampleRatio(ratio);
bitmapOptions.inSampleSize = 2;
bitmapOptions.inMutable = true;
bitmapOptions.inDither=true;//optional
bitmapOptions.inPreferredConfig=Bitmap.Config.RGB_565;//optional
bitmapOptions.outHeight=150;
bitmapOptions.outWidth=150;
input = context.getContentResolver().openInputStream(uri);
Bitmap bitmap = BitmapFactory.decodeStream(input, null, bitmapOptions);
input.close();
//bitmap.setWidth(400);
//bitmap.setHeight(400);
return bitmap;
}
private static int getPowerOfTwoForSampleRatio(double ratio){
int k = Integer.highestOneBit((int)Math.floor(ratio));
if(k==0) return 1;
else return k;
}
}
Update If i have data only text or only image. Then the list view is perfect. The problem i think is when both the view i.e text and image comes into picture.
回答1:
@Override
public int getItemViewType(int position) {
Cursor cursor = (Cursor) getItem(position);
return getItemViewType(cursor);
}
@Override
public int getViewTypeCount() {
return 2;
}
These method has solved the problem of improper display
来源:https://stackoverflow.com/questions/24154275/listview-display-duplicate-data-on-refresh