问题描述
我在这条线得到一个错误的位置:
task.setOnArticleSelectedListener(本);
我得到的错误是:
setOnArticleSelectedListener
(com.beerportfolio.beerportfoliopro.PortfolioGetAllBeers.OnArticleSelectedListener)
在PortfolioGetAllBeers不能应用
至
(android.widget.AdapterView.OnItemSelectedListener)
这code出来的:
公共类组合扩展片段实现PortfolioGetAllBeers.OnArticleSelectedListener { 串beerId =; @覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
捆绑savedInstanceState){ //设置布局这里
最后查看theLayout = inflater.inflate(R.layout.activity_portfolio,集装箱,FALSE);
setHasOptionsMenu(真);
getActivity()的setTitle(风格)。 //获取用户信息
共享preferences preFS = preferenceManager.getDefaultShared preferences(getActivity());
字符串username = prefs.getString(username的,NULL);
字符串userid = prefs.getString(用户ID,NULL); 最后的微调portfolioType =(微调)theLayout.findViewById(R.id.portfolioSpinner);
portfolioType.setOnItemSelectedListener(新AdapterView.OnItemSelectedListener(){
@覆盖
公共无效onItemSelected(适配器视图<> parentView,查看selectedItemView,INT位置,长的id){ 字符串portfolioChoice = portfolioType.getSelectedItem()的toString()。
Log.d(组合,portfolioChoice); 如果(portfolioChoice.equals(全部)){
// TODO:明确第二微调
LL的LinearLayout =(的LinearLayout)theLayout.findViewById(R.id.addSpinnerLayout);
ll.removeAllViews(); //获取用户ID
//获取用户数据
共享preferences preFS = preferenceManager.getDefaultShared preferences(selectedItemView.getContext());
字符串userid = prefs.getString(用户ID,NULL); //构造URL
字符串URL =myURL;
//异步任务到这里
PortfolioGetAllBeers任务=新PortfolioGetAllBeers(getActivity());
task.setOnArticleSelectedListener(本);
task.execute(URL); } 否则如果(portfolioChoice.equals(啤酒)){ LL的LinearLayout =(的LinearLayout)theLayout.findViewById(R.id.addSpinnerLayout);
ll.removeAllViews();
LayoutInflater气筒= (LayoutInflater)selectedItemView.getContext().getSystemService(selectedItemView.getContext().LAYOUT_INFLATER_SERVICE);
视图V = inflater.inflate(R.layout.addspinner_layout,NULL); //膨胀addspinner
微调SP =(微调)v.findViewById(R.id.portfolioSpinner2); // portfolioSpinner2
ll.addView(五); //视图添加到线性布局 // TODO:让酿酒厂,并填写微调
//获取用户ID
//获取用户数据
共享preferences preFS = preferenceManager.getDefaultShared preferences(selectedItemView.getContext());
字符串userid = prefs.getString(用户ID,NULL); //构造URL
字符串URL =myURL2;
//异步任务到这里
新PortfolioGetAllBreweries(selectedItemView.getContext())执行(URL); }
否则,如果(portfolioChoice.equals(风格)){ LL的LinearLayout =(的LinearLayout)theLayout.findViewById(R.id.addSpinnerLayout);
ll.removeAllViews();
LayoutInflater气筒= (LayoutInflater)selectedItemView.getContext().getSystemService(selectedItemView.getContext().LAYOUT_INFLATER_SERVICE);
视图V = inflater.inflate(R.layout.addspinner_layout,NULL); //膨胀addspinner
微调SP =(微调)v.findViewById(R.id.portfolioSpinner2); // portfolioSpinner2
ll.addView(五); //视图添加到线性布局 //获取用户ID
//获取用户数据
共享preferences preFS = preferenceManager.getDefaultShared preferences(selectedItemView.getContext());
字符串userid = prefs.getString(用户ID,NULL); //构造URL
字符串URL =http://beerportfolio.com/app_getAllYourStyles.php?u=+用户ID;
// TODO:异步任务放在这里 新PortfolioGetAllStyles(selectedItemView.getContext())执行(URL); } 否则,如果(portfolioChoice.equals(等级)){ LL的LinearLayout =(的LinearLayout)theLayout.findViewById(R.id.addSpinnerLayout);
ll.removeAllViews();
LayoutInflater气筒= (LayoutInflater)selectedItemView.getContext().getSystemService(selectedItemView.getContext().LAYOUT_INFLATER_SERVICE);
视图V = inflater.inflate(R.layout.addspinner_layout,NULL); //膨胀addspinner
微调SP =(微调)v.findViewById(R.id.portfolioSpinner2); // portfolioSpinner2
ll.addView(五); //视图添加到线性布局 //获取用户ID
//获取用户数据
共享preferences preFS = preferenceManager.getDefaultShared preferences(selectedItemView.getContext());
字符串userid = prefs.getString(用户ID,NULL); //使阵列 //使啤酒的数组列表
最终名单<串GT; tasteList =新的ArrayList<串GT;();
tasteList.add(1);
tasteList.add(2);
tasteList.add(3);
tasteList.add(4);
tasteList.add(5); //微调的选择
微调微调=(微调)theLayout.findViewById(R.id.portfolioSpinner2); //数组的微调中的应用
ArrayAdapter<串GT; spinnerArrayAdapter =新ArrayAdapter<串GT;(selectedItemView.getContext(),android.R.layout.simple_spinner_item,tasteList);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); //下拉视图
spinner.setAdapter(spinnerArrayAdapter); // TODO:在选择添加的微调2
//添加的项目选择
最后的微调portfolioType =(微调)theLayout.findViewById(R.id.portfolioSpinner2);
portfolioType.setOnItemSelectedListener(新AdapterView.OnItemSelectedListener(){
@覆盖
公共无效onItemSelected(适配器视图<> parentView,查看selectedItemView,INT位置,长的id){ 字符串portfolioChoice = portfolioType.getSelectedItem()的toString()。 //Toast.makeText(((Activity)C).getApplicationContext(),portfolioChoice,Toast.LENGTH_LONG).show();
最终的ListView LV =(ListView控件)theLayout.findViewById(R.id.allYourBeersList);
lv.setAdapter(NULL); //获取啤酒厂的啤酒
//获取用户ID
//获取用户数据
共享preferences preFS = preferenceManager.getDefaultShared preferences(selectedItemView.getContext());
字符串userid = prefs.getString(用户ID,NULL); 尝试{ portfolioChoice = URLEn coder.en code(portfolioChoice,UTF-8);
}赶上(UnsupportedEncodingException五){
e.printStackTrace();
} //构造URL
字符串URL =myURL3; Log.d(组合,URL);
//异步任务到这里
新PortfolioGetAllBeers(selectedItemView.getContext())执行(URL); } @覆盖
公共无效onNothingSelected(适配器视图<> parentView){
// 没做什么
} });
} } @覆盖
公共无效onNothingSelected(适配器视图<> parentView){
// 没做什么
} });
//充气的布局该片段
返回theLayout; } @覆盖
公共无效onArticleSelected(BID字符串,字符串啤酒厂){ // code就点击执行
片段Fragment_one;
FragmentManager人= getFragmentManager();
FragmentTransaction TRAN = man.beginTransaction();
Fragment_one =新BeerPage();
最后的捆绑包=新包();
bundle.putString(beerIDSent,BID);
bundle.putString(breweryIDSent,啤酒厂);
Fragment_one.setArguments(包);
tran.replace(R.id.main,Fragment_one); // TRAN。
tran.addToBackStack(NULL);
tran.commit(); }}
然后异步任务code是:
公共类PortfolioGetAllBeers延伸的AsyncTask<弦乐,太虚,字符串> { 上下文℃;
私人ProgressDialog对话框; 公共PortfolioGetAllBeers(上下文的背景下)
{
C =背景;
对话=新ProgressDialog(C);
} @覆盖
保护字符串doInBackground(字符串...为arg0){
// TODO自动生成方法存根
返回readJSONFeed(为arg0 [0]);
} 在preExecute保护无效(){
Dialog.setMessage(获得啤酒); Dialog.setTitle(加载);
Dialog.setCancelable(假);
Dialog.show();
} // *************************** code上点击
OnArticleSelectedListener监听;
公共接口OnArticleSelectedListener {
公共无效onArticleSelected(myString的字符串,字符串啤酒厂); }
公共无效setOnArticleSelectedListener(OnArticleSelectedListener监听){
this.listener =侦听器;
}
// ****结束code代表的onClick 保护无效onPostExecute(字符串结果){
//德code JSON这里
尝试{
JSONArray jsonArray =新JSONArray(结果);
//存取权限列表视图
LV的ListView =(ListView控件)((活动)C).findViewById(R.id.allYourBeersList); //使啤酒的数组列表
最终名单< ShortBeerInfo> tasteList =新的ArrayList< ShortBeerInfo>(); 的for(int i = 0; I< jsonArray.length();我++){ 串啤酒= jsonArray.getJSONObject(ⅰ).getString(啤酒);
串率= jsonArray.getJSONObject(I).getString(速度);
字符串beerID = jsonArray.getJSONObject(I).getString(ID);
字符串BID = jsonArray.getJSONObject(I).getString(breweryID);
//创建对象
ShortBeerInfo tempTaste =新ShortBeerInfo(啤酒,速度,beerID,BID); //添加到ArrayList中
tasteList.add(tempTaste);
}
//项目添加到列表视图
ShortBeerInfoAdapter适配器1 =新ShortBeerInfoAdapter(C,R.layout.brewer_stats_listview,tasteList);
lv.setAdapter(适配器1); //设置点击
lv.setOnItemClickListener(新AdapterView.OnItemClickListener(){
@覆盖
公共无效onItemClick(适配器视图<>为arg0,ARG1查看,
INT ARG2,长ARG3){
ShortBeerInfo O =(ShortBeerInfo)arg0.getItemAtPosition(ARG2); 字符串tempID = o.id;
字符串tempBrewID = o.brewery;
// TODO:去啤酒页
listener.onArticleSelected(tempID,tempBrewID);
}
}); }
赶上(例外五){ } Dialog.dismiss(); } 公共字符串readJSONFeed(字符串URL){
StringBuilder的StringBuilder的=新的StringBuilder();
HttpClient的HttpClient的=新DefaultHttpClient();
HTTPGET HTTPGET =新HTTPGET(URL);
尝试{
HTT presponse响应= httpClient.execute(HTTPGET);
状态行状态行= response.getStatusLine();
INT状态code = statusLine.getStatus code();
如果(状态code == 200){
HttpEntity实体= response.getEntity();
为InputStream的InputStream = entity.getContent();
读者的BufferedReader =新的BufferedReader(
新的InputStreamReader(InputStream的));
串线;
而((行= reader.readLine())!= NULL){
stringBuilder.append(线);
}
inputStream.close();
}其他{
Log.d(JSON,无法下载文件);
}
}赶上(例外五){
Log.d(readJSONFeed,e.getLocalizedMessage());
}
返回stringBuilder.toString();
}}
更新code:的
公共类组合扩展片段实现PortfolioGetAllBeers.OnArticleSelectedListener { 串beerId =; @覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
捆绑savedInstanceState){ //设置布局这里
最后查看theLayout = inflater.inflate(R.layout.activity_portfolio,集装箱,FALSE);
setHasOptionsMenu(真);
getActivity()的setTitle(风格)。 //获取用户信息
共享preferences preFS = preferenceManager.getDefaultShared preferences(getActivity());
字符串username = prefs.getString(username的,NULL);
字符串userid = prefs.getString(用户ID,NULL); 最后的微调portfolioType =(微调)theLayout.findViewById(R.id.portfolioSpinner);
portfolioType.OnArticleSelectedListener(新AdapterView.OnItemSelectedListener(){
@覆盖
公共无效onItemSelected(适配器视图<> parentView,查看selectedItemView,INT位置,长的id){ 字符串portfolioChoice = portfolioType.getSelectedItem()的toString()。
Log.d(组合,portfolioChoice); 如果(portfolioChoice.equals(全部)){
// TODO:明确第二微调
LL的LinearLayout =(的LinearLayout)theLayout.findViewById(R.id.addSpinnerLayout);
ll.removeAllViews(); //获取用户ID
//获取用户数据
共享preferences preFS = preferenceManager.getDefaultShared preferences(selectedItemView.getContext());
字符串userid = prefs.getString(用户ID,NULL); //构造URL
字符串URL =myURL;
//异步任务到这里
PortfolioGetAllBeers任务=新PortfolioGetAllBeers(getActivity());
task.setOnArticleSelectedListener(本);
task.execute(URL); } 否则如果(portfolioChoice.equals(啤酒)){ LL的LinearLayout =(的LinearLayout)theLayout.findViewById(R.id.addSpinnerLayout);
ll.removeAllViews();
LayoutInflater气筒= (LayoutInflater)selectedItemView.getContext().getSystemService(selectedItemView.getContext().LAYOUT_INFLATER_SERVICE);
视图V = inflater.inflate(R.layout.addspinner_layout,NULL); //膨胀addspinner
微调SP =(微调)v.findViewById(R.id.portfolioSpinner2); // portfolioSpinner2
ll.addView(五); //视图添加到线性布局 // TODO:让酿酒厂,并填写微调
//获取用户ID
//获取用户数据
共享preferences preFS = preferenceManager.getDefaultShared preferences(selectedItemView.getContext());
字符串userid = prefs.getString(用户ID,NULL); //构造URL
字符串URL =myURL;
//异步任务到这里
新PortfolioGetAllBreweries(selectedItemView.getContext())执行(URL); }
否则,如果(portfolioChoice.equals(风格)){ LL的LinearLayout =(的LinearLayout)theLayout.findViewById(R.id.addSpinnerLayout);
ll.removeAllViews();
LayoutInflater气筒= (LayoutInflater)selectedItemView.getContext().getSystemService(selectedItemView.getContext().LAYOUT_INFLATER_SERVICE);
视图V = inflater.inflate(R.layout.addspinner_layout,NULL); //膨胀addspinner
微调SP =(微调)v.findViewById(R.id.portfolioSpinner2); // portfolioSpinner2
ll.addView(五); //视图添加到线性布局 //获取用户ID
//获取用户数据
共享preferences preFS = preferenceManager.getDefaultShared preferences(selectedItemView.getContext());
字符串userid = prefs.getString(用户ID,NULL); //构造URL
字符串URL =http://beerportfolio.com/app_getAllYourStyles.php?u=+用户ID;
// TODO:异步任务放在这里 新PortfolioGetAllStyles(selectedItemView.getContext())执行(URL); } 否则,如果(portfolioChoice.equals(等级)){ LL的LinearLayout =(的LinearLayout)theLayout.findViewById(R.id.addSpinnerLayout);
ll.removeAllViews();
LayoutInflater气筒= (LayoutInflater)selectedItemView.getContext().getSystemService(selectedItemView.getContext().LAYOUT_INFLATER_SERVICE);
视图V = inflater.inflate(R.layout.addspinner_layout,NULL); //膨胀addspinner
微调SP =(微调)v.findViewById(R.id.portfolioSpinner2); // portfolioSpinner2
ll.addView(五); //视图添加到线性布局 //获取用户ID
//获取用户数据
共享preferences preFS = preferenceManager.getDefaultShared preferences(selectedItemView.getContext());
字符串userid = prefs.getString(用户ID,NULL); //使阵列 //使啤酒的数组列表
最终名单<串GT; tasteList =新的ArrayList<串GT;();
tasteList.add(1);
tasteList.add(2);
tasteList.add(3);
tasteList.add(4);
tasteList.add(5); //微调的选择
微调微调=(微调)theLayout.findViewById(R.id.portfolioSpinner2); //数组的微调中的应用
ArrayAdapter<串GT; spinnerArrayAdapter =新ArrayAdapter<串GT;(selectedItemView.getContext(),android.R.layout.simple_spinner_item,tasteList);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); //下拉视图
spinner.setAdapter(spinnerArrayAdapter); // TODO:在选择添加的微调2
//添加的项目选择
最后的微调portfolioType =(微调)theLayout.findViewById(R.id.portfolioSpinner2);
portfolioType.setOnItemSelectedListener(新AdapterView.OnItemSelectedListener(){
@覆盖
公共无效onItemSelected(适配器视图<> parentView,查看selectedItemView,INT位置,长的id){ 字符串portfolioChoice = portfolioType.getSelectedItem()的toString()。 //Toast.makeText(((Activity)C).getApplicationContext(),portfolioChoice,Toast.LENGTH_LONG).show();
最终的ListView LV =(ListView控件)theLayout.findViewById(R.id.allYourBeersList);
lv.setAdapter(NULL); //获取啤酒厂的啤酒
//获取用户ID
//获取用户数据
共享preferences preFS = preferenceManager.getDefaultShared preferences(selectedItemView.getContext());
字符串userid = prefs.getString(用户ID,NULL); 尝试{ portfolioChoice = URLEn coder.en code(portfolioChoice,UTF-8);
}赶上(UnsupportedEncodingException五){
e.printStackTrace();
} //构造URL
字符串URL =myURL2; Log.d(组合,URL);
//异步任务到这里
新PortfolioGetAllBeers(selectedItemView.getContext())执行(URL); } @覆盖
公共无效onNothingSelected(适配器视图<> parentView){
// 没做什么
} });
} } @覆盖
公共无效onNothingSelected(适配器视图<> parentView){
// 没做什么
} });
//充气的布局该片段
返回theLayout; } @覆盖
公共无效onArticleSelected(BID字符串,字符串啤酒厂){ // code就点击执行
片段Fragment_one;
FragmentManager人= getFragmentManager();
FragmentTransaction TRAN = man.beginTransaction();
Fragment_one =新BeerPage();
最后的捆绑包=新包();
bundle.putString(beerIDSent,BID);
bundle.putString(breweryIDSent,啤酒厂);
Fragment_one.setArguments(包);
tran.replace(R.id.main,Fragment_one); // TRAN。
tran.addToBackStack(NULL);
tran.commit(); }}
速战速决是简单地改变这一行
task.setOnArticleSelectedListener(本);
到
task.setOnArticleSelectedListener(Portfolio.this);
说明:
您实例化的AdapterView.OnItemSelectedListener的内联函数与行
portfolioType.setOnItemSelectedListener(新AdapterView.OnItemSelectedListener(){...}
所以,当你调用setOnArticleSelectedListener()你这个内嵌类定义中这样做。因此,这种'不被编译器作为投资组合的当前实例的理解,而是作为AdapterView.OnItemSelectedListener的当前实例。但是内嵌定义访问封闭组合类及其成员。通过使用Portfolio.this你告诉编译器使用这种封装类的当前实例。
I am getting an error on this line here:
task.setOnArticleSelectedListener(this);
The error I am getting is:
setOnArticleSelectedListener
(com.beerportfolio.beerportfoliopro.PortfolioGetAllBeers.OnArticleSelectedListener)
in PortfolioGetAllBeers cannot be applied
to
(android.widget.AdapterView.OnItemSelectedListener)
That code comes out of:
public class Portfolio extends Fragment implements PortfolioGetAllBeers.OnArticleSelectedListener {
String beerId = "";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//set layout here
final View theLayout = inflater.inflate(R.layout.activity_portfolio, container, false);
setHasOptionsMenu(true);
getActivity().setTitle("Style");
//get user information
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
String userName = prefs.getString("userName", null);
String userID = prefs.getString("userID", null);
final Spinner portfolioType = (Spinner) theLayout.findViewById(R.id.portfolioSpinner);
portfolioType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
String portfolioChoice = portfolioType.getSelectedItem().toString();
Log.d("portfolio", portfolioChoice);
if( portfolioChoice.equals("All")){
//todo: clear second spinner
LinearLayout ll = (LinearLayout) theLayout.findViewById(R.id.addSpinnerLayout);
ll.removeAllViews();
//get userID
//get user data
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(selectedItemView.getContext());
String userID = prefs.getString("userID", null);
//construct url
String url = "myURL";
//async task goes here
PortfolioGetAllBeers task = new PortfolioGetAllBeers(getActivity());
task.setOnArticleSelectedListener(this);
task.execute(url);
}
else if (portfolioChoice.equals("Brewery")){
LinearLayout ll = (LinearLayout) theLayout.findViewById(R.id.addSpinnerLayout);
ll.removeAllViews();
LayoutInflater inflater = (LayoutInflater)selectedItemView.getContext().getSystemService(selectedItemView.getContext().LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.addspinner_layout, null); // inflate addspinner
Spinner sp = (Spinner) v.findViewById(R.id.portfolioSpinner2); //portfolioSpinner2
ll.addView(v); // add the view to the linear layout
//todo: get breweries and fill spinner
//get userID
//get user data
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(selectedItemView.getContext());
String userID = prefs.getString("userID", null);
//construct url
String url = "myURL2";
//async task goes here
new PortfolioGetAllBreweries(selectedItemView.getContext()).execute(url);
}
else if (portfolioChoice.equals("Style")){
LinearLayout ll = (LinearLayout) theLayout.findViewById(R.id.addSpinnerLayout);
ll.removeAllViews();
LayoutInflater inflater = (LayoutInflater)selectedItemView.getContext().getSystemService(selectedItemView.getContext().LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.addspinner_layout, null); // inflate addspinner
Spinner sp = (Spinner) v.findViewById(R.id.portfolioSpinner2); //portfolioSpinner2
ll.addView(v); // add the view to the linear layout
//get userID
//get user data
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(selectedItemView.getContext());
String userID = prefs.getString("userID", null);
//construct url
String url = "http://beerportfolio.com/app_getAllYourStyles.php?u=" + userID;
//todo: async task goes here
new PortfolioGetAllStyles(selectedItemView.getContext()).execute(url);
}
else if (portfolioChoice.equals("Rating")){
LinearLayout ll = (LinearLayout) theLayout.findViewById(R.id.addSpinnerLayout);
ll.removeAllViews();
LayoutInflater inflater = (LayoutInflater)selectedItemView.getContext().getSystemService(selectedItemView.getContext().LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.addspinner_layout, null); // inflate addspinner
Spinner sp = (Spinner) v.findViewById(R.id.portfolioSpinner2); //portfolioSpinner2
ll.addView(v); // add the view to the linear layout
//get userID
//get user data
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(selectedItemView.getContext());
String userID = prefs.getString("userID", null);
//make array
//make array list for beer
final List<String> tasteList = new ArrayList<String>();
tasteList.add("1");
tasteList.add("2");
tasteList.add("3");
tasteList.add("4");
tasteList.add("5");
// Selection of the spinner
Spinner spinner = (Spinner) theLayout.findViewById(R.id.portfolioSpinner2);
// Application of the Array to the Spinner
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(selectedItemView.getContext(), android.R.layout.simple_spinner_item,tasteList );
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // The drop down view
spinner.setAdapter(spinnerArrayAdapter);
//todo: add on select for spinner 2
//add on item selected
final Spinner portfolioType = (Spinner) theLayout.findViewById(R.id.portfolioSpinner2);
portfolioType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
String portfolioChoice = portfolioType.getSelectedItem().toString();
//Toast.makeText(((Activity) c).getApplicationContext(), portfolioChoice, Toast.LENGTH_LONG).show();
final ListView lv = (ListView) theLayout.findViewById(R.id.allYourBeersList);
lv.setAdapter(null);
//get brewery beers
//get userID
//get user data
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(selectedItemView.getContext());
String userID = prefs.getString("userID", null);
try {
portfolioChoice = URLEncoder.encode(portfolioChoice, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
//construct url
String url = "myURL3";
Log.d("portfolio" , url);
//async task goes here
new PortfolioGetAllBeers(selectedItemView.getContext()).execute(url);
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
// do nothing
}
});
}
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
// do nothing
}
});
// Inflate the layout for this fragment
return theLayout;
}
@Override
public void onArticleSelected(String bID, String brewery){
//code to execute on click
Fragment Fragment_one;
FragmentManager man= getFragmentManager();
FragmentTransaction tran = man.beginTransaction();
Fragment_one = new BeerPage();
final Bundle bundle = new Bundle();
bundle.putString("beerIDSent", bID);
bundle.putString("breweryIDSent", brewery);
Fragment_one.setArguments(bundle);
tran.replace(R.id.main, Fragment_one);//tran.
tran.addToBackStack(null);
tran.commit();
}
}
Then the async task code is:
public class PortfolioGetAllBeers extends AsyncTask<String, Void, String> {
Context c;
private ProgressDialog Dialog;
public PortfolioGetAllBeers (Context context)
{
c = context;
Dialog = new ProgressDialog(c);
}
@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
return readJSONFeed(arg0[0]);
}
protected void onPreExecute() {
Dialog.setMessage("Getting beers");
Dialog.setTitle("Loading");
Dialog.setCancelable(false);
Dialog.show();
}
//***************************code for on click
OnArticleSelectedListener listener;
public interface OnArticleSelectedListener{
public void onArticleSelected(String myString , String brewery);
}
public void setOnArticleSelectedListener(OnArticleSelectedListener listener){
this.listener = listener;
}
//*****************************end code for onClick
protected void onPostExecute(String result){
//decode json here
try{
JSONArray jsonArray = new JSONArray(result);
//acces listview
ListView lv = (ListView) ((Activity) c).findViewById(R.id.allYourBeersList);
//make array list for beer
final List<ShortBeerInfo> tasteList = new ArrayList<ShortBeerInfo>();
for(int i = 0; i < jsonArray.length(); i++) {
String beer = jsonArray.getJSONObject(i).getString("beer");
String rate = jsonArray.getJSONObject(i).getString("rate");
String beerID = jsonArray.getJSONObject(i).getString("id");
String bID = jsonArray.getJSONObject(i).getString("breweryID");
//create object
ShortBeerInfo tempTaste = new ShortBeerInfo(beer, rate, beerID, bID);
//add to arraylist
tasteList.add(tempTaste);
}
//add items to listview
ShortBeerInfoAdapter adapter1 = new ShortBeerInfoAdapter(c ,R.layout.brewer_stats_listview, tasteList);
lv.setAdapter(adapter1);
//set up clicks
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
ShortBeerInfo o=(ShortBeerInfo)arg0.getItemAtPosition(arg2);
String tempID = o.id;
String tempBrewID = o.brewery;
//todo: go to beer page
listener.onArticleSelected(tempID, tempBrewID);
}
});
}
catch(Exception e){
}
Dialog.dismiss();
}
public String readJSONFeed(String URL) {
StringBuilder stringBuilder = new StringBuilder();
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(URL);
try {
HttpResponse response = httpClient.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream inputStream = entity.getContent();
BufferedReader reader = new BufferedReader(
new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
}
inputStream.close();
} else {
Log.d("JSON", "Failed to download file");
}
} catch (Exception e) {
Log.d("readJSONFeed", e.getLocalizedMessage());
}
return stringBuilder.toString();
}
}
Updated Code:
public class Portfolio extends Fragment implements PortfolioGetAllBeers.OnArticleSelectedListener {
String beerId = "";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//set layout here
final View theLayout = inflater.inflate(R.layout.activity_portfolio, container, false);
setHasOptionsMenu(true);
getActivity().setTitle("Style");
//get user information
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
String userName = prefs.getString("userName", null);
String userID = prefs.getString("userID", null);
final Spinner portfolioType = (Spinner) theLayout.findViewById(R.id.portfolioSpinner);
portfolioType.OnArticleSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
String portfolioChoice = portfolioType.getSelectedItem().toString();
Log.d("portfolio", portfolioChoice);
if( portfolioChoice.equals("All")){
//todo: clear second spinner
LinearLayout ll = (LinearLayout) theLayout.findViewById(R.id.addSpinnerLayout);
ll.removeAllViews();
//get userID
//get user data
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(selectedItemView.getContext());
String userID = prefs.getString("userID", null);
//construct url
String url = "myURL";
//async task goes here
PortfolioGetAllBeers task = new PortfolioGetAllBeers(getActivity());
task.setOnArticleSelectedListener(this);
task.execute(url);
}
else if (portfolioChoice.equals("Brewery")){
LinearLayout ll = (LinearLayout) theLayout.findViewById(R.id.addSpinnerLayout);
ll.removeAllViews();
LayoutInflater inflater = (LayoutInflater)selectedItemView.getContext().getSystemService(selectedItemView.getContext().LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.addspinner_layout, null); // inflate addspinner
Spinner sp = (Spinner) v.findViewById(R.id.portfolioSpinner2); //portfolioSpinner2
ll.addView(v); // add the view to the linear layout
//todo: get breweries and fill spinner
//get userID
//get user data
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(selectedItemView.getContext());
String userID = prefs.getString("userID", null);
//construct url
String url = "myURL";
//async task goes here
new PortfolioGetAllBreweries(selectedItemView.getContext()).execute(url);
}
else if (portfolioChoice.equals("Style")){
LinearLayout ll = (LinearLayout) theLayout.findViewById(R.id.addSpinnerLayout);
ll.removeAllViews();
LayoutInflater inflater = (LayoutInflater)selectedItemView.getContext().getSystemService(selectedItemView.getContext().LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.addspinner_layout, null); // inflate addspinner
Spinner sp = (Spinner) v.findViewById(R.id.portfolioSpinner2); //portfolioSpinner2
ll.addView(v); // add the view to the linear layout
//get userID
//get user data
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(selectedItemView.getContext());
String userID = prefs.getString("userID", null);
//construct url
String url = "http://beerportfolio.com/app_getAllYourStyles.php?u=" + userID;
//todo: async task goes here
new PortfolioGetAllStyles(selectedItemView.getContext()).execute(url);
}
else if (portfolioChoice.equals("Rating")){
LinearLayout ll = (LinearLayout) theLayout.findViewById(R.id.addSpinnerLayout);
ll.removeAllViews();
LayoutInflater inflater = (LayoutInflater)selectedItemView.getContext().getSystemService(selectedItemView.getContext().LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.addspinner_layout, null); // inflate addspinner
Spinner sp = (Spinner) v.findViewById(R.id.portfolioSpinner2); //portfolioSpinner2
ll.addView(v); // add the view to the linear layout
//get userID
//get user data
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(selectedItemView.getContext());
String userID = prefs.getString("userID", null);
//make array
//make array list for beer
final List<String> tasteList = new ArrayList<String>();
tasteList.add("1");
tasteList.add("2");
tasteList.add("3");
tasteList.add("4");
tasteList.add("5");
// Selection of the spinner
Spinner spinner = (Spinner) theLayout.findViewById(R.id.portfolioSpinner2);
// Application of the Array to the Spinner
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(selectedItemView.getContext(), android.R.layout.simple_spinner_item,tasteList );
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // The drop down view
spinner.setAdapter(spinnerArrayAdapter);
//todo: add on select for spinner 2
//add on item selected
final Spinner portfolioType = (Spinner) theLayout.findViewById(R.id.portfolioSpinner2);
portfolioType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
String portfolioChoice = portfolioType.getSelectedItem().toString();
//Toast.makeText(((Activity) c).getApplicationContext(), portfolioChoice, Toast.LENGTH_LONG).show();
final ListView lv = (ListView) theLayout.findViewById(R.id.allYourBeersList);
lv.setAdapter(null);
//get brewery beers
//get userID
//get user data
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(selectedItemView.getContext());
String userID = prefs.getString("userID", null);
try {
portfolioChoice = URLEncoder.encode(portfolioChoice, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
//construct url
String url = "myURL2";
Log.d("portfolio" , url);
//async task goes here
new PortfolioGetAllBeers(selectedItemView.getContext()).execute(url);
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
// do nothing
}
});
}
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
// do nothing
}
});
// Inflate the layout for this fragment
return theLayout;
}
@Override
public void onArticleSelected(String bID, String brewery){
//code to execute on click
Fragment Fragment_one;
FragmentManager man= getFragmentManager();
FragmentTransaction tran = man.beginTransaction();
Fragment_one = new BeerPage();
final Bundle bundle = new Bundle();
bundle.putString("beerIDSent", bID);
bundle.putString("breweryIDSent", brewery);
Fragment_one.setArguments(bundle);
tran.replace(R.id.main, Fragment_one);//tran.
tran.addToBackStack(null);
tran.commit();
}
}
The quick fix is to simply change this line
task.setOnArticleSelectedListener(this);
to
task.setOnArticleSelectedListener(Portfolio.this);
Explanation:
You instantiated an inline definition of the AdapterView.OnItemSelectedListener with the line
portfolioType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {...}
so when you are calling setOnArticleSelectedListener() you are doing it within this inline class definition. Thus 'this' is not understood by the compiler as the current instance of Portfolio, but rather as the current instance of AdapterView.OnItemSelectedListener. However inline definition has access to enclosing Portfolio class and its members. With the use of 'Portfolio.this' you tell the compiler to use current instance of this enclosing class.
这篇关于获取试图异步任务使用的接口一个错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!