从中检索文本。 txt文件到listview项的textview

时间:2015-11-06 10:20:07

标签: android android-listview parse-platform

我有一个包含listview的应用。每个listview项都包含大段文本。所以我想将文本放在.txt文件中,将其保存在parse.ccom上,并使用文本文件将文本接收到listview项目的textview中。但是当我按下listview项目时,文本文件中不会显示任何文本。只显示空白页面。

请帮帮我 这是我的代码

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).getText()));
                // 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);
}

}

0 个答案:

没有答案