重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
郑州app开发新闻列表时候,遇到新闻列表中的图片展示,因为图片需要获取网络图片,imageview中的setImageURL提示不兼容的类型: String无法转换为Uri。导致android studio运行结果提示不兼容的类型: String无法转换为Uri。解决方法是需要自己定义类,解决imageview中setImageURL无法输入string的问题。
成都创新互联公司-专业网站定制、快速模板网站建设、高性价比道外网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式道外网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖道外地区。费用合理售后完善,十余年实体公司更值得信赖。
控件布局代码如下
mainactivity中的代码是
package cn.xhhkj.xhhkjtest;
import android.os.bundle;
import android.os.Handler;
import android.util.Xml;
import android.view.View;
import android.view.ViewGroup;
import android.widget.baseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import org.xmlpull.v1.Xmlpullparser;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private ListView lv_list;
private String path = "http://192.168.0.101/news.xml";
private ArrayList
protected static final int GET_DATA = 0;
private MyAdapter adapter;
private Handler handler = new Handler(){
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case GET_DATA:
lv_list.setAdapter(adapter);
break;
}
};
};
@Override
protected void onCreate(bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv_list = (ListView) findViewbyId(R.id.lv_list);
adapter = new MyAdapter();
initData();
}
private void initData(){
new Thread(){
public void run(){
try{
URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(10000);
int code = connection.getResponseCode();
if (code == 200){
InputStream inputStream = connection.getInputStream();
Xmlpullparser pullparser = Xml.newpullparser();
pullparser.setInput(inputStream, "utf-8");
int eventType = pullparser.getEventType();
NewsItem item = null;
while (eventType != Xmlpullparser.END_DOCUMENT){
switch(eventType){
case Xmlpullparser.START_TAG:
if("item".equals(pullparser.getName())){
item = new NewsItem();
}else if("title".equals(pullparser.getName())){
item.title = pullparser.nextText();
}else if("description".equals(pullparser.getName())){
item.description = pullparser.nextText();
}else if("img".equals(pullparser.getName())){
item.img = pullparser.nextText();
}else if("time".equals(pullparser.getName())){
item.time = pullparser.nextText();
}
break;
case Xmlpullparser.END_TAG:
if ("item".equals(pullparser.getName())) {
newslist.add(item);
}
break;
}
eventType = pullparser.next();
}
for(NewsItem item1: newslist){
System.out.println(item1);
}
handler.sendEmptyMessage(GET_DATA);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}
private class MyAdapter extends baseAdapter{
@Override
public int getCount() {
return newslist.size();
}
@Override
public Object getItem(int position) {
return newslist.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
View view = null;
if(convertView == null){
view = View.inflate(MainActivity.this, R.layout.item, null);
}else {
view = convertView;
}
TextView tv_title = (TextView) view.findViewbyId(R.id.tv_title);
TextView tv_content = (TextView) view.findViewbyId(R.id.tv_content);
TextView tv_time = (TextView) view.findViewbyId(R.id.tv_time);
MySmartImageView iv_image = (MySmartImageView) view.findViewbyId(R.id.iv_icon);
NewsItem newsItem = newslist.get(position);
tv_title.setText(newsItem.title);
tv_content.setText(newsItem.description);
tv_time.setText(newsItem.time);
iv_image.setImageURL(newsItem.img);
return view;
}
}
}
以上是主代码,那么自己定义的类
package cn.xhhkj.xhhkjtest;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import android.content.Context;
import android.graphics.bitmap;
import android.graphics.bitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.widget.ImageView;
public class MySmartImageView extends ImageView {
protected static final int GET_pIC_FAILED = 0;
protected static final int GET_pIC_SUCCESS = 1;
private Handler handler = new Handler(){
public void handleMessage(android.os.Message msg) {
bitmap bm = (bitmap) msg.obj;
setImagebitmap(bm);
};
};
public MySmartImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public MySmartImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MySmartImageView(Context context) {
super(context);
}
public void setImageURL(final String path){
new Thread(){
public void run() {
try {
URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(10000);
int code = connection.getResponseCode();
if(code==200){
InputStream inputStream = connection.getInputStream();
bitmap bitmap = bitmapFactory.decodeStream(inputStream);
Message msg = Message.obtain();
msg.obj = bitmap;
handler.sendMessage(msg);
}
} catch (Exception e) {
e.printStackTrace();
}
};
}.start();
}
}
运行结果如图: