我制作了一张listview
图片和文字作为元素。如果数据是文本,则显示文本,如果是图像则显示图像。
当我点击从MainActivity
到列表视图的按钮时,列表视图会根据需要显示,即图像和文本数据的图像。
打开listview活动并在db中插入任何新数据并刷新listview时,会出现问题。视图中的数据完全搞砸了。它在一些文本数据中显示图像。
但是当我回到主要活动并再次回到listview时,一切都很完美。
任何人都可以帮我解决这里出错的问题。
我正在使用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;
}
}
更新 如果我只有数据文本或只有图像。然后列表视图是完美的。我认为问题在于视图,即文本和图像都出现在图片中。
答案 0 :(得分:0)
@Override
public int getItemViewType(int position) {
Cursor cursor = (Cursor) getItem(position);
return getItemViewType(cursor);
}
@Override
public int getViewTypeCount() {
return 2;
}
这些方法解决了显示不当的问题