2015-12-15 85 views
-1

我一直在为此工作大概一个小时,并没有取得任何成功。我想要做的是对我的两个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> 

在此先感谢。

+0

为什么你有两个onItemClickListners_ – Dhina

+0

@Dhina我不知道我自己。这显然是错误的,可能是我的初学者错误,试图让它变得比需要的更复杂。 –

回答

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

+0

它的工作,谢谢。 –

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 
      } 
     });