在 Adapter 中实现 Listview 和 Imageview
Implement Listview and Imageview in Adapter
我的 ImageView
在 Listview
中,用户可以从图库中选择一张图片并将其放入 ImageView
!我遇到的问题是图像在 imageview 中出现一秒钟然后立即消失!
我担心我没有以正确的方式设置适配器,这就是图像消失的原因!问题还在于我是 Android 的新手,我不太了解如何以正确的方式设置适配器以显示图像!
有关信息:在主要 xml 中是 ListView
,在自定义布局中 xml 是 ImageView
和 2 TextView
。一个用于用户名,另一个用于用户发送的评论!它们以正确的方式出现,但问题在于 ListView
和 ImageView
!
在这种情况下我需要指导和建议...
我的适配器来了:
public class RecipesAdapter extends ArrayAdapter<ParseObject> {
protected List<ParseObject> mRecipes;
public Context mContext;
public RecipesAdapter(Context context, List<ParseObject> comment) {
super(context, R.layout.recipes_customlayout, comment);
mContext = context;
mRecipes = comment;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(
R.layout.recipes_customlayout, null);
holder = new ViewHolder();
holder.imgPic = (ParseImageView) convertView.findViewById(R.id.img_recipes);
holder.username_recipesChat = (TextView) convertView.findViewById(R.id.row_username_recipes);
holder.message_recipesChat = (TextView) convertView.findViewById(R.id.row_message_recipes);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
ParseObject recipesObject = mRecipes.get(position);
String username = recipesObject.getString("user");
holder.username_recipesChat.setText(username);
String message = recipesObject.getString("commentStatus");
holder.message_recipesChat.setText(message);
return convertView;
}
public static class ViewHolder {
TextView username_recipesChat;
TextView message_recipesChat;
ParseImageView imgPic;
}
}
这里是主要的 activity 和 ImageView
:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recipes_ideas);
imagePath = PreferenceManager.getDefaultSharedPreferences(this).getString("picturePath", "");
imgPic = (ParseImageView) findViewById(R.id.img_recipes);
mRecipesChat = (EditText) findViewById(R.id.add_text);
mSendPicBtn = (Button) findViewById(R.id.btn_pic);
mSendPicBtn.setOnClickListener(this);
listview = (ListView) findViewById(android.R.id.list);
listview.setAdapter(adapter);
mSendRecipesBtn = (Button) findViewById(R.id.btn_comment);
mSendRecipesBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ParseUser currentUser = ParseUser.getCurrentUser();
String currentUserUsername = currentUser.getUsername();
String commentStatus = mRecipesChat.getText().toString();
if (commentStatus.isEmpty()) {
AlertDialog.Builder builder = new AlertDialog.Builder(RecipesIdeas.this);
builder.setMessage("Please type in a Message!");
builder.setTitle("Oops!");
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
byte[] image = null;
final ParseFile file = new ParseFile("CommentPic.png", image);
// Upload the image into Parse Cloud
file.saveInBackground();
ParseObject commentObject = new ParseObject("Comment");
commentObject.put("commentStatus", commentStatus);
commentObject.put("user", currentUserUsername);
commentObject.put("Image", "CommentPic.png");
commentObject.put("CommentImageFile", file);
commentObject.saveInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
Toast.makeText(getBaseContext(), "Your Pic is Saved!", Toast.LENGTH_LONG).show();
if (e == null) {
Toast.makeText(RecipesIdeas.this, "Send!", Toast.LENGTH_LONG).show();
mRecipesChat.setText("");
queryAndPopulateMsgs();
} else {
Toast.makeText(RecipesIdeas.this, e.getMessage(), Toast.LENGTH_LONG).show();
AlertDialog.Builder builder = new AlertDialog.Builder(RecipesIdeas.this);
builder.setMessage(e.getMessage());
builder.setTitle("Sorry!");
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
}
});
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Here we need to check if the activity that was triggers was the Image Gallery.
// If it is the requestCode will match the LOAD_IMAGE_RESULTS value.
// If the resultCode is RESULT_OK and there is some data we know that an image was picked.
if (requestCode == LOAD_IMAGE_RESULTS && resultCode == RESULT_OK && data != null) {
// Let's read picked image data - its URI
Uri pickedImage = data.getData();
// Let's read picked image path using content resolver
String[] filePath = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(pickedImage, filePath, null, null, null);
cursor.moveToFirst();
String imagePath = cursor.getString(cursor.getColumnIndex(filePath[0]));
PreferenceManager.getDefaultSharedPreferences(this).edit().putString("picturePath", imagePath).commit();
cursor.close();
imgPic = (ParseImageView) findViewById(R.id.img_recipes);
// Now we need to set the GUI ImageView data with data read from the picked file.
imgPic.setImageBitmap(BitmapFactory.decodeFile(imagePath));
// At the end remember to close the cursor or you will end with the RuntimeException!
}
}
您是否介意发布您的 activity 或片段代码,以便我们了解您如何调用和构建适配器?快速通过它看起来好像你只是在设置你的图像,如果你 convertView == null
。请记住,当您滚动和更改方向时,您的列表视图行(您的 convertView)会不断被破坏和重建。
我真的看不出你在适配器中设置图像的位置。图像的设置应该在适配器中而不是在
中完成
.....
onActivityResult
即
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(
R.layout.recipes_customlayout, null);
holder = new ViewHolder();
holder.imgPic = (ParseImageView) convertView.findViewById(R.id.img_recipes);
holder.username_recipesChat = (TextView) convertView.findViewById(R.id.row_username_recipes);
holder.message_recipesChat = (TextView) convertView.findViewById(R.id.row_message_recipes);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
ParseObject recipesObject = mRecipes.get(position);
String username = recipesObject.getString("user");
holder.username_recipesChat.setText(username);
String message = recipesObject.getString("commentStatus");
holder.message_recipesChat.setText(message);
imgPic.setImageBitmap(BitmapFactory.decodeFile(imagePath));
return convertView;
}
但是你为什么要在 ListView 中显示单个图像。这将导致重复相同的图像。我看不出有什么用
Chero Beam 您可能应该在 Google+ 上将您的项目发送给我,以便我可以帮助您修复它。如果你同意的话
我的 ImageView
在 Listview
中,用户可以从图库中选择一张图片并将其放入 ImageView
!我遇到的问题是图像在 imageview 中出现一秒钟然后立即消失!
我担心我没有以正确的方式设置适配器,这就是图像消失的原因!问题还在于我是 Android 的新手,我不太了解如何以正确的方式设置适配器以显示图像!
有关信息:在主要 xml 中是 ListView
,在自定义布局中 xml 是 ImageView
和 2 TextView
。一个用于用户名,另一个用于用户发送的评论!它们以正确的方式出现,但问题在于 ListView
和 ImageView
!
在这种情况下我需要指导和建议...
我的适配器来了:
public class RecipesAdapter extends ArrayAdapter<ParseObject> {
protected List<ParseObject> mRecipes;
public Context mContext;
public RecipesAdapter(Context context, List<ParseObject> comment) {
super(context, R.layout.recipes_customlayout, comment);
mContext = context;
mRecipes = comment;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(
R.layout.recipes_customlayout, null);
holder = new ViewHolder();
holder.imgPic = (ParseImageView) convertView.findViewById(R.id.img_recipes);
holder.username_recipesChat = (TextView) convertView.findViewById(R.id.row_username_recipes);
holder.message_recipesChat = (TextView) convertView.findViewById(R.id.row_message_recipes);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
ParseObject recipesObject = mRecipes.get(position);
String username = recipesObject.getString("user");
holder.username_recipesChat.setText(username);
String message = recipesObject.getString("commentStatus");
holder.message_recipesChat.setText(message);
return convertView;
}
public static class ViewHolder {
TextView username_recipesChat;
TextView message_recipesChat;
ParseImageView imgPic;
}
}
这里是主要的 activity 和 ImageView
:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recipes_ideas);
imagePath = PreferenceManager.getDefaultSharedPreferences(this).getString("picturePath", "");
imgPic = (ParseImageView) findViewById(R.id.img_recipes);
mRecipesChat = (EditText) findViewById(R.id.add_text);
mSendPicBtn = (Button) findViewById(R.id.btn_pic);
mSendPicBtn.setOnClickListener(this);
listview = (ListView) findViewById(android.R.id.list);
listview.setAdapter(adapter);
mSendRecipesBtn = (Button) findViewById(R.id.btn_comment);
mSendRecipesBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ParseUser currentUser = ParseUser.getCurrentUser();
String currentUserUsername = currentUser.getUsername();
String commentStatus = mRecipesChat.getText().toString();
if (commentStatus.isEmpty()) {
AlertDialog.Builder builder = new AlertDialog.Builder(RecipesIdeas.this);
builder.setMessage("Please type in a Message!");
builder.setTitle("Oops!");
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
byte[] image = null;
final ParseFile file = new ParseFile("CommentPic.png", image);
// Upload the image into Parse Cloud
file.saveInBackground();
ParseObject commentObject = new ParseObject("Comment");
commentObject.put("commentStatus", commentStatus);
commentObject.put("user", currentUserUsername);
commentObject.put("Image", "CommentPic.png");
commentObject.put("CommentImageFile", file);
commentObject.saveInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
Toast.makeText(getBaseContext(), "Your Pic is Saved!", Toast.LENGTH_LONG).show();
if (e == null) {
Toast.makeText(RecipesIdeas.this, "Send!", Toast.LENGTH_LONG).show();
mRecipesChat.setText("");
queryAndPopulateMsgs();
} else {
Toast.makeText(RecipesIdeas.this, e.getMessage(), Toast.LENGTH_LONG).show();
AlertDialog.Builder builder = new AlertDialog.Builder(RecipesIdeas.this);
builder.setMessage(e.getMessage());
builder.setTitle("Sorry!");
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
}
});
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Here we need to check if the activity that was triggers was the Image Gallery.
// If it is the requestCode will match the LOAD_IMAGE_RESULTS value.
// If the resultCode is RESULT_OK and there is some data we know that an image was picked.
if (requestCode == LOAD_IMAGE_RESULTS && resultCode == RESULT_OK && data != null) {
// Let's read picked image data - its URI
Uri pickedImage = data.getData();
// Let's read picked image path using content resolver
String[] filePath = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(pickedImage, filePath, null, null, null);
cursor.moveToFirst();
String imagePath = cursor.getString(cursor.getColumnIndex(filePath[0]));
PreferenceManager.getDefaultSharedPreferences(this).edit().putString("picturePath", imagePath).commit();
cursor.close();
imgPic = (ParseImageView) findViewById(R.id.img_recipes);
// Now we need to set the GUI ImageView data with data read from the picked file.
imgPic.setImageBitmap(BitmapFactory.decodeFile(imagePath));
// At the end remember to close the cursor or you will end with the RuntimeException!
}
}
您是否介意发布您的 activity 或片段代码,以便我们了解您如何调用和构建适配器?快速通过它看起来好像你只是在设置你的图像,如果你 convertView == null
。请记住,当您滚动和更改方向时,您的列表视图行(您的 convertView)会不断被破坏和重建。
我真的看不出你在适配器中设置图像的位置。图像的设置应该在适配器中而不是在
中完成.....
onActivityResult
即
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(
R.layout.recipes_customlayout, null);
holder = new ViewHolder();
holder.imgPic = (ParseImageView) convertView.findViewById(R.id.img_recipes);
holder.username_recipesChat = (TextView) convertView.findViewById(R.id.row_username_recipes);
holder.message_recipesChat = (TextView) convertView.findViewById(R.id.row_message_recipes);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
ParseObject recipesObject = mRecipes.get(position);
String username = recipesObject.getString("user");
holder.username_recipesChat.setText(username);
String message = recipesObject.getString("commentStatus");
holder.message_recipesChat.setText(message);
imgPic.setImageBitmap(BitmapFactory.decodeFile(imagePath));
return convertView;
}
但是你为什么要在 ListView 中显示单个图像。这将导致重复相同的图像。我看不出有什么用
Chero Beam 您可能应该在 Google+ 上将您的项目发送给我,以便我可以帮助您修复它。如果你同意的话