0

我正在构建一个基于 android 的应用程序来实现 twitter 搜索功能。按钮单击仅适用于第一个。如果我更改搜索词并再次单击该按钮,则无法刷新。任何人都可以给我一个提示?`公共类 TwitterSActivity 扩展 Activity 实现 OnClickListener{ EditText etQuery; 按钮 btnQuery;

class Tweet{
    public String username;
    public String message;
    public String image_url;
}

ArrayList<Tweet> tweets = new ArrayList<Tweet>();

@Override
public void onCreate(Bundle savedInstanceState) {
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);

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

    etQuery = (EditText) findViewById (R.id.et);
    btnQuery = (Button) findViewById (R.id.btn);
    btnQuery.setOnClickListener(this);
}


@Override
public void onClick(View v){

    if(v.getId() == R.id.btn){
        Toast.makeText(this, "Query submitted", Toast.LENGTH_LONG).show();

        Getdata getdata = new Getdata();
        String returned = null;
        String searchTerm = etQuery.getText().toString();
        try {
            returned = getdata.getInternetData(searchTerm);
        } catch (Exception e) {
            e.printStackTrace();
        }

        try{
            JSONObject jo = new JSONObject(returned);
            JSONArray ja = jo.getJSONArray("results");
            for(int i=0; i<ja.length(); i++){
                JSONObject job = ja.getJSONObject(i);
                Tweet tt = new Tweet();
                tt.username = job.getString("from_user");
                tt.message = job.getString("text");
                tt.image_url = job.getString("profile_image_url");
                tweets.add(tt);
            }
        }
        catch(JSONException e){
            Log.e("log_tag", "Error parsing data: "+e.toString());
        }

        ListView lv = (ListView) findViewById(R.id.listView1);

        class FancyAdapter extends ArrayAdapter<Tweet> {

            public FancyAdapter() {
                super(TwitterSActivity.this, android.R.layout.simple_list_item_1, tweets);
            }

            public View getView(int position, View convertView, ViewGroup parent){
                ViewHolder holder;
                if(convertView == null){
                    LayoutInflater inflater = getLayoutInflater();
                    convertView = inflater.inflate(R.layout.listitem, null);
                    holder = new ViewHolder(convertView);
                    convertView.setTag(holder);
                }
                else
                {
                    holder = (ViewHolder)convertView.getTag();
                }
                holder.populatefrom(tweets.get(position));
                return(convertView);
            }


            class ViewHolder {
                public TextView username = null;
                public TextView message = null;
                public ImageView image = null;

                ViewHolder(View listitem){
                    username = (TextView)listitem.findViewById(R.id.username);
                    message = (TextView)listitem.findViewById(R.id.message);
                    image = (ImageView)listitem.findViewById(R.id.avatar);
                }

                void populatefrom(Tweet t){
                    username.setText(t.username);
                    message.setText(t.message);
                    image.setImageBitmap(getBitmap(t.image_url));
                }
            }
        }

        FancyAdapter ar = new FancyAdapter();
        lv.setAdapter(ar);

    }
}

public Bitmap getBitmap(String bitmapUrl) {
    try {
        URL url = new URL(bitmapUrl);
        return BitmapFactory.decodeStream(url.openConnection().getInputStream()); 
    }
    catch(Exception e) {return null;}
}`
4

1 回答 1

0

最后我弄清楚了,这是我修复它的方法:我更改ArrayList<Tweet> tweets = new ArrayList<Tweet>();final ArrayList<Tweet> tweets = new ArrayList<Tweet>(); 然后将其移动到 onClick() 方法下。没有 notifyDataSetChanged() 也能正常工作。

于 2012-05-13T16:18:31.197 回答