How to obtain listview information without refreshing the page?
- by user1808098
I am currently developing an Android Application for my Final Year Project. But to be honest I do not have any basic knowledges and everything started from scratch and referring to online tutorials a lot. Here is my question, I was trying to retrieve data from listview activity. There are two listview in my page using button. I was able to display the first listview but when it get data for the second listview, the data for first listview is disappeared because the page is refreshed, vice versa. What code should I modified to get both the data in the page? (Database not implemented yet) Please help, thanks a lot. Below are my codings.
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 -->
Codings for Activity Class
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();
}
});
}
}
Coding for listview class
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);
}
});
}
}
Hope I made myself clear, I can provide a screen shot of my apps if it is required, thanks!