Java 在列表视图的特定位置显示图像
我正在创建一个短信应用程序,到目前为止,发送短信的效果非常好。但是我很难实现文件传输 我打开图库,选择一个图像并发送它。该信息由服务器使用PHP收集,并在mysql数据库中插入数据,其中包含user、msg、date和file字段。文件字段有时为空Java 在列表视图的特定位置显示图像,java,android,listview,uiimageview,chat,Java,Android,Listview,Uiimageview,Chat,我正在创建一个短信应用程序,到目前为止,发送短信的效果非常好。但是我很难实现文件传输 我打开图库,选择一个图像并发送它。该信息由服务器使用PHP收集,并在mysql数据库中插入数据,其中包含user、msg、date和file字段。文件字段有时为空 public class Chati_LVAdapter extends BaseAdapter { //Variables globales Context context; List<Chati_class> list_chati_m
public class Chati_LVAdapter extends BaseAdapter {
//Variables globales
Context context;
List<Chati_class> list_chati_mess;
String filename = "";
//Variables auxiliares de apoyo
private static final int LAYOUT_PROPIO = 0, LAYOUT_OTROS = 1, NO_FILE = 0, ATTACHED_FILE = 1;
private static final String URL_FILES = "http://SOMEURL/img/";
LayoutInflater mInflater;
String nombre_real = "";
//Clase estática para almacenar variables
private static class ContactsPlaceholder {
public TextView fecha;
public TextView nombre;
public TextView ultimo_mensaje;
public ImageButton foto_adjunta;
}
//Constructor de la clase
Chati_LVAdapter(List<Chati_class> list_chati_message, Context context) {
//Inicializamos todas las variables necearias para nuestro chat
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.context = context;
this.list_chati_mess = list_chati_message;
DBHandler db = new DBHandler(context);
nombre_real = db.getNameReal(ClassCOM.getUserLogged(context));
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getItemViewType(int position) {
//Obtendremos el tipo de layout a mostrar por cada mensaje de chat
Chati_class user = list_chati_mess.get(position);
//Si el autor del mensaje es el mismo que el usuario que ha iniciado sesión, devolveremos un valor X
if (user.getAutor().equalsIgnoreCase(nombre_real)) {
return LAYOUT_PROPIO;
} else return LAYOUT_OTROS; //En caso contrario devolveremos otro valor estático
}
@Override
public int getCount() {
return list_chati_mess.size();
}
@Override
public Object getItem(int position) {
return list_chati_mess.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//Obtendremos la posicion actual de nuestro listview del chat
final Chati_class p = list_chati_mess.get(position);
ContactsPlaceholder holder;
//Comprobaremos si la variable convertView "no" es nula
if (convertView == null) {
//Si es nula crearemos una nueva vista de chat
holder = new ContactsPlaceholder();
TextView fecha = null;
TextView ult_mensaje = null;
TextView nombre = null;
ImageButton foto_adjunta = null;
switch(getItemFile(position)){
case NO_FILE:
switch (getItemViewType(position)) {
case LAYOUT_PROPIO:
convertView = mInflater.inflate(R.layout.lay_listview_chati_pr, null);
fecha = convertView.findViewById(R.id.textView_chati_pr_date);
ult_mensaje = convertView.findViewById(R.id.textView_chati_pr_message);
nombre = convertView.findViewById(R.id.textView_chati_pr_name);
break;
case LAYOUT_OTROS:
if (ClassCOM.getAppColorString(context).equals("blue")) {
convertView = mInflater.inflate(R.layout.lay_listview_chati_ot_azul, null);
fecha = convertView.findViewById(R.id.textView_chati_o_date);
ult_mensaje = convertView.findViewById(R.id.textView_chati_o_message);
nombre = convertView.findViewById(R.id.textView_chati_o_name);
} else {
convertView = mInflater.inflate(R.layout.lay_listview_chati_ot_rojo, null);
fecha = convertView.findViewById(R.id.textView_chati_o_date);
ult_mensaje = convertView.findViewById(R.id.textView_chati_o_message);
nombre = convertView.findViewById(R.id.textView_chati_o_name);
}
break;
}
break;
case ATTACHED_FILE:
switch (getItemViewType(position)) {
case LAYOUT_PROPIO:
convertView = mInflater.inflate(R.layout.lay_listview_chati_pr_file, null);
fecha = convertView.findViewById(R.id.textView_chati_pr_date_f);
ult_mensaje = convertView.findViewById(R.id.textView_chati_pr_message_f);
nombre = convertView.findViewById(R.id.textView_chati_pr_name_f);
foto_adjunta = convertView.findViewById(R.id.imagen_adjunta_pr);
break;
case LAYOUT_OTROS:
if (ClassCOM.getAppColorString(context).equals("blue")) {
convertView = mInflater.inflate(R.layout.lay_listview_chati_ot_azul_file, null);
fecha = convertView.findViewById(R.id.textView_chati_o_date_f);
ult_mensaje = convertView.findViewById(R.id.textView_chati_o_message_f);
nombre = convertView.findViewById(R.id.textView_chati_o_name_f);
foto_adjunta = convertView.findViewById(R.id.imagen_adjunta_o);
} else {
convertView = mInflater.inflate(R.layout.lay_listview_chati_ot_rojo_file, null);
fecha = convertView.findViewById(R.id.textView_chati_o_date_f);
ult_mensaje = convertView.findViewById(R.id.textView_chati_o_message_f);
nombre = convertView.findViewById(R.id.textView_chati_o_name_f);
foto_adjunta = convertView.findViewById(R.id.imagen_adjunta_o);
}
break;
}
if(p.getFoto() != null) {
String file = p.getFoto();
if(file.length() > 0) {
String ext = file.substring(file.lastIndexOf("."), file.length());
if (ext.equals(".jpg") || ext.equals(".png") || ext.equals(".gif") || ext.equals(".raw") || ext.equals(".webp") || ext.equals(".bmp")) {
Picasso.with(context).load(URL_FILES + p.getFoto()).into(foto_adjunta);
foto_adjunta.setVisibility(View.VISIBLE);
} else {
foto_adjunta.setImageResource(R.drawable.file_icon);
foto_adjunta.setMaxHeight(350);
foto_adjunta.setVisibility(View.VISIBLE);
}
holder.foto_adjunta = foto_adjunta;
}
}
break;
}
holder.fecha = fecha;
holder.ultimo_mensaje = ult_mensaje;
holder.nombre = nombre;
convertView.setTag(holder);
} else {
holder = (ContactsPlaceholder) convertView.getTag();
}
final ImageButton info_foto = convertView.findViewById(R.id.imagen_adjunta_o);
if (info_foto != null) {
info_foto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
filename = p.getFoto();
new DescargaFoto(context, filename, URL_FILES);
}
});
}
holder.ultimo_mensaje.setText(p.getMensaje());
holder.fecha.setText(p.getFecha());
holder.nombre.setText(p.getAutor());
return convertView;
}
public int getItemFile(int position) {
//Obtendremos el tipo de layout a mostrar por cada mensaje de chat
Chati_class user = list_chati_mess.get(position);
//Si el autor del mensaje es el mismo que el usuario que ha iniciado sesión, devolveremos un valor X
if (user.getFoto() != null) {
Log.i("FOTO NO NULL", user.getFoto());
return ATTACHED_FILE;
} else return NO_FILE; //En caso contrario devolveremos otro valor estático
}}
即使所有的ifs都试图避免它,我也会得到一个空错误
E/AndroidRuntime: FATAL EXCEPTION: main
Process: es.monlau.smartschool, PID: 6325
java.lang.StringIndexOutOfBoundsException: length=4; index=-1
at java.lang.String.substring(String.java:1968)
at es.monlau.smartschool.Chati_LVAdapter.getView(Chati_LVAdapter.java:155)
我不明白为什么在有这么多控件检查值是否为null时执行该代码。您不会得到null错误,但是:
java.lang.StringIndexOutOfBoundsException: length=4; index=-1
因此,错误表示使用-1作为子字符串方法。使用file.lastIndexOf可以得到-1。所以基本上没有。在字符串中,这就是为什么得到-1。在尝试提取文件扩展名之前,如果indexOf返回的结果大于-1,则应首先检查:
int dotIndex = file.lastIndexOf(".");
if(dotIndex > -1) {
String ext = file.substring(dotIndex, file.length());
...
}
谢谢你,这很有效!我说我得到了一个空错误,因为我打印了'file'的值,它给了我空值,但仍然通过了iffile!=无效的我真的不知道,但现在没事了!
int dotIndex = file.lastIndexOf(".");
if(dotIndex > -1) {
String ext = file.substring(dotIndex, file.length());
...
}