我想在一行中包含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根據其在列表視圖位置,開始一個新的意圖是什麼?
GetView方法您可以添加'onClickListener刪除'爲您的'holder.bEventPage' – Triode 2013-03-26 13:27:18
@RajeshCP我已經在我的主要活動中完成了它,但是我無法在適配器類中執行此操作,因爲startActivity對於我的適配器類未定義。 – Jaycee 2013-03-26 16:11:35