我试图在我的Android应用程序中使用jsoup库,我想在描述类中获取str1的值,并在MyAdapter类中将其用作items.add(new Item(str1,R.drawable.flag_one));我无法弄明白,任何帮助都非常感谢
public class HomeFragment extends Fragment {
// URL Address
String url = "http://www.livescore.com";
public HomeFragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.fragment_home, container, false);
getOverFlowMenu();
GridView gridView = (GridView)rootView.findViewById(R.id.gridview);
gridView.setAdapter(new MyAdapter(rootView.getContext()));
new Description();
return rootView;
}
private void getOverFlowMenu() {
}
//Description async task
class Description extends AsyncTask<Void, Void, Void>{
String desc;
@Override
protected Void doInBackground(Void... voids) {
try{
// Connect to the web site
Document document = Jsoup.connect(url).get();
Element content = document
.body().getElementsByClass("kubrick-info__title").first();
// Locate the content attribute
desc = content.text();
}catch (IOException e){
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result){
// Set description into TextView
String str1 = desc;
}
}
private class MyAdapter extends BaseAdapter
{
private List<Item> items = new ArrayList<Item>();
private LayoutInflater inflater;
public MyAdapter(Context context)
{
inflater = LayoutInflater.from(context);
items.add(new Item(str1, R.drawable.flag_one));
items.add(new Item("teams", R.drawable.flag_two));
}
@Override
public int getCount() {
return items.size();
}
@Override
public Object getItem(int i)
{
return items.get(i);
}
@Override
public long getItemId(int i)
{
return items.get(i).drawableId;
}
}
答案 0 :(得分:0)
您可以在static String str1 = ""
下方的顶部声明public class HomeFragment extends Fragment
吗?然后在onPostExecute
方法中:
@Override
protected void onPostExecute(Void result){
// Set description into TextView
str1 = desc;
}
这个想法是,如果String声明在顶部,它将在整个HomeFragment
类中可见。