0

我使用擴展基本適配器的列表視圖適配器創建了一個列表視圖。如何在自定義列表視圖中設置OnitemClick

它工作正常。但我需要添加onitemClick監聽器到列表視圖,當我點擊一個列表項時,它會轉到另一個活動,以及listitem.how我們可以做到這一點嗎?

這是使用

int a,b; 
list = new ArrayList<HashMap<String,String>>(); 
for (a=0; a<outputString.length;a++){ 
    HashMap<String,String> temp1 = new HashMap<String,String>(); 
    temp1.put(FIRST_COLUMN,outputString[a][0]); 
    temp1.put(SECOND_COLUMN, outputString[a][1]); 
    temp1.put(THIRD_COLUMN, outputString[a][4]); 
    temp1.put(FOURTH_COLUMN, outputString[a][5]); 
    list.add(temp1); 
} 
listviewAdapter adapter = new listviewAdapter(this, list); 
lview.setAdapter(adapter); 

回答

1

與列表項一起

,如果你想從該列表項的一些數據

,只是已在監聽器中使用parent.getItemAtPosition(position)提及。這將返回相應的Object,您可以用

HashMap<String, String> map = (HashMap<String, String>) parent.getItemAtPosition(position); 
    String one = map.get("key"); 
    String two = map.get("key2"); 

    Intent next = new Intent(FirstActivity.this, NextActivity.class); 
    next.putExtra("One", one); 
    next.putExtra("Two", two); 
    startActivity(next); 

執行進一步的行動得到它在你的第二個活動:

Bundle extras = getIntent().getExtras(); 
String one = extras.getStringExtra("One"); 
String two = extras.getStringExtra("Two"); 
+0

嗨,我需要將該項目傳遞給下一個班級,這有多可能? – SibinF 2013-03-11 11:26:05

+0

您需要傳遞的不是項目本身,而是它所保存的數據。使用getItemAtPosition(position)將返回一個HashMap。使用get方法從這個HashMap獲取字符串,然後把它們作爲額外的意圖 – Droidman 2013-03-11 11:30:21

+0

雅我會嘗試yar .. thanx – SibinF 2013-03-11 11:31:31

1

在您的列表視圖這樣創建onItemClickListener我在ListView適配器

public class listviewAdapter extends BaseAdapter{ 
    String FIRST_COLUMN = Constant.FIRST_COLUMN; 
    String SECOND_COLUMN = Constant.SECOND_COLUMN; 
    String THIRD_COLUMN = Constant.THIRD_COLUMN; 
    String FOURTH_COLUMN = Constant.FOURTH_COLUMN; 
    public ArrayList<HashMap<String,String>> list; 
    Activity activity; 

    public listviewAdapter(Activity activity, ArrayList<HashMap<String,String>> list) { 
    super(); 
    this.activity = activity; 
    this.list = list; 
    } 

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

    @Override 
    public Object getItem(int position) { 
    // TODO Auto-generated method stub 
    return list.get(position); 
    } 

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

    private class ViewHolder { 
     TextView txtFirst; 
     TextView txtSecond; 
     TextView txtThird; 
     TextView txtFourth; 
    } 


    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
    // TODO Auto-generated method stub 
    View vi=convertView; 
    // TODO Auto-generated method stub 
      ViewHolder holder; 
      LayoutInflater inflater = activity.getLayoutInflater(); 

      if (convertView == null) 
      { 
       convertView = inflater.inflate(R.layout.requestcard_columnsdetails, null); 
       holder = new ViewHolder(); 




       holder.txtFirst = (TextView) convertView.findViewById(R.id.date); 
       holder.txtSecond = (TextView) convertView.findViewById(R.id.from); 
       holder.txtThird = (TextView) convertView.findViewById(R.id.to); 
       holder.txtFourth = (TextView) convertView.findViewById(R.id.time); 
       convertView.setTag(holder); 
      } 
      else 
      { 
       holder = (ViewHolder) convertView.getTag(); 
      } 

      HashMap<String, String> map = list.get(position); 
      holder.txtFirst.setText(map.get(FIRST_COLUMN)); 
      holder.txtSecond.setText(map.get(SECOND_COLUMN)); 
      holder.txtThird.setText(map.get(THIRD_COLUMN)); 
      holder.txtFourth.setText(map.get(FOURTH_COLUMN)); 



     return convertView; 
    } 
} 

我居住在主活動列表中的項目代碼:

lview.setOnItemClickListener(new OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView parent, View view, 
        int position, long id) { 
    Intent intent = new intent(mainactivity.this, target.class); 
    // maybe put some extra arguments to the intent 
    startActivity(intent); 

      } 
     }); 

要將您的額外數據用於下一個活動,請使用您的意圖: Intent doc

+0

我如何才能在未來的類ITM ID?我需要將點擊的項目傳遞給下一個類,它怎麼可能 – SibinF 2013-03-11 11:23:46

+0

intent.putExtra(「id」,id) 然後在第二個活動中恢復。 – staaar 2013-03-11 11:24:43

+0

我在我的答案中鏈接了文檔。它給你一個關於如何放置額外數據的更長的解釋。 – staaar 2013-03-11 11:25:16

1

使用setOnItemClickListener

lview.setOnItemClickListener(new OnItemClickListener() { 

    public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

     Intent intent = new Intent(Activity.this, secondActivity.class); 
    startActivity(intent); 
    } 
}); 
+0

我如何得到下一個類的項目ID – SibinF 2013-03-11 11:22:13

+0

我也需要通過項目數組,outputString [a] [0])這個值到下一個類 – SibinF 2013-03-11 11:22:54

+0

使用intent.putExtra(key,Value) – Nermeen 2013-03-11 11:24:03

1

試試這個:

list_view.setOnItemClickListener(new OnItemClickListener() { 

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

     Intent myIntent = new Intent(Current_Activity.this,Next_Activity.class); 
     startActivity(myIntent); 
} 
}); 
相關問題