2014-01-28 42 views
0

我有一個複雜的listview,我已經在列表視圖項上設置onclick,但我想爲listview的每一行做它,它應該打開那裏各自的活動(不同的活動)。我該怎麼辦?我的代碼是在這裏..如何在列表視圖行上設置onclick?

public class Content extends Activity { 

    String [] content = {"Linked List", "Write your own function", "programs", "Trees", "Sorting Techniques", "C Pointer", 
      "C Functions", "C Statements", "C Arrays", "C Variables", "C Structures", "C Macros", "C Headers", "C File Operations", 
      "C Declarations and Definitions", "C Functions-built-in", "C Functions-The Main Function", "OS Concepts", 
      "General Concepts", "Compiling and Linking"}; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.content); 

     ListView list = (ListView)findViewById(R.id.content_listView); 

     ContentAdapter ca = new ContentAdapter(); 
     list.setAdapter(ca); 

     list.setOnItemClickListener(new OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 
        long arg3) { 
       // TODO Auto-generated method stub 


       /*Here I want to do something that after 
       clicking on any listview row it should open there respective page 
       */ 

      } 
     }); 
    } 

    class ContentAdapter extends BaseAdapter { 

     @Override 
     public int getCount() { 
      // TODO Auto-generated method stub 
      return content.length; 
     } 

     @Override 
     public Object getItem(int arg0) { 
      // TODO Auto-generated method stub 
      return null; 
     } 

     @Override 
     public long getItemId(int arg0) { 
      // TODO Auto-generated method stub 
      return 0; 
     } 

     @Override 
     public View getView(int index, View v, ViewGroup vg) { 
      // TODO Auto-generated method stub 

      v = getLayoutInflater().inflate(R.layout.custom_content, null); 

      TextView content_txt = (TextView)v.findViewById(R.id.custom_content_textView); 

      content_txt.setText(content[index]); 

      return v; 
     } 

    } 

} 
+1

使用'content [arg2]'並使用intent和startActivity。 – Raghunandan

回答

0
lv.setOnItemClickListener(new OnItemClickListener() { 
public void onItemClick(AdapterView<?> parent, View view, 
    int position, long id) { 
    // When clicked, show a toast with the TextView text 
    if(position == 1) { 
     //code specific to first list item  
     Intent myIntent = new Intent(view.getContext(), Html_file1.class); 
     startActivityForResult(myIntent, 0); 
    } 

    if(position == 2) { 
     //code specific to 2nd list item  
     Intent myIntent = new Intent(view.getContext(), Html_file2.class); 
     startActivityForResult(myIntent, 0); 
    } 
} 

});

1
list.setOnItemClickListener(new OnItemClickListener() { 

    @Override 
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 
      long arg3) { 

      switch (arg2){ 
       case 0: 
        startActivity(new Intent(this,Activity2.class)); 
        break; 
       case 1: 
        // do something else 
        break; 
      } 
    }); 
0

有兩個選項可用。

  1. 正如其他人建議你可以使用。(推薦一個)

    lv.setOnItemClickListener(Your OnItemClickListener); 
    
  2. 由於每行是您R.layout.custom_content的一部分,所以你可以對XML的最頂部的佈局設置的onclick監聽器文件。這種最頂級的佈局將覆蓋所有行中的視圖。 (這不被推薦,但在可能的解決方案上)。

    v = getLayoutInflater().inflate(R.layout.custom_content, null); 
    TopMostLayoutType topmostlayout = (TopMostLayoutType)v.finViewById(R.id.topmostLayoutId); 
    topmostlayout.setOnClickListener(yourOnClickListener); 
    
0

使用枚舉而不是字符串,它也有利於利用碎片。

public enum Content implements Activity { 
        LINKED("Linked List"),WRITE ("Write your own function"),  
       PROGRAM("programs"),TREE ("Trees"),SORT("Sorting Techniques")}; 


    private String name; 


    Content(String name) { 
    this.name = name; 
} 

    @Override 
public String toString() { 

    return name; 
} 


@Override 
public Activity getActivity() 
    { 
    switch (this) 
    { 

    case LINKED: 

return (new Intent(this,Activity1.class)); 

    case WRITE: 

    return (new Intent(this,Activity2.class)); 



    default: 
     return null; 
    } 
     } 
相關問題