在详细活动中单击上一个/下一个按钮时,移至下一个/上一个项目 [英] Move to next/previous item when click on previous/next button in detailed activity

查看:95
本文介绍了在详细活动中单击上一个/下一个按钮时,移至下一个/上一个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从Web服务获取的listview数据,当我单击listview项时,它进入详细的活动,当我单击nextbutton时,此处显示了描述和图像,单击时,它应该也将我带到列表中的下一项prebutton可以将我带到列表中的上一个项目,但是现在当我单击Next/pre按钮时,我正在强制关闭,我正在使用BaseAdapter.以下是我的代码

I have a listview data obtained from web-service, when i click on listview item it takes to detailed Activity where description and image are present here when i clcik on nextbutton it should take me to next item in list similarly when click on prebutton it shuld take me to previous item in list, but now i am getting force close when i click on next/pre button, i am using BaseAdapter. following is my code

DetailedActivity

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

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)

推荐答案

为什么要在DetailActivity中使用ListView,单击列表项后转到Detailactivity并按意图传递歌曲的单击项位置和数组列表,现在您可以显示所选索引歌曲的详细信息,然后单击下一步"或上一步"按钮,只需将您的清单中的值设置为textview或imgaeviews,就可以完成

Why are you using ListView in your DetailActivity, on clicking the the list item go to the Detailactivity and pass clicked item postion and arraylist of songs in intent, now you can display detail for selected index song, and on clicking the next or prev button just set the values from your arrylist to the textview or imgaeviews, and you are done

这篇关于在详细活动中单击上一个/下一个按钮时,移至下一个/上一个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆