2016-11-07 45 views
0

我在Android中有一個ListView。每個listitem有2個textview。如果我點擊一個TextView我想打開一個Activity,如果我點擊另一個我想做另一件事。在列表行中多次點擊

我adapter's代碼是律這樣的:

public class LazyAdapterHelpCentersAlava extends BaseAdapter { 

    private Activity activity; 
    private ArrayList<HashMap<String, String>> data; 
    private static LayoutInflater inflater=null; 
    HashMap<String, String> song; 
    Typeface tf; 

    Context context; 

    public LazyAdapterHelpCentersAlava(Activity a, ArrayList<HashMap<String, String>> d, String font) { 
     activity = a; 
     data=d; 
     inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     tf = Typeface.createFromAsset(activity.getAssets(), font); 
    } 

    public int getCount() { 
     return data.size(); 
    } 

    public Object getItem(int position) { 
     return position; 
    } 

    public long getItemId(int position) { 
     return position; 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
     View vi=convertView; 
     if(convertView==null) 
     vi = inflater.inflate(R.layout.list_row_help_centers, null); 

     //TextView id = (TextView)vi.findViewById(R.id.id); // title 
     TextView title = (TextView)vi.findViewById(R.id.nombre); // title 
     TextView localizacion = (TextView)vi.findViewById(R.id.localizacion); 
     TextView email = (TextView)vi.findViewById(R.id.email); 
     TextView web = (TextView)vi.findViewById(R.id.web); 

     song = new HashMap<String, String>(); 
     song = data.get(position); 

     // Setting all values in listview 
     title.setText(song.get(Help.TAG_NOMBRE)); 
     web.setText(song.get(Help.TAG_TELEFONO)); 
     localizacion.setText(song.get(Help.TAG_DIRECCION)); 
     email.setText(song.get(Help.TAG_EMAIL)); 

     title.setTypeface(tf); 
     web.setTypeface(tf); 
     localizacion.setTypeface(tf); 
     email.setTypeface(tf); 

     web = (TextView)vi.findViewById(R.id.web); 
     web.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       // Need to start an Activity here  
      } 
     }); 

     return vi; 
    } 
} 

我已經把點擊監聽器web,但是,我不知道如何打開一個活動,因爲它說,像這樣方法錯誤startActivity(Intent)是不明確的類型新View.OnClickListener(){}

有人可以幫助我嗎?非常感謝。

+1

您需要在Context上調用startActivity。在你的情況下,你把它當作活動。調用activity.startActivity(intent); – X3Btel

+0

你可以使用我的代碼嗎?謝謝 –

+0

在onClick您需要開始活動。類型activity.startActivity(意圖) – X3Btel

回答

0

適配器的使用情境

 public View getView(int position, View convertView, ViewGroup parent) { 
     View vi=convertView; 
     if(convertView==null){ 
      context = parent.getContext(); 
     //other code 
     } 

然後

 web.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
       Intent redirect = new Intent(context,New.class); 
       context.startActivity(redirect); 
       } 
      }); 
+0

我在這一行中得到一個錯誤「Intent redirect = new Intent(context,New。類);「 –

+0

替換你的類名而不是新類 – sasikumar

+0

看我的編輯答案 – sasikumar

0

嘗試在構造函數活動

 web.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
      Intent redirect = new Intent(activity,New.class); 
      activity.startActivity(redirect); 
      } 
     }); 
0

這一點,因爲你給的上下文如果您的適配器是內部嵌套類你的MainActivity然後你需要做這樣的事情。

web.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     Intent newActivity = new Intent(MainActivity.this, NewActivity.class); 
     startActivity(newActivity); 
    } 
}); 

如果您的適配器是不是裏面嵌套類你MainActivity您需要在ActivityContext傳遞給LazyAdapterHelpCentersAlava第一,然後使用Context啓動Activity

public class LazyAdapterHelpCentersAlava extends BaseAdapter { 

    private ArrayList<HashMap<String, String>> data; 
    private static LayoutInflater inflater=null; 
    HashMap<String, String> song; 
    Typeface tf; 

    Context context; 

    public LazyAdapterHelpCentersAlava(Context ctx, ArrayList<HashMap<String, String>> d, String font) { 
     data=d; 
     inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     tf = Typeface.createFromAsset(activity.getAssets(), font); 
     context = ctx; // Initialize the context here. 
    } 

    // .. Here are the other functions ... Omitted for clarity 

    public View getView(int position, View convertView, ViewGroup parent) { 
     View vi=convertView; 
     if(convertView==null) 
     vi = inflater.inflate(R.layout.list_row_help_centers, null); 

     // Here are the other lines in getView function. Omitted for clarity 

     web = (TextView)vi.findViewById(R.id.web); 
     web.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       // Need to start an Activity here 
       Intent newActivity = new Intent(context, NewActivity.class); 
       context.startActivity(newActivity); 
      } 
     }); 

     return vi; 
    } 
} 
0

加入這一行onCreate()方法

this.context=this; 

加入這一項目下的網絡OnClick方法

Intent i = new Intent(context,NextActivity.class); 
context.startActivity(i); 

NextActivity是你必須去

0

使用活動上下文,

context.startActivity(intent); 
0

在你的onClick()方法,補充一點:

Intent startActivity=new Intent("abc.Xyz"); 
    startActivity(newActivity); 

在你的AndroidManifest.xml中,添加:

<activity android:name=".Xyz" 
       android:label="@string/app_name"> 
     <intent-filter> 
      <action android:name="abc.XYZ" /> 
      <category android:name="android.intent.action.DEFAULT" /> 
     </intent-filter> 
    </activity> 

注:在上面的代碼的Xyz是的類名要開始的活動和abc是其中存在Activity文件的PackageName。