从.txt文件到textview的文本,其中从parse.com检索.txt文件

时间:2015-11-07 09:54:29

标签: android android-listview parse-platform

我的应用包含列表视图。单击列表视图项时,应打开包含文本的新活动。我希望从我放入parse.com的.txt文件中检索文本。但是当我单击权限项目时,会打开一个没有文本的空白活动。我该怎么办?

请帮帮我 这是我的代码

main activity.java

package com.mycompany.myapp;

import android.app.*;
import android.os.*;
import com.parse.*;
import android.widget.*;
import java.util.*;
import android.util.*;

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("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();
        }
        }



}

text file.java

package com.mycompany.myapp;

public class Textfile

{

private String text;
private String image;
private String txtview;

public void setTxtview(String txtview)
{
    this.txtview = txtview;
}

public String getTxtview()
{
    return txtview;
}

public void setImage(String image)
{
    this.image = image;
}

public String getImage()
{
    return image;
}



public void setText(String text)
{
    this.text = text;
}

public String getText()
{
    return text;
}}

listview adapter.java

package com.mycompany.myapp;
import android.widget.*;
import android.view.*;
import android.content.*;
import java.util.*;
import android.view.View.*;

public class ListViewAdapter extends BaseAdapter
{

// Declare Variables
Context context;
LayoutInflater inflater;
ImageLoader imageLoader;
private List<Textfile> textfilelist = null;
private ArrayList<Textfile> arraylist;

public ListViewAdapter(Context context,
                       List<Textfile> textfilelist) {
    this.context = context;
    this.textfilelist = textfilelist;
    inflater = LayoutInflater.from(context);
    this.arraylist = new ArrayList<Textfile>();
    this.arraylist.addAll(textfilelist);
    imageLoader = new ImageLoader(context);

    }

public class ViewHolder {
    TextView heading;
    ImageView mainimage;
}

@Override
public int getCount()
{
    // TODO: Implement this method
    return textfilelist.size();
}

@Override
public Object getItem(int position)
{
    // TODO: Implement this method
    return textfilelist.get(position);
}

@Override
public long getItemId(int position)
{
    // TODO: Implement this method
    return position;
}

@Override
public View getView(final int position, View view, ViewGroup parent)
{
    final ViewHolder holder;
    if (view == null) {
        holder = new ViewHolder();
    view = inflater.inflate(R.layout.listview_item, null);
    holder.heading = (TextView) view.findViewById(R.id.listview_itemTextView);
    holder.mainimage = (ImageView) view.findViewById(R.id.listview_itemImageView);
    view.setTag(holder);
    }
    else{
        holder = (ViewHolder) view.getTag();
        }

        holder.heading.setText(textfilelist.get(position).getText());

    imageLoader.DisplayImage(textfilelist.get(position).getImage(),
                             holder.mainimage);


    view.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent(context, SingleItemView.class);
                intent.putExtra("text",
                                (textfilelist.get(position).getTxtview());
                // Pass all data flag
                intent.putExtra("image",
                                (textfilelist.get(position).getImage()));
                // Start SingleItemView Class

                context.startActivity(intent);
                }
                });



    // TODO: Implement this method
    return view;
 }

}

singleitemview.java

package com.mycompany.myapp;
import android.app.*;
import android.os.*;
import android.content.*;
import android.widget.*;

public class SingleItemView extends Activity
{
String text;

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

    Intent i = getIntent();

    text= i.getStringExtra("textfile");

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

    txt.setText(text);
}

}

1 个答案:

答案 0 :(得分:0)

这只是一个简单的错字。您将String放在名为“text”的intent上,然后您尝试使用名称“textfile”获取此String。

您的onClick代码:

nginx: [emerg] "location" directive is not allowed here in /etc/nginx/conf.d/default.conf:1
nginx: configuration file /etc/nginx/nginx.conf test failed

您的活动代码:

intent.putExtra("text", (textfilelist.get(position).getText()));

应该是:

text= i.getStringExtra("textfile");

修改 适配器:

text= i.getStringExtra("text");

活性:

        @Override
        public void onClick(View arg0) {
            Intent intent = new Intent(context, SingleItemView.class);
            intent.putExtra("text",
                            (textfilelist.get(position).getTxtview());
            // Pass all data flag
            intent.putExtra("image",
                            (textfilelist.get(position).getImage()));

            intent.putExtra("textfile",
                            (textfilelist.get(position).getTxtview()));
            // Start SingleItemView Class

            context.startActivity(intent);
            }
            });

如果其余的都没问题,那么