我需要滾動視圖和位置,以便知道打開哪個鏈接。 Textviews和列表是不可點擊的,因此我不能調用這個意圖。我讓他們設置爲假,因爲我讀了那些你需要做的事情讓孩子們去工作。我完全卡住了。
package com.example.user.siy;如何在Scroll View中使用SetItemClickListener?
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);
}
});
ScrollView沒有項目,它有一些視圖,你添加到它。您可以直接將偵聽器設置爲視圖。 –
是否這樣?示例=(TextView)getActivity()。findViewById(R.id.title2); example.setOnClickListener(新View.OnClickListener(){ @Override 公共無效的onClick(視圖v){ 鏈路= beerList2.get(1); 意圖myIntent =新意圖(getActivity(),Info.class) ; getActivity()。startActivity(myIntent); } }); 沒有我的應用程序mon't解析和崩潰。現在是空的。 – user6940221
這是另一個問題。你絕對做一些很奇怪的事情。 –