0

我使用 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);

            }

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

4

2 回答 2

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");
于 2013-02-15T18:02:10.773 回答
2

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

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

        }
    });

}}
于 2014-06-24T09:27:47.180 回答