如何在不刷新页面的情况下获取列表视图信息?

时间:2012-11-08 04:38:53

标签: android listview refresh

我目前正在为我的最后一年项目开发Android应用程序。但说实话,我没有任何基本的知识,一切都从头开始,并参考在线教程很多。这是我的问题,我试图从listview活动中检索数据。我的页面中有两个listview使用按钮。我能够显示第一个列表视图但是当它获取第二个列表视图的数据时,第一个列表视图的数据消失了,因为页面被刷新,反之亦然。我应该修改哪些代码来获取页面中的数据? (数据库尚未实现)请帮助,非常感谢。以下是我的编码。

Codings for XML。               

      <!-- Location -->
      <TextView android:id="@+id/TextViewLocation"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dip"
            android:layout_marginBottom="10dip"
            android:text="Location Information"
            android:gravity="center"
            android:textSize="15dip"
            android:textColor="#025f7c"/>

      <!--  Condition Label -->
      <TextView android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textColor="#372c24"
            android:text="Traffic Condition"/>
      <Button android:id="@+id/inputListView"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dip"
            android:layout_marginBottom="10dip"
            android:text="choose one..."/>

      <!--  Comment Label -->
      <TextView android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textColor="#372c24"
            android:text="What's Happening?"/>
      <Button android:id="@+id/inputListView2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dip"
            android:layout_marginBottom="10dip"
            android:text="choose one..."/>

      <!--  Suggestion Label -->
      <TextView android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textColor="#372c24"
            android:text="Comments / Suggestion"/>
      <EditText android:layout_width="fill_parent"
            android:layout_height="80dp"
            android:layout_marginTop="5dip"
            android:layout_marginBottom="10dip"
            android:singleLine="true"/>
      <!-- Image button -->
      <Button android:id="@+id/btnImage"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dip"
            android:text="Upload Image"/>


      <!-- Report button -->
      <Button android:id="@+id/btnReportCheckin"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dip"
            android:text="Report"/>

      <!-- Link to Logout -->
      <TextView android:id="@+id/linkLogout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dip"
            android:layout_marginBottom="40dip"
            android:text="Log Out"
            android:gravity="center"
            android:textSize="20dip"
            android:textColor="#025f7c"/>

    </LinearLayout>
    <!-- Check or Report Form Ends -->

活动类的编码

public class CheckinActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Set View to checkin.xml
    setContentView(R.layout.checkin);
    /*
    TextView LocationView = (TextView) findViewById(R.id.TextViewLocation);
    Intent h = getIntent();
    // getting attached intent data
    String address = h.getStringExtra("address");
    // displaying selected product name
    LocationView.setText(address); */ 
    Button ListViewScreen = (Button) findViewById(R.id.inputListView);
    //Listening to Button 
    ListViewScreen.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            //Switching to ListView Screen
            Intent i = new Intent(getApplicationContext(), ListViewActivity.class);
            startActivity(i);
        }
    }   );

    Button SelectedView = (Button) findViewById(R.id.inputListView);
        Intent i = getIntent();
        // getting attached intent data
        String product = i.getStringExtra("product");
        // displaying selected product name
        SelectedView.setText(product);

    Button ListView2Screen = (Button) findViewById(R.id.inputListView2);

  //Listening to Button 
    ListView2Screen.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            //Switching to ListView Screen
            Intent j = new Intent(getApplicationContext(), ListView2Activity.class);
            startActivity(j);
        }
    }   );  

    Button SelectedView2 = (Button) findViewById(R.id.inputListView2);

    Intent j = getIntent();
    // getting attached intent data
    String product2 = j.getStringExtra("product2");
    // displaying selected product name
    SelectedView2.setText(product2);



    TextView Logout = (TextView) findViewById(R.id.linkLogout);
    // Listening to Log out
    Logout.setOnClickListener(new View.OnClickListener() {

        public void onClick(View arg0) {
                            // Closing menu screen
            // Switching to Login Screen/closing register screen
            finish();
        }
    });
}
}

为listview类编码

 public class ListViewActivity extends ListActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // storing string resources into Array
        String[] traffic_condition = getResources().getStringArray(R.array.traffic_condition);

        // Binding resources Array to ListAdapter
        this.setListAdapter(new ArrayAdapter<String>(this, R.layout.listitem, R.id.listViewLayout, traffic_condition));

        ListView lv = getListView();

        // listening to single list item on click
        lv.setOnItemClickListener(new OnItemClickListener() {
          public void onItemClick(AdapterView<?> parent, View view,
              int position, long id) {
              // selected item
              String product = ((TextView) view).getText().toString();
              // Launching new Activity on selecting single List Item
              Intent i = new Intent(getApplicationContext(), CheckinActivity.class);
              // sending data to new activity
              i.putExtra("product", product);
              startActivity(i);
          }
        });
    }
}

希望我明确表示,如果需要,我可以提供我的应用程序的屏幕截图,谢谢!

1 个答案:

答案 0 :(得分:0)

尝试调用adapter.notifyDataSetChanged();刚设置了listview的适配器之后。这样,数据集中的更改(在您的情况下为List)将显示在listView中。

我希望这个答案可以帮助你