私は、jsoup
を使用してHTMLページから解析するリンクをリストアップしたアプリケーションを作成しました。Androidリストビューのアイテムのクリックが正しい画面に表示されない
リストにはリンクのタイトルとリンクが必要な矢印が表示されます。しかし、それらのアイテムを選択すると、私は最後のリンクだけを取得します。ここで
は私Controller
です:
public class Controller extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_view);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
toolbar.setTitle("");
toolbar.setSubtitle("");
linkList = new ArrayList<>();
http = this.getString(R.string.http);
url = this.getString(R.string.path1);
lv = (ListView) findViewById(R.id.list);
new GetLinks().execute();
}
private class GetLinks extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... arg0) {
Document doc;
Elements links;
try {
doc = Jsoup.connect(url).get();
links = doc.getElementsByClass("processlink");
linkList = ParseHTML(links);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
ListAdapter adapter = new SimpleAdapter(
Controller.this, linkList,
R.layout.list_item, new String[]{TITLE, LINK},
new int[]{R.id.title});
//Log.i(TAG, "onPostExecute: "+ getString(R.id.link));
lv.setAdapter(adapter);
}
}
private ArrayList<HashMap<String, String>> ParseHTML(Elements links) throws IOException {
if (links != null) {
ArrayList<HashMap<String, String>> linkList = new ArrayList<>();
for (Element link : links) {
final String linkhref = http + link.select("a").attr("href");
String linktext = link.text();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent,
View view, int position, long id) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse(linkhref));
startActivity(browserIntent);
}
});
HashMap<String, String> student = new HashMap<>();
student.put(TITLE, linktext);
student.put(LINK, linkhref);
linkList.add(student);
}
return linkList;
}
else {
Log.e(TAG, "Couldn't get html from server.");
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Couldn't get html from server. Check LogCat for possible errors!",
Toast.LENGTH_LONG)
.show();
}
});
}
return null;
}
}
ここで私はタイトルとリンクをログインした場合、私が手list_item.xml
<TextView
android:id="@+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="2dip"
android:paddingTop="6dip"
android:textColor="@color/colorPrimaryDark"
android:textSize="16sp"
android:textStyle="bold"
android:text="Title"
android:layout_toLeftOf="@+id/link"
android:layout_toStartOf="@+id/link" />
<ImageView
android:id="@+id/link"
android:layout_width="35dp"
android:layout_height="wrap_content"
android:gravity="end"
android:src="@drawable/arrow"
android:autoLink="web"
android:padding="1dp"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignBottom="@+id/title" />
、main_view.xml
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textView2" />
<TextView
android:layout_width="match_parent"
android:layout_height="30dp"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="@color/colorPrimary"
android:text="@string/app_name"
android:gravity="center"
android:id="@+id/textView2"
android:layout_below="@+id/toolbar"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
そして最後にではなく、少なくともですすべてのリンクとタイトルが、アプリではありません。
私の質問は、リンクが最終リンクだけでなくリストとして機能する方法です。
あなたのコードは、ベストプラクティスに従うか、POJOクラスを使用するか、文字列の文字列リストを維持する必要があります。私の助言はチュートリアルに従います。 –