我需要滚动视图和位置,以便知道要打开的链接。 Textviews和列表是不可点击的,因此我无法调用intent。我把它们设置为假,因为我读到了你需要对孩子们做些什么让它工作。我完全卡住了。
包com.example.user.siy;
import android.app.Fragment;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class EventsController extends Fragment{
View myView;
private ListView lvProduct;
private EventsListAdapter adapter;
private List<Events> mProductList;
ProgressDialog mProgressDialog;
String linkHref;
public static String link;
public ArrayList<String> beerList = new ArrayList<String>();
public ArrayList<String> beerList2 = new ArrayList<String>();
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
myView = inflater.inflate(R.layout.events, container, false);
new NewThread().execute();
return myView;
}
public class NewThread extends AsyncTask<String, Void, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Create a progressdialog
mProgressDialog = new ProgressDialog(getActivity());
// Set progressdialog message
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
// Show progressdialog
mProgressDialog.show();
}
@Override
protected String doInBackground(String... arg) {
mProductList = new ArrayList<>();
mProductList.clear();
Document doc;
try {
doc = Jsoup.connect("http://www.website.com/").get();
linkHref = new String("");
link = new String("");
Elements beer = doc.select("div[class=av-upcoming-events avia-
builder-el-22 el_after_av_hr avia-builder-el-last]");
Elements theH4 = beer.select("h4");
Elements links = beer.select("a"); //Sections
for (int j = 0; j < theH4.size(); j++) {
beerList.add( theH4.get(j).text());
}
for (org.jsoup.nodes.Element link : links) {
linkHref = link.attr("href");
beerList2.add(linkHref);
}
for (int f = 0; f < theH4.size(); f++) {
mProductList.add(new Events(beerList.get(f),
beerList2.get(f)));
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
//Add sample data for list
//We can get data from DB, webservice here
//Init adapter
lvProduct = (ListView) getActivity().findViewById(R.id.listView2);
adapter = new EventsListAdapter( getActivity(), mProductList);
lvProduct.setAdapter(adapter);
mProgressDialog.dismiss();
lvProduct.setOnItemClickListener(new
AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int
position, long id) {
//Do something
//Ex: display msg with product id get from view.getTag
link = beerList2.get(position);
Intent myIntent = new Intent(getActivity(), Info.class);
getActivity().startActivity(myIntent);
}
});
}
}
//public boolean isOnline() {
//ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
//NetworkInfo netInfo = cm.getActiveNetworkInfo();
//if (netInfo != null && netInfo.isConnectedOrConnecting()) {
//return true;
//}
//return false;
// }
public String getLink(){
return link;
}
}
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:focusable="false"
android:clickable="true"
android:focusableInTouchMode="false"
android:layout_height="fill_parent" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="@+id/list"
android:clickable="false"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView android:id="@+id/title2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dip"
android:text="Mooon Light Stuff"
android:textColor="#000000"
android:textSize="22sp"
android:clickable="false"
android:textStyle="bold"
/>
</LinearLayout>
</ScrollView>
Attempt to fix.
example = (TextView) getActivity().findViewById(R.id.title2);
example.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
link = beerList2.get(1);
Intent myIntent = new Intent(getActivity(), Info.class);
getActivity().startActivity(myIntent);
}
});
答案 0 :(得分:1)
以下是如何实现OnItemClickListener的工作示例。 确保您的类实现View.OnClickListener&amp; AdapterView.OnItemSelectedListener,如代码所示。 BTW OnItemClickListener 将使用scrollview或您使用的任何其他布局。在我的情况下,此代码可以正常使用滚动视图。
package com.smart.projects.activity;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.Toast;
import com.smartmob.projects.adapters.NavigationDrawerListAdapter;
import com.smartmob.projects.models.Items;
import java.util.ArrayList;
/**
* <p/>
* This activity will add Navigation Drawer for our application and all the code related to navigation drawer.
* We are going to extend all our other activites from this BaseActivity so that every activity will have Navigation Drawer in it.
* This activity layout contain one frame layout in which we will add our child activity layout.
*/
public class BaseActivity extends Activity implements View.OnClickListener, AdapterView.OnItemSelectedListener {
/**
* Static variable for selected item position. Which can be used in child activity to know which item is selected from the list.
*/
protected static int position;
/**
* This flag is used just to check that launcher activity is called first time so that we can open appropriate Activity on launch and make list item position selected accordingly.
*/
private static boolean isLaunch = true;
/**
* Frame layout: Which is going to be used as parent layout for child activity layout.
* This layout is protected so that child activity can access this
*/
protected FrameLayout frameLayout;
/**
* ListView to add navigation drawer item in it.
* We have made it protected to access it in child class. We will just use it in child class to make item selected according to activity opened.
*/
protected ListView mDrawerList;
/**
* List item array for navigation drawer items.
*/
protected String[] listArray = {"CML Monitor Features","About CML", "View CBC Report", "Add CBC Report", "View CBC Trend"};
protected ArrayList<Items> _items;
boolean dataExists = false;
/**
* Base layout node of this Activity
*/
private DrawerLayout mDrawerLayout;
/**
* Drawer listner class for drawer open, close etc.
*/
private ActionBarDrawerToggle actionBarDrawerToggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.navigation_drawer_base_layout);
frameLayout = (FrameLayout) findViewById(R.id.content_frame);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
// set a custom shadow that overlays the main content when the drawer opens
//mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
_items = new ArrayList<Items>();
_items.add(new Items("About CML", "In Menu click on 'About CML' to learn about CML (Chronic Myeloid Leukemia)", R.drawable.item_1));
_items.add(new Items("View Report", "In Menu click 'View Report' to view CBC Reports stored in the device", R.drawable.item_2));
_items.add(new Items("Add Record", "In Menu click 'Add Record' to add a new CBC Record", R.drawable.item_5));
_items.add(new Items("View CBC Trend", "In Menu click 'CBC trend' to view trends of each CBC parameter in CBC Report", R.drawable.item_4));
// _items.add(new Items("Tabular Report", "View tabular CBC Report", R.drawable.item_5));
//Adding header on list view
View header = getLayoutInflater().inflate(R.layout.list_view_header_layout, null);
mDrawerList.addHeaderView(header);
// set up the drawer's list view with items and click listener
mDrawerList.setAdapter(new NavigationDrawerListAdapter(this, _items));
mDrawerList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
openActivity(position);
Log.e("Logger:BaseActivity", "onItemClick-Enter");
}
});
// enable ActionBar app icon to behave as action to toggle nav drawer
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
// ActionBarDrawerToggle ties together the the proper interactions between the sliding drawer and the action bar app icon
actionBarDrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.drawable.apple_fruit, /* nav drawer image to replace 'Up' caret */
R.string.open_drawer, /* "open drawer" description for accessibility */
R.string.close_drawer) /* "close drawer" description for accessibility */ {
@Override
public void onDrawerClosed(View drawerView) {
getActionBar().setTitle(listArray[position]);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
super.onDrawerClosed(drawerView);
}
@Override
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(getString(R.string.app_name));
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
super.onDrawerOpened(drawerView);
}
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
super.onDrawerSlide(drawerView, slideOffset);
}
@Override
public void onDrawerStateChanged(int newState) {
super.onDrawerStateChanged(newState);
}
};
mDrawerLayout.setDrawerListener(actionBarDrawerToggle);
}
/*End of OnCreate*/
/*Start OnClick*/
/* Comment : Have to implement with the OnClickListner onClick is called when a view has been clicked.*/
public void onClick(View v) { // Parameter v stands for the view that was clicked.
Log.e("Logger:BaseActivity", "onClick:Enter");
final Context context = this;
}
/*End of onClick*/
/**
* @param position Launching activity when any list item is clicked.
*/
protected void openActivity(int position) {
Log.e("Logger:BaseActivity", "openActivity-Enter");
/**
* We can set title & itemChecked here but as this BaseActivity is parent for other activity,
* So whenever any activity is going to launch this BaseActivity is also going to be called and
* it will reset this value because of initialization in onCreate method.
* So that we are setting this in child activity.
*/
// mDrawerList.setItemChecked(position, true);
// setTitle(listArray[position]);
mDrawerLayout.closeDrawer(mDrawerList);
BaseActivity.position = position; //Setting currently selected position in this field so that it will be available in our child activities.
switch (position) {
case 0:
startActivity(new Intent(this, com.smartmob.projects.activity.HomeActivity.class));
break;
case 1:
startActivity(new Intent(this, AboutActivity.class));
break;
case 2:
startActivity(new Intent(this, com.smartmob.projects.activity.MainActivity.class));
break;
case 3:
startActivity(new Intent(this, com.smartmob.projects.activity.AddActivity.class));
break;
case 4:
startActivity(new Intent(this, TrendActivity.class));
break;
default:
break;
}
}
//@Override
public boolean onCreateOptionsMenu(Menu menu) {
Log.e("Logger:BaseActivity", "onCreateOptionsMenu-Enter");
getMenuInflater().inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Log.e("Logger:BaseActivity", "onOptionsItemSelected-Enter");
// The action bar home/up action should open or close the drawer.
// ActionBarDrawerToggle will take care of this.
if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
Log.e("Logger:BaseActivity", "onOptionsItemSelected-Enter"+item);
return true;
}
switch (item.getItemId()) {
case R.id.exit:
Log.e("Logger:BaseActivity", "onOptionsItemSelected-getItemId"+item.getItemId());
Toast.makeText(this, "Exit Application", Toast.LENGTH_LONG).show();
moveTaskToBack(true);
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(1);
return true;
/*case R.id.action_settings:
Log.e("Logger:BaseActivity", "onOptionsItemSelected-getItemId"+item.getItemId());
Toast.makeText(this, "Settings clicked", Toast.LENGTH_SHORT).show();
return true;*/
default:
return super.onOptionsItemSelected(item);
}
}
/* Called whenever we call invalidateOptionsMenu() */
//@Override
public boolean onPrepareOptionsMenu(Menu menu) {
Log.e("Logger:BaseActivity", "onPrepareOptionsMenu-Enter");
// If the nav drawer is open, hide action items related to the content view
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
//////// menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
menu.findItem(R.id.exit).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
/* We can override onBackPressed method to toggle navigation drawer*/
@Override
public void onBackPressed() {
Log.e("Logger:BaseActivity", "onBackPressed-Enter");
if (mDrawerLayout.isDrawerOpen(mDrawerList)) {
mDrawerLayout.closeDrawer(mDrawerList);
} else {
mDrawerLayout.openDrawer(mDrawerList);
}
}
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
Log.e("Logger:BaseActivity", "onItemSelected-Enter");
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
}