我现在已经工作了大约一个小时,并没有取得任何成功。我想要做的是为我的两个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>
我的ListView适配器的XML:
<?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>
提前致谢。
答案 0 :(得分:1)
替换
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")));
}
});
与
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
if(position == 0){
startActivity(new Intent(MainActivity.this, games.class));
}else
{
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://en.wikipedia.org/wiki/Coffee")));
}
}
});
您正在设置两个侦听器,因此始终考虑您设置的最后一个侦听器。因此,您总是最终导航到外部链接。
干杯,
SREE
答案 1 :(得分:0)
只使用一个
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
//Do If else statement here to cross check which item you clicked by using position
}
});