我试图在OnPostExecute中使用else if语句,所以当点击一个按钮时会下载带有数据的API,从String源文件中获取一个正确的标题(在本例中为Honda或Suzuki),它们都放在一个包中并转移到另一项活动。然后标题用作我的DisplayDataActivity上的标题,数据被放入TextView。 所以我使用相同的活动来显示我的数据,只是使用不同的标题和数据。 总共有20个按钮,但是一旦我知道如何获得其中两个按钮,我将把它扩展到其余的按钮。 但是我无法在OnPostExecute中传递我的按钮ID(所以我可以根据点击的按钮id选择正确的else_if语句)。 有没有办法做到这一点? 我会非常感谢任何帮助!
!更新代码:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public class Wrap
{
public String dataPassedInOnClick;
public String resultData;
}
public class JSONTask extends AsyncTask<String, String, Wrap> {
private Context mContext;
private View rootView;
ProgressDialog pDialog;
JSONTask(Context mContext) {
this.mContext = mContext;
}
public JSONTask(Context context, View rootView){
this.mContext=context;
this.rootView=rootView;
}
@Override
protected Wrap doInBackground(String... params) {
HttpURLConnection connection = null;
BufferedReader reader = null;
try {
URL url = new URL(params[0]);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream stream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
Wrap wrapper=new Wrap();
wrapper.dataPassedInOnClick=params[0];
wrapper.resultData=buffer.toString();
return wrapper;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPreExecute(){
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Loading data...");
pDialog.show();
}
protected void onPostExecute(Wrap wrapResult) {
super.onPostExecute(wrapResult);
this.pDialog.dismiss();
if (wrapResult.dataPassedInOnClick.contains("api_suzuki")) {
String activity_title =
getResources().getString(R.string.suzuki);
Intent intent = new Intent(MainActivity.this,
DisplayDataActivity.class);
Bundle extras = new Bundle();
extras.putString("title", activity_title);
extras.putString("JSON_Object", wrapResult.resultData);
intent.putExtras(extras);
startActivity(intent);
}
else if (wrapResult.dataPassedInOnClick.contains("api_honda")) {
String activity_title =
getResources().getString(R.string.honda);
Intent intent = new Intent(MainActivity.this,
DisplayDataActivity.class);
Bundle extras = new Bundle();
extras.putString("title", activity_title);
extras.putString("JSON_Object", wrapResult.resultData);
intent.putExtras(extras);
startActivity(intent);
}
else if (wrapResult.dataPassedInOnClick.contains("api_kawasaki")) {
String activity_title =
getResources().getString(R.string.kawasaki);
Intent intent = new Intent(MainActivity.this,
DisplayDataActivity.class);
Bundle extras = new Bundle();
extras.putString("title", activity_title);
extras.putString("JSON_Object", wrapResult.resultData);
intent.putExtras(extras);
startActivity(intent);
}
else if (wrapResult.dataPassedInOnClick.contains("api_triumph")) {
String activity_title =
getResources().getString(R.string.triumph);
Intent intent = new Intent(MainActivity.this,
DisplayDataActivity.class);
Bundle extras = new Bundle();
extras.putString("title", activity_title);
extras.putString("JSON_Object", wrapResult.resultData);
intent.putExtras(extras);
startActivity(intent);
}
else if (wrapResult.dataPassedInOnClick.contains("api_bmw")) {
String activity_title =
getResources().getString(R.string.bmw);
Intent intent = new Intent(MainActivity.this,
DisplayDataActivity.class);
Bundle extras = new Bundle();
extras.putString("title", activity_title);
extras.putString("JSON_Object", wrapResult.resultData);
intent.putExtras(extras);
startActivity(intent);
}
else if (wrapResult.dataPassedInOnClick.contains("api_hyousung")) {
String activity_title =
getResources().getString(R.string.hyousung);
Intent intent = new Intent(MainActivity.this,
DisplayDataActivity.class);
Bundle extras = new Bundle();
extras.putString("title", activity_title);
extras.putString("JSON_Object", wrapResult.resultData);
intent.putExtras(extras);
startActivity(intent);
}
else if (wrapResult.dataPassedInOnClick.contains("api_ducati")) {
String activity_title =
getResources().getString(R.string.ducati);
Intent intent = new Intent(MainActivity.this,
DisplayDataActivity.class);
Bundle extras = new Bundle();
extras.putString("title", activity_title);
extras.putString("JSON_Object", wrapResult.resultData);
intent.putExtras(extras);
startActivity(intent);
}
else if (wrapResult.dataPassedInOnClick.contains("api_harley")) {
String activity_title =
getResources().getString(R.string.harley);
Intent intent = new Intent(MainActivity.this,
DisplayDataActivity.class);
Bundle extras = new Bundle();
extras.putString("title", activity_title);
extras.putString("JSON_Object", wrapResult.resultData);
intent.putExtras(extras);
startActivity(intent);
}
else if (wrapResult.dataPassedInOnClick.contains("api_aprilia")) {
String activity_title =
getResources().getString(R.string.aprilia);
Intent intent = new Intent(MainActivity.this,
DisplayDataActivity.class);
Bundle extras = new Bundle();
extras.putString("title", activity_title);
extras.putString("JSON_Object", wrapResult.resultData);
intent.putExtras(extras);
startActivity(intent);
}
else if (wrapResult.dataPassedInOnClick.contains("api_ktm")) {
String activity_title =
getResources().getString(R.string.ktm);
Intent intent = new Intent(MainActivity.this,
DisplayDataActivity.class);
Bundle extras = new Bundle();
extras.putString("title", activity_title);
extras.putString("JSON_Object", wrapResult.resultData);
intent.putExtras(extras);
startActivity(intent);
}
else if (wrapResult.dataPassedInOnClick.contains("api_mv")) {
String activity_title =
getResources().getString(R.string.mv);
Intent intent = new Intent(MainActivity.this,
DisplayDataActivity.class);
Bundle extras = new Bundle();
extras.putString("title", activity_title);
extras.putString("JSON_Object", wrapResult.resultData);
intent.putExtras(extras);
startActivity(intent);
}
else if (wrapResult.dataPassedInOnClick.contains("api_bajaj")) {
String activity_title =
getResources().getString(R.string.bajaj);
Intent intent = new Intent(MainActivity.this,
DisplayDataActivity.class);
Bundle extras = new Bundle();
extras.putString("title", activity_title);
extras.putString("JSON_Object", wrapResult.resultData);
intent.putExtras(extras);
startActivity(intent);
}
else if (wrapResult.dataPassedInOnClick.contains("royal")) {
String activity_title =
getResources().getString(R.string.royal);
Intent intent = new Intent(MainActivity.this,
DisplayDataActivity.class);
Bundle extras = new Bundle();
extras.putString("title", activity_title);
extras.putString("JSON_Object", wrapResult.resultData);
intent.putExtras(extras);
startActivity(intent);
}
else if (wrapResult.dataPassedInOnClick.contains("api_hero")) {
String activity_title =
getResources().getString(R.string.hero);
Intent intent = new Intent(MainActivity.this,
DisplayDataActivity.class);
Bundle extras = new Bundle();
extras.putString("title", activity_title);
extras.putString("JSON_Object", wrapResult.resultData);
intent.putExtras(extras);
startActivity(intent);
}
else if (wrapResult.dataPassedInOnClick.contains("api_indian")) {
String activity_title =
getResources().getString(R.string.indian);
Intent intent = new Intent(MainActivity.this,
DisplayDataActivity.class);
Bundle extras = new Bundle();
extras.putString("title", activity_title);
extras.putString("JSON_Object", wrapResult.resultData);
intent.putExtras(extras);
startActivity(intent);
}
}
}
public void onSuzuki(View view) {
JSONTask objJSONTask=new JSONTask(MainActivity.this);
objJSONTask.execute("api_suzuki");
}
public void onHonda(View view) {
JSONTask objJSONTask=new JSONTask(MainActivity.this);
objJSONTask.execute("api_honda");
}
public void onKawasaki(View view) {
JSONTask objJSONTask=new JSONTask(MainActivity.this);
objJSONTask.execute("api_kawasaki");
}
public void onTriumph(View view) {
JSONTask objJSONTask=new JSONTask(MainActivity.this);
objJSONTask.execute("api_triumph");
}
public void onBmw(View view) {
JSONTask objJSONTask=new JSONTask(MainActivity.this);
objJSONTask.execute("api_bmw");
}
public void onHyuosung(View view) {
JSONTask objJSONTask=new JSONTask(MainActivity.this);
objJSONTask.execute("api_hyuosung");
}
public void onDucati(View view) {
JSONTask objJSONTask=new JSONTask(MainActivity.this);
objJSONTask.execute("api_ducati");
}
public void onHarley(View view) {
JSONTask objJSONTask=new JSONTask(MainActivity.this);
objJSONTask.execute("api_harley");
}
public void onAprilia(View view) {
JSONTask objJSONTask=new JSONTask(MainActivity.this);
objJSONTask.execute("api_aprilia");
}
public void onKtm(View view) {
JSONTask objJSONTask=new JSONTask(MainActivity.this);
objJSONTask.execute("api_ktm");
}
public void onMv(View view) {
JSONTask objJSONTask=new JSONTask(MainActivity.this);
objJSONTask.execute("api_mv");
}
public void onBajaj(View view) {
JSONTask objJSONTask=new JSONTask(MainActivity.this);
objJSONTask.execute("api_bajaj");
}
public void onRoyal(View view) {
JSONTask objJSONTask=new JSONTask(MainActivity.this);
objJSONTask.execute("api_royal");
}
public void onHero(View view) {
JSONTask objJSONTask=new JSONTask(MainActivity.this);
objJSONTask.execute("api_hero");
}
public void onIndian(View view) {
JSONTask objJSONTask=new JSONTask(MainActivity.this);
objJSONTask.execute("api_indian");
}
答案 0 :(得分:1)
你正在为onPostExecute中的每个案例传递和获取rootView,当你试图通过调用int id = view.getId ();
来获取id时,你实际上每次都获得了rootView的id,所以你必须通过查看每个按钮或找出另一种方法。
在这种情况下你可以做的是比较你在onClick
方法中传递的字符串,因为你已经为每个案例传递了一个不同的字符串,你已经知道从哪里调用你的{{1方法,所以你不需要按下按钮的视图,因为它是多余的。
试试这个:
JSONTask
我们必须定义一个Wrap类,因为public class Wrap
{
public String dataPassedInOnClick;
public String resultData;
}
public class JSONTask extends AsyncTask<String, String, Wrap> {
private Context mContext;
private View rootView;
ProgressDialog pDialog;
JSONTask(Context mContext) {
this.mContext = mContext;
}
public JSONTask(Context context, View rootView){
this.mContext=context;
this.rootView=rootView;
}
@Override
protected Wrap doInBackground(String... params) {
HttpURLConnection connection = null;
BufferedReader reader = null;
try {
URL url = new URL(params[0]);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream stream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
Wrap wrapper=new Wrap();
wrapper.dataPassedInOnClick=params[0];
wrapper.resultData=buffer.toString();
return wrapper;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPreExecute(){
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Loading data...");
pDialog.show();
}
protected void onPostExecute(Wrap wrapResult) {
super.onPostExecute(wrapResult);
this.pDialog.dismiss();
if (wrapResult.dataPassedInOnClick.contains("Honda") ||
wrapResult.dataPassedInOnClick.contains("honda")) {
String activity_title =
getResources().getString(R.string.Honda);
Intent intent = new Intent(MainActivity.this,
DisplayDataActivity.class);
Bundle extras = new Bundle();
extras.putString("title", activity_title);
extras.putString("JSON_Object", wrapResult.resultData);
intent.putExtras(extras);
startActivity(intent);
} else if (wrapResult.dataPassedInOnClick.contains("Suzuki") ||
wrapResult.dataPassedInOnClick.contains("suzuki")) {
String activity_title =
getResources().getString(R.string.Suzuki);
Intent intent = new Intent(MainActivity.this,
DisplayDataActivity.class);
Bundle extras = new Bundle();
extras.putString("title", activity_title);
extras.putString("JSON_Object", wrapResult.resultData);
intent.putExtras(extras);
startActivity(intent);
}
}
}
方法只能返回一个对象。