Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
166 views
in Technique[技术] by (71.8m points)

android - Open Multiple Listview Item Click to One Class

Hope you guys can help.

I have a activity which handles all the 10 image button clicks and list view intents. What i am looking to do is have 1 layout for all the list view button clicks. And in this layout call different data to it. When i started this project i had many activitys until a great stack overflow user pointed out that i can make it simpler which i did and made it a lot clear.

  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
}
}

It would be great if someone can show me how to make a class which i can call all the item clicks from all list views too from my code here.

 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.

http://img40.imageshack.us/img40/705/f6h9.png

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You're using the ListView but not using any of it's callbacks? Here, this is my code that I use for my ListView. I'm putting activities in my array, but you could put anything. Modifying the R.layout.mfd_view allows you to put whatever you want for each list item. A Button if that's what you need. Hope this helps. I'm still learning myself.

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);
}
}

Note that whatever activity calls this fragment must implement the OnViewSelectedListener interface. If you added this to your main activity as a subclass, this wouldn't be required. The public void onListItemClick(arg0, arg1, arg2, arg3) callback is fine. You just swap fragments or call activities from inside there.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...