2015-11-07 79 views
0

我的應用程序包含一個列表視圖。當單擊列表視圖項目時,應打開包含文本的新活動。我想從我放入parse.com的.txt文件中檢索文本。但是當我點擊權限項目時,一個空白活動打開時沒有文本。我該怎麼辦?文本從.txt文件到textview,其中.txt文件從parse.com檢索

請幫我 這裏是我的代碼

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



} 

文本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; 
}} 

列表視圖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); 
} 

}

回答

0

這只是一個簡單的拼寫錯誤。您將字符串的名稱設置爲「text」,然後嘗試使用名稱「textfile」獲取此String。

你的onClick代碼:

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

活動:

  @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

,但我有我的parse.com對象我列「文本文件」下的.txt文件和我想從.txt文件檢索文本到我的活動 – user5524159

+0

Yeach,但你已經把意圖的結果「textfilelist.get(position).getText()」,然後在singleitemview活動你試圖從字符串意圖與名稱「文本文件」(它不存在)。你的意圖**不具有稱爲「文本文件」的項目。 **解決方法:**在適配器:'intent.putExtra( 「文本文件」, (textfilelist.get(位置).getTxtview()));' – michal3377

+0

我已經編輯,現在請幫助 – user5524159