1

我想在一行中包含imageview和textview的多個項目的listview。現在我只想訪問listview的一行圖像視圖,然後使用它通過單擊它開始一個新的意圖。 這是我的主要活動:在列表視圖中只使用imageview來開始一個新的意圖

public class MTCRichGraphicsActivity extends Activity { 
int ELEMENT_COUNT = 1; 
int position=0; 
ImageButton bAddView; 
ImageView bEventPage; 
int r =0 ; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    bAddView = (ImageButton) findViewById(R.id.bNewEvent); 
    bEventPage = (ImageView) findViewById(R.id.ivEvent); 
    final ListView list = (ListView) findViewById(R.id.list3d); 

    bAddView.setOnClickListener(new View.OnClickListener() { 
    List<String> element = new ArrayList<String>(); 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      switch(v.getId()) 
      { 
      case R.id.bNewEvent : 
       //String[] elements = new String[ELEMENT_COUNT]; 
       //for (int i = 0; i< ELEMENT_COUNT; i++) { 
        //elements[i] = String.valueOf(i); 
        element.add(String.valueOf(ELEMENT_COUNT++)); 
       //} 
       final MyAdapter adapter = new MyAdapter(MTCRichGraphicsActivity.this,element); 
       //list.setScrollY(currentPosition); 
       //list.setTranslationY(currentPosition); 
       list.setDivider(null); 
       list.setAdapter(adapter); 
       list.setTranscriptMode(1); 
       list.setStackFromBottom(true); 
       //ELEMENT_COUNT = ELEMENT_COUNT + 1; 
       adapter.notifyDataSetChanged();       
      } 

     } 
    }); 

    list.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> parent, View v, int position, 
       long id) { 
      // TODO Auto-generated method stub 

      bEventPage.setFocusable(false); 
      bEventPage.setOnClickListener(new View.OnClickListener() { 

       @Override 
       public void onClick(View v) { 
        // TODO Auto-generated method stub 
        Intent i = new Intent("de.inovex.mtc.EventsPage"); 
        startActivity(i); 
       } 
      }); 

     } 

    }); 

} 

}

現在這裏bEventPage是一個ImageView的,當我點擊任何列表視圖排整排的被突出顯示,並給了我一個空指針異常,但ImageView的是沒有點擊並且新的活動沒有開始。 這裏是我的適配器類:

public class MyAdapter extends BaseAdapter { 

private final LayoutInflater mInflater; 
private final List<String> mItems; 
TextView t; 
ViewHolder holder; 
public MyAdapter(Activity c,List<String> objects) { 
    mInflater = c.getLayoutInflater(); 
    mItems = objects; 
} 

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


@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    TextView t; 
    if (convertView == null) { 
     convertView = mInflater.inflate(R.layout.listitem, parent,false); 
} 
    holder = new ViewHolder(); 
    holder.t1 = (TextView) convertView.findViewById(R.id.textView1); 
    holder.t2 = (TextView) convertView.findViewById(R.id.textView2); 
    holder.t3 = (TextView) convertView.findViewById(R.id.textView3); 
    holder.t4 = (TextView) convertView.findViewById(R.id.textView4); 
    holder.bEventPage = (ImageView) convertView.findViewById(R.id.ivEvent); 
    holder.t1.setText("Title"+position); 


    //((ImageView)convertView).setTextAlignment(1); 

    return convertView; 
} 

@Override 
public Object getItem(int position) { 
    return mItems.get(position); 
} 

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

}

所以,請告訴我如何只使用一排的ImageView根據其在列表視圖位置,開始一個新的意圖是什麼?

+0

GetView方法您可以添加'onClickListener刪除'爲您的'holder.bEventPage' – Triode 2013-03-26 13:27:18

+0

@RajeshCP我已經在我的主要活動中完成了它,但是我無法在適配器類中執行此操作,因爲startActivity對於我的適配器類未定義。 – Jaycee 2013-03-26 16:11:35

回答

1

的偵聽器添加到在adapter..and圖像視圖從列表項偵聽器

Activity activity; 

public MyAdapter(Activity c,List<String> objects) { 
    mInflater = c.getLayoutInflater(); 
    mItems = objects; 
    activity = c; 
} 

public View getView(int position, View convertView, ViewGroup parent) { 
TextView t; 
if (convertView == null) { 
    convertView = mInflater.inflate(R.layout.listitem, parent,false); 
} 
holder = new ViewHolder(); 
holder.t1 = (TextView) convertView.findViewById(R.id.textView1); 
holder.t2 = (TextView) convertView.findViewById(R.id.textView2); 
holder.t3 = (TextView) convertView.findViewById(R.id.textView3); 
holder.t4 = (TextView) convertView.findViewById(R.id.textView4); 
holder.bEventPage = (ImageView) convertView.findViewById(R.id.ivEvent); 
holder.t1.setText("Title"+position); 


holder.bEventPage.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       Intent i = new Intent("de.inovex.mtc.EventsPage"); 
       activity.startActivity(i); 
      } 
     }); 

return convertView; 
} 
+0

我不能這樣做,因爲這裏的startActivity(i)對於myadapter類是未定義的。 startActivity僅爲Activity類定義,但無法擴展它,因爲我已經擴展了BaseAdapter類。 – Jaycee 2013-03-26 16:14:37

+0

你已經將活動傳遞給你的適配器,使用它,檢查更新的答案 – Nermeen 2013-03-27 07:44:10

0

可以設置的onClick監聽onImageView在MyAdapter

+0

我不能這樣做,因爲我想要一個新的意圖啓動他們和startActivity(intent)未定義爲我的適配器類 – Jaycee 2013-03-26 16:12:56

相關問題