簡體   English   中英

Twitter feed列表視圖問題

[英]twitter feed list view issue

我正在開發一個從特定查詢中檢索Twitter提要的應用程序。我編寫了以下查詢。String url =“ http://search.twitter.com/search.json?q=sunburn”;

//String url="https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name=twitterapi&count=2";

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.mainlist);

        InputStream source = retrieveStream(url);

        Gson gson = new Gson();

        Reader reader = new InputStreamReader(source);

        SearchResponse response = gson.fromJson(reader, SearchResponse.class);

        Toast.makeText(this, response.query, Toast.LENGTH_SHORT).show();

        List<Result> results = response.results;

        for (Result result : results) {
            //Toast.makeText(this, result.fromUser, Toast.LENGTH_SHORT).show();
            Toast.makeText(this, result.text, Toast.LENGTH_LONG).show();


            SimpleArrayAdapter adapter = new SimpleArrayAdapter(this, result.text);

            setListAdapter(adapter);
}
}

我的陣列適配器是

public SimpleArrayAdapter(Activity context, String results) {
        super(context, R.layout.mainlist,results);

        // TODO Auto-generated constructor stub
        this.context = context;
        this.results = results;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = context.getLayoutInflater();
        View rowView = inflater.inflate(R.layout.mainlist, null, true);



        textView = (TextView) rowView.findViewById(R.id.);
        textView.setText(results);
        return rowView;
    }

問題是mainlist.xml無法解決。 我的listactivity在一個包中,而另一個在另一個包中。

這是我前一段時間做的一種方法。 但是我認為更好的選擇是先將數據保存在sqlite數據庫中,然后再從數據庫中填充listview。 現在這里是代碼:

JSONFunctions.class:

// You can get the data from your JSON URL with this class    
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
//import java.util.HashMap;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
//import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
//import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class JSONfunctions {

    public static JSONObject getJSONfromURL(String url){
        InputStream is = null;
        String result = "";
        JSONObject jArray = null;

        //http post
        try{
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost(url);
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();
                is = entity.getContent();

        }catch(Exception e){
                Log.e("log_tag", "Error in http connection "+e.toString());
        }

      //convert response to string
        try{
                BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
                StringBuilder sb = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) {
                        sb.append(line + "\n");
                }
                is.close();
                result=sb.toString();
        }catch(Exception e){
                Log.e("log_tag", "Error converting result "+e.toString());
        }

        try{

            jArray = new JSONObject(result);            
        }catch(JSONException e){
                Log.e("log_tag", "Error parsing data "+e.toString());
        }

        return jArray;
    }
}

MainActivity.class:

//Which gets the JSON and populate a listview with the data from it     
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONException;
import org.json.JSONObject;
import com.pxr.tutorial.xmltest.R;
import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class Main extends ListActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listplaceholder);

        ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
        JSONObject json = JSONfunctions.getJSONfromURL("http://api.stampii.com/?service=public&method=get_collections");


        try{

            JSONObject second = json.getJSONObject("result");
            for(int i=0;i<second.length();i++){                     
                HashMap<String, String> map = new HashMap<String, String>();

                JSONObject e = second.getJSONObject("4");

                map.put("id",  String.valueOf(i));  // collection 4
                map.put("name", "Alias: " + e.getString("alias"));
                map.put("posts", "Participants: " +  e.getString("participants"));
                mylist.add(map);            
            }       
        }catch(JSONException e)        {
             Log.e("log_tag", "Error parsing data "+e.toString());
        }

        ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.main, 
                        new String[] { "name", "posts","name5","posts5" }, 
                        new int[] { R.id.item_title, R.id.item_subtitle });

        setListAdapter(adapter);

        final ListView lv = getListView();
        lv.setTextFilterEnabled(true);  
        lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {              
                @SuppressWarnings("unchecked")
                HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);                   
                Toast.makeText(Main.this, "ID '" + o.get("id") + "' was clicked.", Toast.LENGTH_SHORT).show(); 

            }
        });
    }
}

main.xml:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="7dp"
    >
<TextView  
    android:id="@+id/item_title"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:padding="2dp"
    android:textSize="20dp" />
<TextView  
    android:id="@+id/item_subtitle"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:padding="2dp"
    android:textSize="13dp" />

</LinearLayout>

希望對您有幫助。 我可以向您保證此代碼可以正常工作。

由於應用程序中包含許多軟件包,請確保導入中沒有此軟件包:

import android.R;    // Remove it 

但:

import your.application.packagename.R;  // include this

更新:

從您的代碼中,我注意到了一件事,您正在嘗試創建一個新的ArrayAdapter並將其設置為在For循環內,將其寫入for循環外並進行嘗試。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM