2013-07-11 48 views
1

希望你們能幫上忙。打開多個列表視圖項目Click to One Class

我有一個處理所有10個圖像按鈕點擊和列表視圖意圖的活動。我期望做的是對所有列表視圖按鈕單擊有1個佈局。在這個佈局中調用不同的數據。當我開始這個項目時,我進行了很多活動,直到一個出色的堆棧溢出用戶指出我可以簡化它,並做了很多清理。

package com.example.testtest; 

    import android.app.Activity; 
    import android.graphics.Typeface; 
    import android.os.Bundle; 
    import android.widget.ArrayAdapter; 
    import android.widget.ImageView; 
    import android.widget.ListView; 
    import android.widget.TextView; 

    public class Listviewact extends Activity { 

public void onCreate(Bundle b) { 
    super.onCreate(b); 
    setContentView(R.layout.listview_layout); 

    Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/AlexBrush-Regular-OTF.otf"); 
    TextView tv = (TextView) findViewById(R.id.textView1); 
    tv.setTypeface(tf); 
    } 

    public void onResume() { 
    super.onResume(); 
    int buttonId = getIntent().getIntExtra("buttonId", 0); 
    int buttonIdx = getButtonIdx(buttonId); 

    // find and set image according to buttonId 
    int imageId = IMAGE_IDS[buttonIdx];  // image to show for given button 
    ImageView imageView = (ImageView)findViewById(R.id.imageView1); 
    imageView.setImageResource(imageId); 

    // find and set listview imtes according to buttonId 
    String[] items = LISTVIEW_DATA[buttonIdx]; // listview items to show for given button 
    ListView listView = (ListView)findViewById(R.id.listView1); 
    ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, items); 
    listView.setAdapter(adapter); 
} 

private void setListAdapter(ArrayAdapter adapter) { 
    // TODO Auto-generated method stub 

} 

// a little helper to map ids to array indices 
// to be able to fetch the correct image and listview data later 
private final static int[] BUTTON_IDS = new int[] { 
    R.id.imageButton1, 
    R.id.imageButton2, 
    R.id.imageButton3, 
    R.id.imageButton4, 
    R.id.imageButton5, 
    R.id.imageButton6 
}; 

// 6 images 
private final static int[] IMAGE_IDS = new int[] { 
    R.drawable.bmw, 
    R.drawable.ford, 
    R.drawable.honda, 
    R.drawable.toy, 
    R.drawable.vok2, 
    R.drawable.ic_launcher 
}; 

// 6 different sets of strings for the listviews 
private final static String[][] LISTVIEW_DATA = new String[][] { 
    {"First A", "First B", "First C", "First D","First E","First F"}, 
    {"Second A", "Second B", "Second C"}, 
    {"Third A", "Third B", "Third C"}, 
    {"Forth A", "Forth B", "Forth C"}, 
    {"Fifth A", "Fifth B", "Fifth C"}, 
    {"Sixth A", "Sixth B", "Sixth C"}, 
}; 

// map button id to array index 
static private int getButtonIdx(int id) { 
    for(int i = 0; i<BUTTON_IDS.length; i++) { 
     if (BUTTON_IDS[i] == id) return i; 
    } 
    return 0; // should not happen 
} 
} 

這將是巨大的,如果有人能告訴我如何做一個一流的,我可以在這裏也呼籲所有的項目點擊從所有列表視圖從我的代碼。

package com.example.testtest; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.ArrayAdapter; 
import android.widget.ImageButton; 
import android.widget.ListView; 

public class MainActivity extends Activity implements OnClickListener{ 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.layout_of_button); 
ImageButton btn1 = (ImageButton)findViewById(R.id.imageButton1); 
ImageButton btn2 = (ImageButton)findViewById(R.id.imageButton2); 
ImageButton btn3 = (ImageButton)findViewById(R.id.imageButton3); 
ImageButton btn4 = (ImageButton)findViewById(R.id.imageButton4); 
ImageButton btn5 = (ImageButton)findViewById(R.id.imageButton5); 
ImageButton btn6 = (ImageButton)findViewById(R.id.imageButton6); 
btn1.setOnClickListener(this); 
btn2.setOnClickListener(this); 
btn3.setOnClickListener(this); 
btn4.setOnClickListener(this); 
btn5.setOnClickListener(this); 
btn6.setOnClickListener(this); 
} 

@Override 
public void onClick(View v) { 
    switch(v.getId()) { 
    // if one of the image buttons is pressed... 
    case R.id.imageButton1: 
    case R.id.imageButton2: 
    case R.id.imageButton3: 
    case R.id.imageButton4: 
    case R.id.imageButton5: 
    case R.id.imageButton6: 
     Intent intent = new Intent(this, Listviewact.class); 
     // pass ID of pressed button to listview-activity 
     intent.putExtra("buttonId", v.getId()); 
     startActivity(intent); 
     break; 
    // here you could place handling of other clicks if necessary...   
    } 
} 

private void setListAdapter(ArrayAdapter<String> arrayAdapter) { 
// TODO Auto-generated method stub 

} 

private ListView getListView() { 
// TODO Auto-generated method stub 
    return null; 
} 
} 

CHEERS。

​​

+0

我很困惑,正是你想要的。你想點擊列表中的項目,然後點擊一個按鈕,打開一個新的「活動」,包含所有已點擊的項目? – codeMagic

+0

你好魔術什麼是當我點擊任何我的列表視圖項目(所有這些)它會打開一個新的佈局,我可以重複使用不同的信息,所以一個類的所有項目clicks.or如果我使它更難我的自我請讓我知道。 – coolcat

+0

你絕對可以......我一直這樣做。只要不管你點擊了什麼,你都希望有相同的「佈局」,儘管它可能略有不同,仍然可以管理。你可以創建一個類來存儲'靜態ArrayList'或類似的東西,或者通過'Intent.extras'發送數據 – codeMagic

回答

0

如果我明白你想要什麼,你可以創建一個類似static Arraylist的類,每次單擊一個項目時都會添加一個類。所以創建類似

public class Data class 
{ 
    static ArrayList<String> dataArray = new ArrayList<String>();; 

    public Data() 
    { 
     // empty constructor but could be used if needed 
    } 

然後你可以添加不同的getters/setters或任何你需要在這裏。當你點擊一個項目你只需要調用類似

Data.dataArray.add("stuff"); 

然後檢索在你的下一個Activity在這裏的項目。

如果是過於複雜或比你更需要那麼你可以讓您通過Intent

Intents

也需要一個ArrayList或任何對象,只是一個偏好,但因爲所有的Button S的做同樣的事情,你可以避免初始化它們並在它們全部設置listeners。在XML中只需添加

`android:onClick="someFunctionName"` 

每個Button然後使用該函數名稱

public void someFunctionName(View v) { 
switch(v.getId()) { 
// if one of the image buttons is pressed... 

    Intent intent = new Intent(this, Listviewact.class); 
    // pass ID of pressed button to listview-activity 
    intent.putExtra("buttonId", v.getId()); 
    startActivity(intent); 
    break; 
// here you could place handling of other clicks if necessary...   
} 

也沒有必要爲case陳述,因爲他們都做同樣的事情,你不需要implements OnClickListenerActivity聲明

+0

感覺有點愚蠢的代碼,我一直在試圖讓事情發生,但我只是沒有得到它。我正在想什麼,我想做的事情就像我對編碼不熟悉一樣。我很難解釋之後的事情。 – coolcat

+0

這個魔法就是我想要做的http://img594.imageshack.us/img594/9373/a7l8.png現在我已經完成了前兩個圖像,但是這是最後一個類/圖像,我發現它很難編碼。最後一堂課將有不同的文本/數據/信息從列表視圖點擊第二個圖像。爲什麼我問這個問題,因爲我不想讓像50班有自己的數據/信息。我覺得有很多是一種有1班的方式,只是改變視圖或插入不同的數據textview.Your代碼,你給我開始可能是我只是不明白它 – coolcat

+0

好吧,我想我有點明白你想要什麼。在我的第一個例子中使用類應該可以工作。當你點擊一個項目時,關聯的數據將被存儲在該類中。很難說如何設置它,因爲我不知道你需要什麼。你可以有不同的'String',int','ArrayList','HashMap' ......它只取決於你的情況。這是保持數據與演示文稿級別分開的好方法,也是 – codeMagic

0

您使用的是ListView但不使用任何它的回調?在這裏,這是我用於我的ListView的代碼。我將活動放在我的陣列中,但你可以放任何東西。修改R.layout.mfd_view可讓您爲每個列表項目添加任何你想要的內容。 A Button如果這是你所需要的。希望這可以幫助。我仍然在學習自己。

import android.app.Activity; 
import android.app.ListFragment; 
import android.content.Context; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.ListAdapter; 
import android.widget.ListView; 

public class MyListFragment extends ListFragment { 
String fragmentBackStack; 
MyMapHandler handler; 
OnViewSelectedListener mListener; 

/** 
* An array of POJOs used to hold the info about the fragments we'll be 
* swapping between This should be inserted into an array adapter of some 
* sort before being passed onto ListAdapter 
*/ 
private static final ViewDetails[] ACTIVITY_DETAILS = { 
     new ViewDetails(R.string.action_largeTach, 
       R.string.largeTach_description, LargeTachActivity.class), 
     new ViewDetails(R.string.action_map, R.string.map_description, 
       MyMapHandler.class), 
     new ViewDetails(R.string.action_navigation, 
       R.string.navigation_description, NavigationActivity.class), 
     new ViewDetails(R.string.action_raceMode, 
       R.string.raceMode_description, RaceModeActivity.class), 
     new ViewDetails(R.string.action_settings, 
       R.string.settings_description, SettingsFragment.class), 
     new ViewDetails(R.string.action_extraInfo, 
       R.string.extraInfo_description, ExtraInfoActivity.class) }; 

/** 
* @author PyleC1 
* 
*   A POJO that holds a class object and it's resource info 
*/ 
public static class ViewDetails { 
    private final Class<? extends Activity> viewActivity; 
    private int titleId; 
    private int descriptionId; 

    /** 
    * @param titleId 
    *   The resource ID of the string for the title 
    * @param descriptionId 
    *   The resource ID of the string for the description 
    * @param activityClass 
    *   The fragment's class associated with this list position 
    */ 
    ViewDetails(int titleId, int descriptionId, 
      Class<? extends Activity> viewActivity) { 

     super(); 
     this.titleId = titleId; 
     this.descriptionId = descriptionId; 
     this.viewActivity = viewActivity; 
    } 

    public Class<? extends Activity> getViewActivity() { 
     return viewActivity; 
    } 
} 

/** 
* @author PyleC1 
* 
*   Extends the ArrayAdapter class to support our custom array that 
*   we'll insert into the ListAdapter so the user can pick between 
*   MFD screens at boot time. 
*/ 
private static class CustomArrayAdapter extends ArrayAdapter<ViewDetails> { 
    public CustomArrayAdapter(Context context, ViewDetails[] activities) { 
     super(context, R.layout.mfd_view, R.id.mfdTitle, activities); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     MFDView mfdView; 

     if (convertView instanceof MFDView) { 
      mfdView = (MFDView) convertView; 
     } else { 
      mfdView = new MFDView(getContext()); 
     } 

     ViewDetails details = getItem(position); 

     mfdView.setTitleId(details.titleId); 
     mfdView.setDescriptionId(details.descriptionId); 

     return mfdView; 
    } 
} 

public void onAttach(Activity activity) { 
    super.onAttach(activity); 

    ListAdapter listAdapter = new CustomArrayAdapter(getActivity(), 
      ACTIVITY_DETAILS); 
    setListAdapter(listAdapter); 

    try { 
     mListener = (OnViewSelectedListener) activity; 
    } catch (ClassCastException e) { 
     throw new ClassCastException(activity.toString() 
       + " must implement OnViewSelectedListener!"); 
    } 
} 

@Override 
public void onResume() { 
    super.onResume(); 
} 

public interface OnViewSelectedListener { 
    public void onViewSelected(Class<? extends Activity> activityClass); 
} 

public void onListItemClick(ListView l, View v, int position, long id) { 
    ViewDetails details = (ViewDetails) getListAdapter().getItem(position); 

    mListener.onViewSelected(details.viewActivity); 
} 
} 

注意,無論活動調用此片段必須實現OnViewSelectedListener接口。如果您將其作爲子類添加到主要活動中,則不需要這樣做。 public void onListItemClick(arg0, arg1, arg2, arg3)回調很好。您只需從那裏交換片段或調用活動。

+0

目前一切都很好,當我點擊我的任何一個imagebutton的活動打開罰款和listview在那裏,從這裏工作,我想打開一個新的活動/佈局從所有的listview項目已被clciked.if我錯過的東西,請讓我知道歡呼downrangefuture ...我們將在那裏到達;) – coolcat

+0

所以你試圖從用戶選擇一個新的動態佈局?您可能需要將捆綁在一起併發送給新的活動。然後在新活動的佈局中,使用0dp和各種'android:layout_weight'值設置視圖容器。然後,您可以在新活動的'onCreate(Bundle)'中顯示/隱藏片段。在開始調用FragmentManager之前,一定要調用'setContentView(int)'。 – DownrangeFuture

相關問題