您有一个加载Twitter Feed的活动 Android Twitter String to Json Array
我想在顶部点击重新加载数据时放置一个刷新按钮。
到目前为止,我只是重新加载活动这是最好的方法吗?
package co.uk.fantasticmedia.TheEvoStikLeague;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.app.ListActivity;
public class TwitterActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.twitteract);
Button refresh = (Button) findViewById(R.id.btn_refresh);
//Listening to button event
refresh.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
//Starting a new Intent
Intent reload = new Intent(getApplicationContext(), TwitterActivity.class);
startActivity(reload);
}
});
try{
// Create a new HTTP Client
DefaultHttpClient defaultClient = new DefaultHttpClient();
// Setup the get request
HttpGet httpGetRequest = new HttpGet("https://api.twitter.com/1/statuses/user_timeline.json?screen_name=evostikleague&count=10");
// Execute the request in the client
HttpResponse httpResponse = defaultClient.execute(httpGetRequest);
// Grab the response
BufferedReader reader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent(), "UTF-8"));
String json = reader.readLine();
Log.v(json,"jsonfeed");
List<String> items = new ArrayList<String>();
//items.add(json);
JSONArray jArray = new JSONArray(json);
for (int i=0; i < jArray.length(); i++)
{ JSONObject oneObject = jArray.getJSONObject(i);
items.add(oneObject.getString("text"));
Log.i("items", "items");
}
setListAdapter ( new ArrayAdapter<String>(this, R.layout.single_item, items));
ListView list = getListView();
list.setTextFilterEnabled(true);
list.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), ((TextView) arg1).getText(),1000).show();
}
});
} catch(Exception e){
// In your production code handle any errors and catch the individual exceptions
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.home) {
startActivity(new Intent(TwitterActivity.this, HomeActivity.class));
return(true);
}
if (item.getItemId() == R.id.match) {
startActivity(new Intent(TwitterActivity.this, MatchActivity.class));
return(true);
}
if (item.getItemId() == R.id.teams) {
startActivity(new Intent(TwitterActivity.this, TeamsActivity.class));
return(true);
}
if (item.getItemId() == R.id.twitter) {
startActivity(new Intent(TwitterActivity.this, TwitterActivity.class));
return(true);
}
if (item.getItemId() == R.id.info) {
startActivity(new Intent(TwitterActivity.this, InfoActivity.class));
return(true);
}
return(super.onOptionsItemSelected(item));
}
}
答案 0 :(得分:1)
使用菜单提供刷新选项。因此,当用户按下手机上的菜单按钮时,他们可以选择刷新命令重新加载推文
答案 1 :(得分:1)
您可以发布剩余的活动吗?特别是您第一次加载列表时使用的部分。
可能没有必要重新启动整个活动。您只需将填充列表的代码部分移动到自己的方法中即可。然后,当用户想要刷新时,您可以调用该方法。
如果没有别的,你应该改变:
Intent reload = new Intent(getApplicationContext(), TwitterActivity.class);
到
Intent reload = new Intent(TwitterActivity.this, TwitterActivity.class);
编辑: 您需要将try / catch块移动到名为refresh()的新方法中。然后在您想要重新加载列表的任何时候调用该方法。像这样:
将网络移出主线程也是个好主意。
public class TwitterActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.twitteract);
Button refresh = (Button) findViewById(R.id.btn_refresh);
//Listening to button event
refresh.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
refresh();
}
});
refresh();
}
private void refresh(){
try{
// Create a new HTTP Client
DefaultHttpClient defaultClient = new DefaultHttpClient();
// Setup the get request
HttpGet httpGetRequest = new HttpGet("https://api.twitter.com/1/statuses/user_timeline.json?screen_name=evostikleague&count=10");
// Execute the request in the client
HttpResponse httpResponse = defaultClient.execute(httpGetRequest);
// Grab the response
BufferedReader reader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent(), "UTF-8"));
String json = reader.readLine();
Log.v(json,"jsonfeed");
List<String> items = new ArrayList<String>();
//items.add(json);
JSONArray jArray = new JSONArray(json);
for (int i=0; i < jArray.length(); i++)
{ JSONObject oneObject = jArray.getJSONObject(i);
items.add(oneObject.getString("text"));
Log.i("items", "items");
}
setListAdapter ( new ArrayAdapter<String>(this, R.layout.single_item, items));
ListView list = getListView();
list.setTextFilterEnabled(true);
list.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), ((TextView) arg1).getText(),1000).show();
}
});
} catch(Exception e){
// In your production code handle any errors and catch the individual exceptions
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.home) {
startActivity(new Intent(TwitterActivity.this, HomeActivity.class));
return(true);
}
if (item.getItemId() == R.id.match) {
startActivity(new Intent(TwitterActivity.this, MatchActivity.class));
return(true);
}
if (item.getItemId() == R.id.teams) {
startActivity(new Intent(TwitterActivity.this, TeamsActivity.class));
return(true);
}
if (item.getItemId() == R.id.twitter) {
startActivity(new Intent(TwitterActivity.this, TwitterActivity.class));
return(true);
}
if (item.getItemId() == R.id.info) {
startActivity(new Intent(TwitterActivity.this, InfoActivity.class));
return(true);
}
return(super.onOptionsItemSelected(item));
}
}
答案 2 :(得分:0)
这是更古老的刷新按钮,但我在我的应用程序中运行良好。
public void refresh(View view){ //refresh is onClick name
onRestart();
}
@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
Intent i = new Intent(lala.this, lala.class); //your class
startActivity(i);
finish();
}