我是android编程的新手。我创建了一个应用程序,显示从放置在助手中的CSV文件中读取的项目列表。现在,当我单击列表视图中的项目时,将为该特定时间设置警报。此外,现在我想用颜色突出显示列表视图中的项目,直到闹钟响起。一旦用户取消警报,列表视图中突出显示的项目必须转为原始状态。
我的查询:
我在这里发布我的完整代码。有人在那里帮助我,因为这是我的应用程序发布的唯一待决事项。
显示列表视图:
public class mrvtoparanur extends Activity {
int hours,mins,day,month,year,pos;
long time;
boolean state ;
Activity s;
CSVAdapter mAdapter;
final static int RQS_1=1;
Calendar cal = Calendar.getInstance();
Calendar calset = (Calendar)cal.clone();
static AlarmManager alarmManager;
static PendingIntent pendingIntent;
int selectedPos;
boolean a;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mrvtoparanur);
final ListView mList = (ListView)findViewById(R.id.mrvtoparanurlist);
mAdapter=new CSVAdapter(this,-1);
mList.setAdapter(mAdapter);
mList.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
clock clicked=mAdapter.getItem(arg2);
String [] res = clicked.getTime().split(":");
hours=Integer.parseInt(res[0]);
mins=Integer.parseInt(res[1]);
pos=mAdapter.getPosition(clicked);
mList.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
TextView tx=(TextView)arg1.findViewById(R.id.listtxtview);
if(mList.isItemChecked(arg2))
tx.setTextColor(Color.RED);
else
tx.setTextColor(Color.BLACK);
Toast.makeText(getApplicationContext(), "You selected time :"+hours+"hours and "+mins+"mins", Toast.LENGTH_SHORT).show();
ScheduleAlarm();
}});
}
protected void ScheduleAlarm() {
// TODO Auto-generated method stub
calset.set(Calendar.HOUR_OF_DAY, hours);
calset.set(Calendar.MINUTE, mins);
calset.set(Calendar.SECOND, 0);
Long time = calset.getTimeInMillis();
Intent intentAlarm = new Intent(this, AlarmReciever.class);
pendingIntent = PendingIntent.getBroadcast(this, 1, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, time, 1000, pendingIntent);
Toast.makeText(this, "Reminder Set", Toast.LENGTH_SHORT).show();
stopService(new Intent(mrvtoparanur.this,AlarmReciever.class));
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event){
//Changes 'back' button action
if(keyCode==KeyEvent.KEYCODE_BACK)
{
//Include the code here
Intent i=new Intent(this,MainActivity.class);
startActivity(i);
}
return true;
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
阅读资产文件夹中的CSV文件
public class CSVAdapter extends ArrayAdapter<clock>{
Context ctx;
View v;
String line;
public CSVAdapter(Context context,int textViewResourceId)
{
super (context,textViewResourceId);
this.ctx = context;
loadArrayFromFile();
}
@Override
public View getView(final int pos,View convertView,final ViewGroup parent){
TextView mView=(TextView)convertView;
if(null == mView){
mView = new TextView(parent.getContext());
mView.setTextSize(28);
}
mView.setText(getItem(pos).getTime());
return mView;
}
private void loadArrayFromFile(){
try
{
InputStream is=ctx.getAssets().open("mrvtoparanur.csv");
BufferedReader reader=new BufferedReader(new InputStreamReader(is));
//Read each line
while((line = reader.readLine())!=null){
clock cur = new clock();
cur.setTime(line);
this.add(cur);
}
}catch(IOException e)
{
e.printStackTrace();
}
}
}
这是我的列表视图的xml文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@android:color/white" >
<ListView
android:id="@+id/mrvtoparanurlist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:choiceMode="singleChoice"
android:textFilterEnabled="true"
android:duplicateParentState="true">
</ListView>
</LinearLayout>
P.S:我希望这个应用程序从API LEVEL 8工作到当前可用的API LEVEL。所以请为我提供一个通用的解决方案。我也经历了很多类似问题的论坛,但我发现我与其他人的不同之处主要在于使用CSV适配器读取CSV文件。