从parse.com

时间:2015-11-16 10:25:01

标签: android parse-platform android-listview

我有一个listview,可以从解析中检索文本和图像。我将文本设置为我的singleitemview,但不知道如何将图像设置为singleitemview。 我试过如下 这是我的代码

main 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); 
    }

1 个答案:

答案 0 :(得分:0)

有很多方法可以实现它。建议谷歌使用Picasso。在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);