简体   繁体   English

android按钮刷新

[英]android button refresh

I am building a android-based app to implement twitter search function. 我正在构建一个基于Android的应用程序来实现Twitter搜索功能。 The button click only works for the first. 单击按钮仅适用于第一次。 If i change the search term and click the button again, it fails to refresh. 如果我更改搜索词并再次单击该按钮,它将无法刷新。 Anyone can give me a hint? 有人可以给我提示吗? `public class TwitterSActivity extends Activity implements OnClickListener{ EditText etQuery; 公共类TwitterSActivity扩展Activity实现OnClickListener {EditText etQuery; Button btnQuery; 按钮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;}
}`

Finally i figure it out, here is how I fix it: I change ArrayList<Tweet> tweets = new ArrayList<Tweet>(); 终于我弄清楚了,这是我的解决方法:我更改ArrayList<Tweet> tweets = new ArrayList<Tweet>(); to final ArrayList<Tweet> tweets = new ArrayList<Tweet>(); final ArrayList<Tweet> tweets = new ArrayList<Tweet>(); and then move it under onClick() method. 然后将其移动到onClick()方法下。 It works well without notifyDataSetChanged(). 它在没有notifyDataSetChanged()的情况下工作良好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM