2013-02-23 106 views
0

移動到下一個/上一個項目我有一個從網絡服務獲得的listview數據,當我點擊listview項目時,它需要詳細的活動,其中的描述和圖像在這裏,當我在下一個按鈕clcik它應該帶我到列表中的下一個項目同樣,當點擊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) 
+0

如果您提供日誌貓,它也會更好。 – RobinHood 2013-02-23 05:37:27

回答

0

你爲什麼用你的DetailActivity的ListView,上單擊列表中的項目去注意力,並通過點擊項目位置和歌曲陣列列表的意圖,現在你可以顯示所選索引歌曲的詳細信息,點擊下一個或上一個按鈕,只需將您的arrylist中的值設置爲textview或imgaeviews,即可完成

+0

然後我應該放置內部 \t \t \t私人無效的MoveNext(){ \t \t \t \t // TODO自動生成方法存根 \t \t \t \t \t \t \t \t list.setSelection(list.getSelectedItemPosition()+ 1); \t \t \t} }); – Tycoon 2013-02-23 08:37:15

+0

inside moveNext從你的ArrayList獲取下一個項目,獲取更新你的視圖 – 2013-02-23 08:39:26

+0

仍然沒有得到任何示例代碼,所以我可以嘗試 – Tycoon 2013-02-23 09:36:22

相關問題