Asqaraliyev Islomjon 111 20-g.
3-modul.
Mavzu: Musiqa asboblari onlayn do’koni.s
MainActivity.java
package com.example.otm;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity { ListView listView;
String[] title={"Pianino","Ud","Fleyta","Doira","Skripka","Udarnik","Ud"};
String[] subTitle={"pianino.com","ud.com","fleyta.com","doira.com","skripka.com","udarnik.com","ud.com"};
Integer[] imageId={R.drawable.pianino,R.drawable.ud,R.drawable.fleyta,R.drawable.doira,R.drawable.skripka,
R.drawable.udarnik,R.drawable.ud};
String[] urls={"https://www.dell.com/ru-rs?c=uz&l=en&s=,gen&~ck=cr&redirect=1", "https://www.apple.com/","https://www.samsung.com/uz_ru/","https://www.hp.com/kz-ru/home.html","https://www.acer.com/ru-ru","https://www.dell.com/ru-rs?c=uz&l=en&s=,gen&~ck=cr&redirect=1","https://www.asus.com/ru/","https://www.lenovo.com/ru/ru/"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView=findViewById(R.id.listView_id);
com.example.otm.MyListAdapter adapter = new com.example.otm.MyListAdapter(MainActivity.this, title,subTitle,imageId,urls);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView> parent, View view, int position, long id) {
// Toast.makeText(MainActivity.this, urls[position],Toast.LENGTH_LONG).show();
Intent openLinksIntent = new Intent(Intent.ACTION_VIEW,Uri.parse(urls[position]));
startActivity(openLinksIntent);
}
});
}
}
MyListAdapter.java
package com.example.otm;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class MyListAdapter extends ArrayAdapter{
Activity context;
String[] title;
String[] subtitle;
Integer[] imageId;
String[] urls;
public MyListAdapter(Activity context, String[] title, String[] subtitle, Integer[] imageId,String[] urls) {
super(context,R.layout.custom_list_layout,title);
this.urls=urls;
this.context = context;
this.title = title;
this.subtitle = subtitle;
this.imageId = imageId;
}
public View getView(int position, View view, ViewGroup parent){
LayoutInflater inflater=context.getLayoutInflater();
@SuppressLint("ViewHolder") View rootView=inflater.inflate(R.layout.custom_list_layout, null,true);
TextView titleTxt=rootView.findViewById(R.id.title_id);
TextView subtitleTxt=rootView.findViewById(R.id.subtitle_id);
ImageView imageView=rootView.findViewById(R.id.imag_id);
Log.e("Tag","getView:" +title[position]);
titleTxt.setText(title[position].toString());
subtitleTxt.setText(subtitle[position]);
imageView.setImageResource(imageId[position]);
return rootView;
}
public String[] getSubtitle() {
return subtitle;
}
}
custom.list_layout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="@color/purple_200">
android:id="@+id/imag_id"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="5dp"
android:elevation="5dp"
android:contentDescription="@string/todo" />
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
android:id="@+id/title_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/title"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="@color/black"
android:layout_margin="5dp"
android:textAppearance="?android:attr/textAppearanceMedium"/>
android:id="@+id/subtitle_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/subtitle"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="@color/purple_200"
android:layout_margin="5dp"/>
Activy_main.xml
?xml version="1.0" encoding="utf-8"?>
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/teal_700"
android:gravity="center"
tools:context=".MainActivity">
android:id="@+id/listView_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#000"
android:dividerHeight="2dp" />
Do'stlaringiz bilan baham: |