part 27 : String Array, ArrayList-String, List view, Array Adapter, String To Array ( String split), Extract Specific Character From String ( String replace ) .

MainActivity.java 

package first.learn.listviewproject;

import static first.learn.listviewproject.Common.showStaticCustomDialog;
import static first.learn.listviewproject.MainActivity2.Message;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {


Button btnOne, btnTwo;
TextView tvOne;
ListView listView, listView2;
/*
Activity Starting :
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

setTitle("Screen One ");



listView = (ListView)findViewById(R.id.listView);
listView2 = (ListView)findViewById(R.id.listView2);




ArrayList<String> list = new ArrayList<>();
list.add("a");
list.add("b");
list.add("c");
list.add("d");

if(list.size()>4) {
System.out.println("Element List : " + list.get(4));
}else {

// Again Network Call :
System.out.println("Element List : " + list.get(3));
}





String [] array = {"apple", "Banana", "Cucumber", "Dragon"};

if(array.length>4) {
System.out.println("Element : Array : " + array[4]);
}else {
System.out.println("Element : Array : " + "NetWork Call Again");
}



/// String To Array :
String s = "a#b#whatever#?value#ddddd#lksajdflakjsdf#kasd;lfa#";
String [] arb = s.split("#");


ArrayAdapter<String> adapter = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_list_item_1, android.R.id.text1, arb );
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

String a = arb[position];

new AlertDialog.Builder(MainActivity.this)
.setTitle("You Clicked")
.setMessage("Position : "+ position + " Item : "+ a)
.setPositiveButton("ok", null)
.create().show();

}
});



ArrayAdapter<String> adapter2 = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_list_item_1, android.R.id.text1, list );
listView2.setAdapter(adapter2);

listView2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

String a = list.get(position);

new AlertDialog.Builder(MainActivity.this)
.setTitle("You Clicked")
.setMessage("Position : "+ position + " Item : "+ a)
.setPositiveButton("ok", null)
.create().show();

}
});


//////////////////////////////


String [] ar = s.split("\\?");
System.out.println("Array Length: "+ ar.length);
System.out.println("Array : "+ ar[1]);




// Extract Specific Character From String :
String t = "noor@gmail.com";
String [] tx = t.split("@");
String found = tx[0];

System.out.println("Name : "+ found);

String hostProvider = tx[1].replace(".com", "");
System.out.println("Name Host : "+ hostProvider);


}


}


activity_main.xml 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
android:layout_margin="16dp">




<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>




<ListView
android:layout_marginTop="20dp"
android:id="@+id/listView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>



</LinearLayout>
















No comments:

Post a Comment