2010-08-04 52 views
1

好吧,所以我有一個數組適應listview(數組適配是在另一個類中完成)..我只是得到了點擊監聽器爲列表工作,但現在我想要設置它,以便當我點擊一個項目時,它從被點擊的項目中抽取字符串,並將它們附加到新活動的意圖上。我想我應該使用intent.putextra但是我不知道如何拉正確的字符串對應於我點擊..我的代碼是下面的項..即時通訊只是失去了誠實如何從一個適合列表項的字符串中抽取一個點擊列表項的列表

//Initialize the ListView 
     lstTest = (ListView)findViewById(R.id.lstText); 

     //Initialize the ArrayList 
     alrts = new ArrayList<Alerts>(); 

     //Initialize the array adapter notice with the listitems.xml layout 
     arrayAdapter = new AlertsAdapter(this, R.layout.listitems,alrts); 

     //Set the above adapter as the adapter for the list 
     lstTest.setAdapter(arrayAdapter); 

     //Set the click listener for the list 
     lstTest.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      public void onItemClick(AdapterView adapterView, View view, int item, long arg3) { 
       Intent intent = new Intent(
         HomePageActivity.this, 
         PromotionActivity.class 
         ); 
       finish(); 
       startActivity(intent); 
      } 
     }); 

我的警示類..

public class Alerts { 

public String cityid; 
public String promoterid; 
public String promoshortcontent; 
public String promocontent; 
public String promotitle; 
public String locationid; 
public String cover; 

@Override 
public String toString() { 
    return "City: " +cityid+ " Promoter: " +promoterid+ "Short Promotion: " +promoshortcontent+ "Promotion: " +promocontent+ "Title: " +promotitle+ "Location: " +locationid+ "Cover: " +cover+ "$"; 
} 

}

anddddd我alertsadapter類..

public class AlertsAdapter extends ArrayAdapter<Alerts> { 

int resource; 
String response; 
Context context; 
//Initialize adapter 
public AlertsAdapter(Context context, int resource, List<Alerts> items) { 
    super(context, resource, items); 
    this.resource=resource; 

} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) 
{ 
    LinearLayout alertView; 
    //Get the current alert object 
    Alerts al = getItem(position); 

    //Inflate the view 
    if(convertView==null) 
    { 
     alertView = new LinearLayout(getContext()); 
     String inflater = Context.LAYOUT_INFLATER_SERVICE; 
     LayoutInflater vi; 
     vi = (LayoutInflater)getContext().getSystemService(inflater); 
     vi.inflate(resource, alertView, true); 
    } 
    else 
    { 
     alertView = (LinearLayout) convertView; 
    } 
    //Get the text boxes from the listitem.xml file 
    TextView textPromo =(TextView)alertView.findViewById(R.id.txtPromo); 
    TextView textPromoter =(TextView)alertView.findViewById(R.id.txtPromoter); 
    TextView textLocation =(TextView)alertView.findViewById(R.id.txtLocation); 

    //Assign the appropriate data from our alert object above 
    textPromo.setText(al.promocontent); 
    textPromoter.setText(al.promoterid); 
    textLocation.setText(al.locationid); 

    return alertView; 
} 

}

回答

0

讀出值了在週末有關如何解決這個問題的頓悟,我終於找到了一個很好的解決方案,我的應用程序..我知道它不是最優的,因爲我硬編碼數字100,但爲我的用途截至目前,我知道我不會曾經有許多列表項..

我加入的代碼,這兩位我alertsadapter類

int startzero = 0; 

public static String[][] promomatrix = new String[6][100]; 

promomatrix [0] [startzero] = al.cityid; promomatrix [1] [startzero] = al.promoterid; promomatrix [2] [startzero] = al.promocontent; promomatrix [3] [startzero] = al.promotitle; promomatrix [4] [startzero] = al.locationid; promomatrix [5] [startzero] = al.cover;

startzero++; 

然後跑到我homepageactivity類,並添加到點擊收聽

Intent intent = new Intent(
         HomePageActivity.this,PromotionActivity.class); 

       intent.putExtra("listitemcity", AlertsAdapter.promomatrix[0][pos]); 
       intent.putExtra("listitempromoter", AlertsAdapter.promomatrix[1][pos]); 
       intent.putExtra("listitemcontent", AlertsAdapter.promomatrix[2][pos]); 
       intent.putExtra("listitemtitle", AlertsAdapter.promomatrix[3][pos]); 
       intent.putExtra("listitemlocation", AlertsAdapter.promomatrix[4][pos]); 
       intent.putExtra("listitemcover", AlertsAdapter.promomatrix[5][pos]); 

       finish(); 
       startActivity(intent); 

終於走到我的promotionactivity(我在那裏試圖發送的字符串),並將此

Bundle extras = getIntent().getExtras(); 
     if (extras == null){ 
      return; 
     } 
     String listitemcity = extras.getString("listitemcity"); 
     String listitempromoter = extras.getString("listitempromoter"); 
     String listitemcontent = extras.getString("listitemcontent"); 
     String listitemtitle = extras.getString("listitemtitle"); 
     String listitemlocation = extras.getString("listitemlocation"); 
     String listitemcover = extras.getString("listitemcover"); 

像一個魅力工作..我希望這可以幫助別人:)

0

您需要使用onItemClick事件的參數

與PARAM名全更可讀的PARAM枚舉是

(AdapterView<?> parent, View view, int pos, long id) 

這意味着您有指示適配器中位置的參數pos

,你所要做的是:

  • 跳轉到pos在適配器
  • 從適配器
  • 使用putExtra上申請的意圖
+0

說實話我只編程了大約一個星期了..一切都完成了,通過在谷歌和使用本網站的研究教程..我知道如何做你的第三個子彈所以我一直試圖研究前兩個無濟於事。任何方式,你可以寫一些示例代碼或告訴我如何讀取適配器中某個位置的值的教程? – dootcher 2010-08-04 19:35:31

+0

ArrayAdapter有一個'getItem'方法,您可以使用它來獲取位置處的項目。這可能是一個字符串,所以你也完成了第2步。它會是這樣的:如果我瞭解該代碼的權利,然後這將適用於一個工作的字符串myItem = HomePageActivity.this.arrayAdapter.getItem(item)' – Pentium10 2010-08-04 20:16:19

+0

k listview是由列表項填充的,每個列表項只有一個相應的字符串..但是我忽略了提及每個列表項都有多個對應的字符串..檢查我的代碼上面,我剛剛添加..它的我的Alerts類和我的AlertsAdapter類這擴展了ArrayAdapter ,所以我想這使得它更復雜..對不起: -/ – dootcher 2010-08-04 20:31:14

相關問題