我一直在爲此工作大概一個小時,並沒有取得任何成功。我想要做的是對我的兩個ListView對象應用不同的操作,但兩者都只響應一個,這是外部鏈接。我如何讓他們每個人都有自己的行爲?我可以根據要求Android - 自定義ListView的單獨鏈接?
這裏提供更多的代碼是我的代碼:
package net.androidbootcamp.gamesandcoffee;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
public class MainActivity extends Activity {
ListView listView;
int[] list_image_resource = {R.drawable.games, R.drawable.coffee};
String[] list_titles;
String[] list_descriptions;
ListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.list_view);
list_descriptions = getResources().getStringArray(R.array.description);
list_titles = getResources().getStringArray(R.array.titles);
int i=0;
adapter = new ListAdapter(getApplicationContext(),R.layout.row_layout);
listView.setAdapter(adapter);
for(String titles: list_titles)
{
GameDataProvider dataProvider = new GameDataProvider(list_image_resource[i],
titles, list_descriptions[i]);
adapter.add(dataProvider);
i++;
}
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
startActivity(new Intent(MainActivity.this, games.class));
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://en.wikipedia.org/wiki/Coffee")));
}
});
}
}
這裏是我的主XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="#BBBBBB"
>
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>
</RelativeLayout>
和XML爲我的ListView適配器:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="#BBBBBB">
<ImageView
android:id="@+id/layout_image"
android:layout_width="72dp"
android:layout_height="72dp"
android:layout_alignParentLeft="true"
android:src="@drawable/coffee"
/>
<TextView
android:id="@+id/item_title"
android:layout_width="match_parent"
android:layout_height="72dp"
android:layout_toRightOf="@+id/layout_image"
android:text="This is the title"
android:paddingTop="15dp"
android:paddingLeft="10dp"
android:textSize="20sp"
/>
<TextView
android:id="@+id/list_description"
android:layout_width="match_parent"
android:layout_height="72dp"
android:layout_toRightOf="@id/layout_image"
android:paddingTop="35dp"
android:paddingLeft="40dp"
android:text="This is the description"
/>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#000000"
android:layout_below="@+id/item_title">
</View>
</RelativeLayout>
在此先感謝。
爲什麼你有兩個onItemClickListners_ – Dhina
@Dhina我不知道我自己。這顯然是錯誤的,可能是我的初學者錯誤,試圖讓它變得比需要的更復雜。 –