Toshkent axborot texnologiyalari universiteti dasturiy vositalar xavfsizligi


-qadam: MainActivity.java fayli bilan ishlash


Download 0.5 Mb.
bet4/5
Sana12.03.2023
Hajmi0.5 Mb.
#1264145
1   2   3   4   5
Bog'liq
Jamoaviy ish 011-18 Rahimov M

5-qadam: MainActivity.java fayli bilan ishlash

Endi MainActivity.java faylidagi barcha narsalarni sozlang. NoteEditorActivity.java kodiga qo'ng'iroq qiling, java-ga barcha xml-ni biriktiring va dasturni ishga tushiring. Quyida MainActivity.java faylining toʻliq kodi keltirilgan. Kodni batafsilroq tushunish uchun sharhlar kod ichiga qo'shiladi.




Java
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.HashSet;


public class MainActivity extends AppCompatActivity {


static ArrayList notes = new ArrayList<>();
static ArrayAdapter arrayAdapter;


@Override
public boolean onCreateOptionsMenu(Menu menu) {


MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.add_note_menu, menu);


return super.onCreateOptionsMenu(menu);
}


@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
super.onOptionsItemSelected(item);


if (item.getItemId() == R.id.add_note) {


// Going from MainActivity to NotesEditorActivity
Intent intent = new Intent(getApplicationContext(), NoteEditorActivity.class);
startActivity(intent);
return true;
}


return false;
}


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


ListView listView = findViewById(R.id.listView);
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("com.example.notes", Context.MODE_PRIVATE);
HashSet set = (HashSet) sharedPreferences.getStringSet("notes", null);


if (set == null) {


notes.add("Example note");
} else {
notes = new ArrayList(set);
}


// Using custom listView Provided by Android Studio
arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_expandable_list_item_1, notes);


listView.setAdapter(arrayAdapter);


listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView adapterView, View view, int i, long l) {


// Going from MainActivity to NotesEditorActivity
Intent intent = new Intent(getApplicationContext(), NoteEditorActivity.class);
intent.putExtra("noteId", i);
startActivity(intent);


}
});


listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView adapterView, View view, int i, long l) {



Download 0.5 Mb.

Do'stlaringiz bilan baham:
1   2   3   4   5




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling