我正在尝试为学生构建一个出勤管理器应用程序,并在点击时使用自定义 ListView 来显示主题名称和最低出勤率。
这些是我面临的问题::
1)我希望我的第一个列表项是“添加主题项”,点击它会AddSubjectActivity
通过SharedPreferences
.
2)一旦按下提交按钮,AddSubjectActivity
我希望下一个列表项说“点击添加主题”等等。
3)基本上,假设ListView
有 5 个项目,点击每个项目会将我带到 AddSubject 活动,但对应于相应list Item
被点击的信息会显示在其中。
这是我的代码:
类 CustomAdapter
public class CustomAdapter extends BaseAdapter{
String [] result;
Context context;
int [] imageId;
private static LayoutInflater inflater=null;
public CustomAdapter(SecondActivity mainActivity, String[] prgmNameList, int[] prgmImages) {
// TODO Auto-generated constructor stub
result=prgmNameList;
context=mainActivity;
imageId=prgmImages;
inflater = ( LayoutInflater )context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return result.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public class Holder
{
TextView tv;
ImageView img;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Holder holder=new Holder();
SecondActivity second=new SecondActivity();
View rowView;
rowView = inflater.inflate(R.layout.program_list, null);
holder.tv=(TextView) rowView.findViewById(R.id.textView);
holder.img=(ImageView) rowView.findViewById(R.id.statsImageView);
holder.tv.setText(result[position]);
holder.img.setImageResource(imageId[position]);
rowView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
Toast.makeText(context, "You Clicked "+result[position], Toast.LENGTH_LONG).show();
context.startActivity(new Intent(context, AddSubject.class));
}
});
return rowView;
}
}
SecondActivity:包含ListView的Activity
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Bundle bundle=getIntent().getExtras();
String fullNameFinal = bundle.getString("fullName");
String courseFinal = bundle.getString("course");
int avgAttendanceFinal= bundle.getInt("avgAttendance");
boolean logInStatusFinal = bundle.getBoolean("logInStatus");
nameTextView=(TextView)findViewById(R.id.nameTextView);
courseTextView=(TextView)findViewById(R.id.courseTextView);
nameTextView.setText(fullNameFinal);
courseTextView.setText(courseFinal);
overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
ListView lv;
Context context;
ArrayList prgmName;
int [] prgmImages={R.drawable.listblockgold,R.drawable.listblockindigo,R.drawable.listblocklime,R.drawable.listblockorange,R.drawable.listblockpink,R.drawable.listblockred,R.drawable.listblocksilver,R.drawable.listblocktan,R.drawable.listblockteal};
String [] prgmNameList={"Maths","P.Comm.","IOT","Operating Systems","Web Programming","DSP","PHP","Jquery","JavaScript"};
context=this;
lv=(ListView) findViewById(R.id.testListView);
lv.setAdapter(new CustomAdapter(this, prgmNameList,prgmImages));
`AddSubject Activity`
public class AddSubject extends AppCompatActivity {
//Buttons
Button missedMinus;
Button missedPlus;
//ButtonsEnd
//TextViews
TextView safeBunksNumber;
TextView missedLectures;
TextView totalLecturesTextView ;
TextView percentageTextView;
//TextViewsEnd
EditText missedLecturesTextView;
ProgressBar progressBar;
static String s= Integer.toString(10);
public void missedMinusClick(View view)
{
totalLecturesTextView=(TextView)findViewById(R.id.totalLecturesTextView);
int totalLectures= Integer.parseInt(totalLecturesTextView.getText().toString());
missedLecturesTextView=(EditText)findViewById(R.id.missedLecturesTextView);
int missedLectures= Integer.parseInt(missedLecturesTextView.getText().toString());
missedLecturesTextView.setText(Integer.toString(missedLectures-1));
safeBunksNumber=(TextView)findViewById(R.id.safeBunksNumber);
int safeBunksNumber;
}
public void missedPlusClick(View view2)
{
missedLecturesTextView=(EditText)findViewById(R.id.missedLecturesTextView);
int missedLectures= Integer.parseInt(missedLecturesTextView.getText().toString());
missedLecturesTextView.setText(Integer.toString(missedLectures+1));
}
public void submitOnClick1(View view3)
{
EditText subName = (EditText)findViewById(R.id.subName);
EditText totalLecturesTextView = (EditText)findViewById(R.id.totalLecturesTextView);
EditText missedLecturesTextView =(EditText)findViewById(R.id.missedLecturesTextView);
String subjectName = subName.getText().toString();
int totalLectures = Integer.parseInt(totalLecturesTextView.getText().toString());
int missedLectures = Integer.parseInt(missedLecturesTextView.getText().toString());
SharedPreferences sharedPreferences = this.getSharedPreferences("com.example.yashrandive.attendancemanager", Context.MODE_PRIVATE);
sharedPreferences.edit().putString("subName",subjectName);
sharedPreferences.edit().putInt("totalLectures",totalLectures);
sharedPreferences.edit().putInt("missedLectures",missedLectures);
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_subject);
}
}