2015-11-16 38 views
0

我有一个从解析中检索文本和图像的列表视图。我将文字设置为单个视图,但不知道如何将图像设置为单个视图。 我已经试过如下 这里是我的代码从parse.com设置图像

主要activity.java

public class MainActivity extends Activity 
{ 
ListView listview; 
List<ParseObject> ob; 
ProgressDialog mProgressDialog; 
ListViewAdapter adapter; 
private List<Textfile> textfilelist = null; 

@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    // Add your initialization code here 
    Parse.initialize(this, "Hwq4ExtHsfHimcHFO3596nYv4dcwd6MX2hdIC7wN", "x7iF0nUMRS1T1boXBOAHhwNI8HUkhGuGapJFksI6"); 

    ParseUser.enableAutomaticUser(); 
ParseACL defaultACL = new ParseACL(); 

    // If you would like all objects to be private by default, remove this line. 
    defaultACL.setPublicReadAccess(true); 

    ParseACL.setDefaultACL(defaultACL, true); 

    new RemoteDataTask().execute(); 
} 

private class RemoteDataTask extends AsyncTask<Void, Void, Void> { 
    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     // Create a progressdialog 
     mProgressDialog = new ProgressDialog(MainActivity.this); 
     // Set progressdialog title 
     mProgressDialog.setTitle("loading"); 
     // Set progressdialog message 
     mProgressDialog.setMessage("Loading..."); 
     mProgressDialog.setIndeterminate(false); 
     // Show progressdialog 
     mProgressDialog.show(); 
    } 

    @Override 
    protected Void doInBackground(Void... params) { 


     // Create the array 
     textfilelist = new ArrayList<Textfile>(); 
     try { 
      // Locate the class table named "TestText" in Parse.com 
      ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(
       "TestText"); 

      query.orderByDescending("_created_at"); 
      ob= query.find(); 

      for (ParseObject txtobject : ob) { 
       // Locate images in flag column 
       ParseFile image = (ParseFile) txtobject.get("image"); 

       Textfile map = new Textfile(); 
       map.setText((String) txtobject.get("text")); 
       map.setTxtview((String) txtobject.get("code")); 
       //map.setTxtview((String) txtobject.get("textfile")); 
       map.setImage(image.getUrl()); 
       textfilelist.add(map); 
      } 
     } catch (ParseException e) { 
      Log.e("Error", e.getMessage()); 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    @Override 
    protected void onPostExecute(Void result) { 
     // Locate the listview in listview_main.xml 
     listview = (ListView) findViewById(R.id.listview); 
     // Pass the results into ListViewAdapter.java 
     adapter = new ListViewAdapter(MainActivity.this, 
             textfilelist); 
     // Binds the Adapter to the ListView 
     listview.setAdapter(adapter); 
     // Close the progressdialog 
     mProgressDialog.dismiss(); 
     } 
     } 



    } 

singleitemview.java

@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    // TODO: Implement this method 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.singleitemview); 

    Intent i = getIntent(); 


    text = i.getStringExtra("code"); 

    TextView txt= (TextView) findViewById(R.id.singleitemviewTextView); 

    txt.setText(text); 

ImageView img = (ImageView) findViewById(R.id.singleitemviewImageView); 
    image = i.getStringExtra("image"); 
    img.setImageBitmap(image); 
    } 
+0

TEXTFILE地图=新的文本文件(); map.setImage(image.getUrl()); 什么是Textfile类和setImage方法do !!!? 一旦你得到了图像的URL 而不是img.setImageBitmap(image);使用毕加索作为@Gufran提 –

回答

0

还有的通过实现it.Recommended多种方式谷歌,你可以使用毕加索。在https://github.com/square/picasso

首先编译依赖

compile 'com.squareup.picasso:picasso:2.5.2' 

那么简单的使用这个在您的适配器的getView()

Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView); 
+0

但同样可能的解析代码就像在singleitemview中用于textview一样。我已经在某处读过它,但不记得它。你说的方法也很好,但是我想从分析中解脱 – user5524159

+0

我有很多picaso的图片,会变成很多代码。但解析它的唯一的两条线,我可以在我的解析数据中添加图像 – user5524159

+0

Bro @ user5524159我红色的评论10次....但我不明白....如果你想显示图像在你的应用程序从解析,U会得到特定图像的URL。所以,U必须使用图像加载器库或Ur自己的代码(HttpURLConnection)... –