使用下一个/上一个按钮逐个显示记录

时间:2013-02-15 16:27:45

标签: android android-widget android-listview android-button

我正在使用Hash Map显示10条记录:

 for (int i = 0; i < jArray.length(); i++) {
                JSONObject c = jArray.getJSONObject(i);

            //  Item objItem = new Item();

                u_Id = jArray.getJSONObject(i).getString(TAG_ID);
                u_name = jArray.getJSONObject(i).getString(TAG_NAME);
                u_refer = jArray.getJSONObject(i).getString(TAG_Image);
                u_image = jArray.getJSONObject(i).getString(TAG_refere);
                u_des = jArray.getJSONObject(i).getString(TAG_des);

                HashMap<String, String> map = new HashMap<String, String>();

                map.put(TAG_ID, u_Id);
                map.put(TAG_NAME, u_name);
                map.put(TAG_refere, u_refer);
                List.add(map);

                Log.v(TAG_ID, u_Id);
                Log.v(TAG_NAME,u_name);
                Log.v(TAG_refere, u_refer);
                Log.v(TAG_Image,u_image);
                Log.v(TAG_des, u_des);

             ListAdapter adapter = new SimpleAdapter(this, List,
             R.layout.row,new String[] { TAG_ID, TAG_NAME }, new int[] {R.id.textView1, R.id.textView2 });
             listView.setAdapter(adapter);
           //  View header = (View)getLayoutInflater().inflate(R.layout.todo_row, null);
           //  listView.addHeaderView(header);

            }

现在我想在下一个和上一个按钮上逐个显示记录,在上一个按钮上应显示上一个记录,在下一个按钮上应显示下一个记录。请任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:3)

我通过这样做实现了我的目标:

 int position =0 ;


@Override
        public void onClick(View button) {
            if (button == nxt) {
                if(position < List.size() - 1){
                    position++;
                    List.get(position);
                    String name = List.get(position).get(TAG_NAME);
                    String Des = List.get(position).get(TAG_des);
                    String aa = String.valueOf(position);
                 //  Log.v("tag",name);
                 //   Log.v("tag", Des);
                   System.out.println( name+Des);
                }
                else
                  Log.d("TAG","Last Record");
            } else if (button == pre) {
                if(position > 0){
                   position--;        
                  List.get(position);
                  String aa = String.valueOf(position);
                 Log.i("tag", aa);
               // System.out.print(position);
              //  (position);
              }
              else
                Log.d("TAG","First Record");

答案 1 :(得分:2)

  

将您的String数组和位置值发送到下一个活动

list.setOnItemClickListener(new OnItemClickListener() {
          public void onItemClick(AdapterView<?> parent, View view,
              int position, long id) {
              Intent i = new Intent(listview.this, Single_SMS.class);
              // sending data to new activity
              i.putExtra("position", position);
              i.putExtra("strings", web);
              startActivity(i);

          }
        });
  

在下一个活动中检索字符串数组和位置。

public class NextActivity extends Activity {
int position;
String[] web;
int size;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
     setContentView(R.layout.single_sms);

    Bundle bd = getIntent().getExtras();

    if (bd != null) {
        position = bd.getInt("position", 0);
        web = bd.getStringArray("strings");
        size = web.length;
        Button txtProduct = (Button) findViewById(R.id.button1);
        txtProduct.setText(" " + web[position]);
    }

    btn_next.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            position = position + 1;
            if (position > size) {
                Button txtProduct = (Button) findViewById(R.id.button1);
                txtProduct.setText(" " + web[position]);
            }

        }
    });

}}