我目前正在为我的最后一年项目开发一个 Android 应用程序。但老实说,我没有任何基础知识,一切都是从零开始,并且经常参考在线教程。这是我的问题,我试图从 listview 活动中检索数据。我的页面中有两个使用按钮的列表视图。我能够显示第一个列表视图,但是当它获取第二个列表视图的数据时,第一个列表视图的数据会因为页面刷新而消失,反之亦然。我应该修改什么代码来获取页面中的数据?(数据库尚未实现)请帮助,非常感谢。下面是我的编码。
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();
}
});
}
}
列表视图类的编码
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);
}
});
}
}
希望我说清楚了,如果需要,我可以提供我的应用程序的屏幕截图,谢谢!