单击详细活动中的上一个/下一个按钮,移至下一个/上一个项目

时间:2013-02-23 05:34:38

标签: android listview

我有一个从网络服务获得的列表视图数据,当我点击listview项目时它需要详细的活动,其中描述和图像出现在这里当我clcik on nextbutton它应该带我到列表中的下一个项目同样点击prebutton它shuld带我到列表中的上一项,但现在我点击下一个/前一个按钮,我正在使用BaseAdapter强制关闭。以下是我的代码

DetailedActivity

public class SampleDesp extends Activity   {

 ArrayList<HashMap<String, String>> songsList;
 ListView list;


    // JSON node keys
    static final String KEY_URL_FOR_MAP = "url_site"; 
    private static final String KEY_TITLE = "title";
    private static final String KEY_SITEURL = "url";
    private static final String KEY_DATE = "date";
    private static final String KEY_NAME = "name";
    private static final String KEY_CONTENT = "content";
    private static final String KEY_URL = "url";
     static final String KEY_SLUG1= "slug";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       // requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.sampledes);
        ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
     // move up event handler
        ImageButton preButton = (ImageButton) findViewById(R.id.prevButton);

        preButton.setOnClickListener(new View.OnClickListener() {
           public void onClick(View view) {
               movePre();
           }
        });

        // move down event handler
        ImageButton nxtButton = (ImageButton) findViewById(R.id.nextButton);
        nxtButton.setOnClickListener(new View.OnClickListener() {
           public void onClick(View view) {
               moveNxt();
           }
        });

         ListView list = new ListView(this);
        StationAdapter adapter1 = new StationAdapter(this, songsList);    
         list.setAdapter(adapter1);


        // getting intent data
        Intent in = getIntent();
        final String url1 = in.getStringExtra(KEY_URL);

        ImageView imgv = (ImageView) findViewById(R.id.imgdesc);
        ImageLoader imageLoader = new ImageLoader(getApplicationContext());
        imageLoader.DisplayImage(url1, imgv);


        // Get JSON values from previous intent
        String songsList1 = in.getStringExtra("song");
        int listPosition = in.getIntExtra("listPosition", 0);
        final String title = in.getStringExtra(KEY_TITLE);
        final String siteurl = in.getStringExtra(KEY_URL_FOR_MAP);
        String date = in.getStringExtra(KEY_DATE);
        String name = in.getStringExtra(KEY_NAME);
        final String content = in.getStringExtra(KEY_CONTENT);

        // Displaying all values on the screen
        TextView lblName = (TextView) findViewById(R.id.name_label);
        TextView lblUrl = (TextView) findViewById(R.id.url_label);

        TextView lblCost = (TextView) findViewById(R.id.email_label);
        TextView lblDesc = (TextView) findViewById(R.id.mobile_label);
        TextView lblCont = (TextView) findViewById(R.id.content_label);

        lblName.setText(title);
        lblUrl.setText(siteurl);
        lblCost.setText(date);
        lblDesc.setText(name);
        lblCont.setText(content);
    }    

    private void movePre(){



        list.setSelection(list.getSelectedItemPosition() - 1);
    }




        // Move selected item "down" in the ViewList.
        private void moveNxt(){

            list.setSelection(list.getSelectedItemPosition() + 1);


        }

StationAdapter

class StationAdapter  extends BaseAdapter{
  private static final String TAG = null;
    private Activity activity;
    private ArrayList<HashMap<String, String>> data;
    private static LayoutInflater inflater=null;
    public ImageLoader imageLoader; 

    public StationAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
        activity = a;
        data=d;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader=new ImageLoader(activity.getApplicationContext());
        Log.v(TAG, "Message here");
    }



    public int getCount() {
        return data.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        if(convertView==null)
            vi = inflater.inflate(R.layout.myfragment_layout, null);

        TextView title = (TextView)vi.findViewById(R.id.name_label); // title
        TextView siteurl = (TextView)vi.findViewById(R.id.url_label);
        TextView date = (TextView)vi.findViewById(R.id.email_label); // artist name
        TextView content = (TextView)vi.findViewById(R.id.content_label);  // duration
        TextView name = (TextView)vi.findViewById(R.id.mobile_label); 
        // duration
        ImageView thumb_image=(ImageView)vi.findViewById(R.id.imgdesc); // thumb image

        HashMap<String, String> song = new HashMap<String, String>();
        song = data.get(position);


        ListView list;
        // Setting all values in listview
       title.setText(song.get(MainActivity.KEY_TITLE));
      // siteurl.setText(song.get(MainActivity.KEY_SITEURL));
        date.setText(song.get(MainActivity.KEY_DATE));
        siteurl.setText(song.get(MainActivity.KEY_URL_FOR_MAP));// correct code
        ((TextView)siteurl).setVisibility(TextView.INVISIBLE);
        content.setText(song.get(MainActivity.KEY_CONTENT));
        name.setText(song.get(MainActivity.KEY_NAME));
        System.out.println("output: " +name);
       imageLoader.DisplayImage(song.get(MainActivity.KEY_URL), thumb_image);
        return vi;
    }
}

LogCat错误

02-23 12:32:52.506: E/AndroidRuntime(894): FATAL EXCEPTION: main
02-23 12:32:52.506: E/AndroidRuntime(894): java.lang.NullPointerException
02-23 12:32:52.506: E/AndroidRuntime(894):  at com.example.sampleofmain.SampleDesp.moveNxt(SampleDesp.java:137)
02-23 12:32:52.506: E/AndroidRuntime(894):  at com.example.sampleofmain.SampleDesp.access$1(SampleDesp.java:135)
02-23 12:32:52.506: E/AndroidRuntime(894):  at com.example.sampleofmain.SampleDesp$2.onClick(SampleDesp.java:82)
02-23 12:32:52.506: E/AndroidRuntime(894):  at android.view.View.performClick(View.java:2485)
02-23 12:32:52.506: E/AndroidRuntime(894):  at android.view.View$PerformClick.run(View.java:9080)
02-23 12:32:52.506: E/AndroidRuntime(894):  at android.os.Handler.handleCallback(Handler.java:587)
02-23 12:32:52.506: E/AndroidRuntime(894):  at android.os.Handler.dispatchMessage(Handler.java:92)
02-23 12:32:52.506: E/AndroidRuntime(894):  at android.os.Looper.loop(Looper.java:123)
02-23 12:32:52.506: E/AndroidRuntime(894):  at android.app.ActivityThread.main(ActivityThread.java:3683)
02-23 12:32:52.506: E/AndroidRuntime(894):  at java.lang.reflect.Method.invokeNative(Native Method)
02-23 12:32:52.506: E/AndroidRuntime(894):  at java.lang.reflect.Method.invoke(Method.java:507)
02-23 12:32:52.506: E/AndroidRuntime(894):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-23 12:32:52.506: E/AndroidRuntime(894):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-23 12:32:52.506: E/AndroidRuntime(894):  at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

为什么在DetailActivity中使用ListView,单击列表项转到Detailactivity并在意图中传递单击的项目位置和歌曲的arraylist,现在您可以显示所选索引歌曲的详细信息,并单击下一个或prev按钮只是将您的arrylist中的值设置为textview或imgaeviews,您就完成了