#database #listview #android-intent
#База данных #listview #android-намерение
Вопрос:
Я пытаюсь перенести свои данные listview в AttendenceActivity в другой listview с базой данных, чтобы сохранить мои данные для дальнейшего использования.
Прошу прощения за плохое кодирование. Все еще новичок в этом. Кроме того, у меня сжатые сроки для моего проекта, любая помощь была бы очень признательна, ребята.
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
public class AttendenceActivity extends AppCompatActivity {
ListView simpleList;
String[] QL1501S;
Button submit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_attendence);
// get the string array from string.xml file
QL1501S = getResources().getStringArray(R.array.QL1501S);
// get the reference of ListView and Button
simpleList = (ListView) findViewById(R.id.simpleListView);
submit = (Button) findViewById(R.id.submit);
// set the adapter to fill the data in the ListView
CustomAdapter customAdapter = new CustomAdapter(getApplicationContext(), QL1501S);
simpleList.setAdapter(customAdapter);
// perform setOnClickListerner event on Button
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String message = "";
// get the value of selected answers from custom adapter
for (int i = 0; i < CustomAdapter.selectedAnswers.size(); i ) {
message = message "n" (i 1) " " CustomAdapter.selectedAnswers.get(i);
}
// display the message on screen with the help of Toast.
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
Вот XML-файл для AttendenceActivity………….
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Attendence"
android:id="@ id/textView7"
android:layout_gravity="center_horizontal"
android:textSize="50dp" />
<ListView
android:id="@ id/simpleListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@ id/submit"
android:divider="@color/material_blue_grey_800"
android:dividerHeight="1dp"
android:footerDividersEnabled="false"
android:layout_below="@ id/textView7" />
<Button
android:id="@ id/submit"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_margin="20dp"
android:background="#0f0"
android:text="Submit"
android:textColor="#fff"
android:textSize="20sp" />
</RelativeLayout>
Вот кодировка CustomAdapter для AttendenceActivity
public class CustomAdapter extends BaseAdapter {
Context context;
String[] QL1501S;
LayoutInflater inflter;
public static ArrayList<String> selectedAnswers;
public CustomAdapter(Context applicationContext, String[] questionsList) {
this.context = context;
this.QL1501S = questionsList;
// initialize arraylist and add static string for all the questions
selectedAnswers = new ArrayList<>();
for (int i = 0; i < questionsList.length; i ) {
selectedAnswers.add("Not Marked");
}
inflter = (LayoutInflater.from(applicationContext));
}
@Override
public int getCount() {return QL1501S.length;}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(final int i, View view, ViewGroup viewGroup) {
view = inflter.inflate(R.layout.list_items, null);
// get the reference of TextView and Button's
TextView NameOfStud = (TextView) view.findViewById(R.id.NameOfStud);
RadioButton Present = (RadioButton) view.findViewById(R.id.Present);
RadioButton Absent = (RadioButton) view.findViewById(R.id.Absent);
// perform setOnCheckedChangeListener event on yes button
Present.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// set Yes values in ArrayList if RadioButton is checked
if (isChecked)
selectedAnswers.set(i, "Present");
}
});
// perform setOnCheckedChangeListener event on no button
Absent.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// set No values in ArrayList if RadioButton is checked
if (isChecked)
selectedAnswers.set(i, "Absent");
}
});
// set the value in TextView
NameOfStud.setText(QL1501S[i]);
return view;
}
}
Наконец, listview для посещаемости Listview
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- TextView for studentname-->
<TextView
android:id="@ id/NameOfStud"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="@dimen/activity_horizontal_margin"
android:textColor="#000" />
<!-- RadioGroup for grouping of RadioButton-->
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:weightSum="2">
<RadioButton
android:id="@ id/Present"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_weight="1"
android:text="Present"
android:textColor="#000"
android:checked="false" />
<RadioButton
android:id="@ id/Absent"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_weight="1"
android:text="Absent"
android:textColor="#000"
android:checked="false" />
</RadioGroup>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@ id/Remarks"
android:hint="Remarks" />