【发布时间】:2016-06-17 17:29:27
【问题描述】:
为了在列表视图中显示图像,我使用了 picasso,但出现了这个错误
06-14 11:07:10.780 1197-1197/com.example.dell.app2 E/value:: [{"Reference":"QNPD2016","Nom":"ERK","Description":"done","image":"image1"},{"Reference":"TTGH25361","Nom":"PSD","Description":"usage","image":"image2"}]
<!-- Hosting24 Analytics Code -->
<script type="text/javascript" src="http://stats.hosting24.com/count.php"></script>
<!-- End Of Analytics Code -->
06-14 11:07:10.800 1197-1197/com.example.dell.app2 W/Settings: Setting airplane_mode_on has moved from android.provider.Settings.System to android.provider.Settings.Global, returning read-only value.
06-14 11:07:10.824 1197-1197/com.example.dell.app2 E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /image1: open failed: ENOENT (No such file or directory)
06-14 11:07:10.824 1197-1197/com.example.dell.app2 I/System.out: resolveUri failed on bad bitmap uri: image1
06-14 11:07:10.828 1197-1197/com.example.dell.app2 E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /image2: open failed: ENOENT (No such file or directory)
06-14 11:07:10.832 1197-1197/com.example.dell.app2 I/System.out: resolveUri failed on bad bitmap uri: image2
06-14 11:07:10.840 1197-1197/com.example.dell.app2 E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /image1: open failed: ENOENT (No such file or directory)
06-14 11:07:10.844 1197-1197/com.example.dell.app2 I/System.out: resolveUri failed on bad bitmap uri: image1
06-14 11:07:10.844 1197-1197/com.example.dell.app2 E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /image2: open failed: ENOENT (No such file or directory)
06-14 11:07:10.848 1197-1197/com.example.dell.app2 I/System.out: resolveUri failed on bad bitmap uri: image2
06-14 11:07:10.848 1197-1197/com.example.dell.app2 E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /image1: open failed: ENOENT (No such file or directory)
06-14 11:07:10.852 1197-1197/com.example.dell.app2 I/System.out: resolveUri failed on bad bitmap uri: image1
06-14 11:07:10.884 1197-1197/com.example.dell.app2 E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /image2: open failed: ENOENT (No such file or directory)
06-14 11:07:10.884 1197-1197/com.example.dell.app2 I/System.out: resolveUri failed on bad bitmap uri: image2
如何纠正这个错误 这是我的代码
我使用了 json 和 picasso。 我已经使用此代码显示了图像,但是当我尝试将其与列表视图一起使用时,它不起作用
public class ResultFragment extends Fragment implements ListView.OnItemClickListener {
private String JSON_STRING;
private ImageButton btn1;
ListAdapter adapter;
public static final String URL = "http://aaaaa.com/PHP/getAllRes.php";
//public static final String TAG_JSON_ARRAY = "result";
// List view
private ListView listView;
// Search EditText
EditText inputSearch;
private ImageView imageView1;
LinearLayout layout;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.fragment_produits, container, false);
listView = (ListView) rootview.findViewById(R.id.listView);
inputSearch = (EditText) rootview.findViewById(R.id.inputSearch);
listView.setOnItemClickListener(this);
imageView1= (ImageView)rootview.findViewById(R.id.imageView1);
layout=(LinearLayout) rootview.findViewById(R.id.layout);
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
((SimpleAdapter) ProduitsFragment.this.adapter).getFilter().filter(cs);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
}
@Override
public void afterTextChanged(Editable arg0) {
}
});
getJSON();
return rootview;
}
private void showProduct(String json){
JSONObject jsonObject = null;
ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String, String>>();
try {
JSONArray result = new JSONArray(json);
for(int i = 0; i<result.length(); i++){
JSONObject jo = result.getJSONObject(i);
String name = jo.getString("Nom");
String ref = jo.getString("Reference");
String image1 = jo.getString("image");
final ImageView im =new ImageView (getActivity());
Picasso.with(getActivity())
.load("http://aaaaa.com/PHP/images/"+image1+".jpg")
.resize(50, 50)
.into(im);
//layout.addView(im);
HashMap<String,String> product= new HashMap<>();
product.put("name",name);
product.put("ref",ref);
product.put("image1",image1);
list.add(product);
}
} catch (JSONException e) {
e.printStackTrace();
}
adapter = new SimpleAdapter(
getActivity(), list, R.layout.list_row,
new String[]{"image1","ref","name"},
new int[]{R.id.imageView1, R.id.nom, R.id.email2});
listView.setAdapter(adapter);
}
private void getJSON() {
class GetJSON extends AsyncTask<Void, Void, String> {
ProgressDialog loading;
@Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(getActivity(), "Téléchargement", "Veuillez patientez...", false, false);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
JSON_STRING = s;
Log.e("value: ", JSON_STRING);
showProduct(s);
}
@Override
protected String doInBackground(Void... params) {
RequestHandler rh = new RequestHandler();
String s = rh.sendGetRequest(URL);
return s;
}
}
GetJSON gj = new GetJSON();
gj.execute();
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(getActivity().getApplicationContext(), ViewProduit.class);
HashMap<String, String> map = (HashMap) parent.getItemAtPosition(position);
String tid = map.get("ref").toString();
intent.putExtra("ref", tid);
startActivity(intent);
}
}
编辑 CustomArrayAdapter .java
public class CustomArrayAdapter extends ArrayAdapter<Product> {
private Context context;
private ArrayList<Product> productList;
private ArrayList<HashMap<String, String>> filteredData;
public CustomArrayAdapter(Context context, ArrayList<Product> productList) {
super(context, R.layout.listrow2, productList);
this.context = context;
this.productList = productList;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.listrow2, parent, false);
TextView nom = (TextView) rowView.findViewById(R.id.nom);
TextView email2 = (TextView) rowView.findViewById(R.id.email2);
ImageView imageView = (ImageView) rowView.findViewById(R.id.imageView1);
nom.setText(productList.get(position).getName()); //make sure nom is for name and email2 is for ref. This is just my guesswork.
email2.setText(productList.get(position).getRef());
Picasso.with(context).load(productList.get(position).getImageUrl()).into(imageView);
return rowView;
}
public Filter getFilter()
{
return new Filter()
{
@Override
protected FilterResults performFiltering(CharSequence charSequence)
{
FilterResults results = new FilterResults();
//If there's nothing to filter on, return the original data for your list
if(charSequence == null || charSequence.length() == 0)
{
results.values = productList;
results.count = productList.size();
}
else
{
ArrayList<HashMap<String,String>> filterResultsData = new ArrayList<HashMap<String,String>>();
for(HashMap<String,String> data : productList)
{
//In this loop, you'll filter through originalData and compare each item to charSequence.
//If you find a match, add it to your new ArrayList
//I'm not sure how you're going to do comparison, so you'll need to fill out this conditional
if(data matches your filter criteria)
{
filterResultsData.add(data);
}
}
results.values = filterResultsData;
results.count = filterResultsData.size();
}
return results;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults)
{
filteredData = (ArrayList<HashMap<String,String>>)filterResults.values;
notifyDataSetChanged();
}
};
}
}
我想要一个这样的列表视图 enter image description here
这是我的 filezilla 截图
【问题讨论】:
-
这是一个
FileNotFoundException。这意味着http://aaaaa.com/PHP/images/image1.jpg没有任何内容。 “http://aaaaa.com/PHP/images/”网址中没有image1.jpg。 -
此网址中有 2 张图片。我已经在其他活动中展示了它们
-
也许吧,但是这两个图像的名称不是您在此行中指定的“image1.jpg”:
Picasso.with(getActivity()) .load("http://aaaaa.com/PHP/images/"+image1+".jpg") .resize(50, 50) .into(im);找出这些图像的确切文件名。 -
名称正确。我用 FileZilla 截图更新了我的帖子
-
是的,亲爱的。我真的很抱歉之前的肤浅一瞥。现在我更仔细地查看了您的代码,我有几个问题。你为什么这样做:
final ImageView im =new ImageView (getActivity()); Picasso.with(getActivity()) .load("http://aaaaa.com/PHP/images/"+image1+".jpg") .resize(50, 50) .into(im);,你为什么将HashMaps 的ArrayList传递给你的适配器,你能告诉我你的SimpleAdapter的代码吗?