2012-10-11 113 views
0

這是我的代碼點擊自定義列表視圖。當我點擊標題時,它可以工作,但在標題之後它不起作用。 CustomAdapter是我的應用程序中的另一個類,其中我定義了標題和listview的所有變量。請幫我解決這個問題。點擊項目的自定義列表視圖不工作

 import android.app.Activity; 
     import android.content.Intent; 
     import android.os.Bundle; 
     import android.text.Html; 
     import android.view.View; 
     import android.widget.AdapterView; 
     import android.widget.Toast; 
     import android.widget.AdapterView.OnItemClickListener; 
     import android.widget.ListView; 

     public class ProbabilityConditional extends Activity { 
String htmlcodefor_root = "&#x221A", htmlcodefor_multiply = "&#xD7", 
htmlcodefor_divide = "&#xF7", htmlcodefor_underroot = "&#00B3"; 
ListView listView1; 
String htmlcodefor_space = "&#8194", htmlcodefor_pi = "&#928", 
htmlcodefor_largespace = "&#8195"; 
String htmlcodefor_implies = "&#x21D2"; 
String htmlcodefor_i = "&#7522"; 
String htmlcodefor_angle = "&#952"; 
String htmlcodefor_overline = "&#x203E", htmlcodefor_plusminus = "&#177"; 



// TextView txtv; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    // txtv = (TextView)findViewById(R.id.txtTitle); 
    // txtv.setText(Html.fromHtml("1.(constant)<sup><small>0</></> = 1")); 
    listView1 = (ListView) findViewById(R.id.listView1); 

    CustomAdapter.formula_one_custom_adapter_class_var = Html.fromHtml("1 "); 
    CustomAdapter.formula_two_custom_adapter_class_var = Html.fromHtml("2 "); 

    CustomItemCall formula_data[] = new CustomItemCall[] { 
       new               CustomItemCall(CustomAdapter.formula_one_custom_adapter_class_var), 
      new CustomItemCall(CustomAdapter.formula_two_custom_adapter_class_var), 

    }; 
    CustomAdapter adapter = new CustomAdapter(this,R.layout.listview_item_row, formula_data); 

    View header = (View) getLayoutInflater().inflate(R.layout.listview_header_row, null); 
    listView1.addHeaderView(header); 

    listView1.setAdapter(adapter); 
    listView1.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View view,int position, long id) { 
      System.out.println(position); 
      Toast.makeText(ProbabilityConditional.this,position + " " , Toast.LENGTH_LONG).show(); 
      // When clicked, show a toast with the TextView text 
      if (position == 1) { 
       startActivity(new Intent(ProbabilityConditional.this,ProbabilityConditionalDiscrete.class)); 
      } if (position == 2) { 
       startActivity(new Intent(ProbabilityConditional.this,ProbabilityConditionalContinuous.class)); 
      } 

     } 
    }); 
} 

} 

回答

0

讓你列表視圖可聚焦真正在你的XML android:focusable="true" 添加這些行,使列表的其他項目作虛假android:focusable="false "

listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
       public void onItemClick(AdapterView<?> parent, View view,int position, long id) { 
        System.out.println(position); 
        Toast.makeText(ProbabilityConditional.this,position + " " , Toast.LENGTH_LONG).show(); 
        // When clicked, show a toast with the TextView text 
        if (position == 1) { 
         startActivity(new Intent(ProbabilityConditional.this,ProbabilityConditionalDiscrete.class)); 
        } if (position == 2) { 
         startActivity(new Intent(ProbabilityConditional.this,ProbabilityConditionalContinuous.class)); 
        } 

       } 
      }); 
+0

感謝您的答覆。我已經使用了一個,但它不工作。 – jhon

+0

更新我的答案上使用項目點擊這個代碼。 –

+0

感謝很大的反響。但它不適合我....請你給我你的電子郵件??,以便我可以發送我的aap的總代碼,我認爲這將是對你的幫助。再次感謝.... – jhon

0

寫這樣的代碼,簡單的方法: - 的ListView列表; list.setOnItemClickListener(新OnItemClickListener(){

 public void onItemClick(AdapterView parent, View view, 
       int position, long id) 
     { 
       //if in future you need to start a new activity 
       //then add below line also in your activity 

     Intent in = new Intent(MainActivity.this, SecondActivity.class); 
         startActivity(in); 
          }         
      }); 
相關問題