Code Snippet for android developers
I'm use Android Studio IDE for code android applicationsDownload full source code basic listview and customize listview from here
1. Basic ListView:
Source code:
package com.example.vilh.demobasiclistview;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
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.Toast;
import java.util.ArrayList;
import java.util.List;
public class BasicLView extends AppCompatActivity {
Button btnClicked;
ListView lstView;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_basic_lview);
lstView = (ListView)findViewById(R.id.listView);
btnClicked = (Button)findViewById(R.id.buttonClick);
btnClicked.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
Intent main = new Intent(BasicLView.this, MainActivity.class);
startActivity(main);
}
});
final ArrayList<String> arMonHoc = new ArrayList<String>();
arMonHoc.add("Game Phong Thần");
arMonHoc.add("Game Võ Lâm 1");
arMonHoc.add("Game Kiếm Thế");
arMonHoc.add("Game Võ Lâm 2");
arMonHoc.add("Game Thiên Long");
arMonHoc.add("Game Hoa Thiên Cốt");
ArrayAdapter adapter = new ArrayAdapter(
BasicLView.this,
android.R.layout.simple_list_item_1,
arMonHoc
);
lstView.setAdapter(adapter);
lstView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(
getApplicationContext(),
arMonHoc.get(position),
Toast.LENGTH_SHORT ).show();
}
});
}
}
2. Custom ListView
Use basic ListView:
Drag 2 pictures to drawable folder
Choose Proejcts before do that!
Create activity_dong_food.xml (Interface)
Source code:
clsFood.java
package com.example.vilh.democustomelistview; /** *
Created by Zidane-huuvi168@gmail.com on 4/19/2016.
*/public class clsFood { public String Name; public Integer Money; public Integer Position; public clsFood(String name, Integer m, Integer pos) { Name = name; Money = m; Position = pos; /* position of image */ } }
ListAdapter.java
package com.example.vilh.
democustomelistview
; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.ImageView; import android.widget.TextView; import java.util.List; public class ListAdapter extends ArrayAdapter<clsFood> { public ListAdapter(Context context, int textViewResourceId) { super(context, textViewResourceId); } // context màn hinh đang đứng // resource = layout đang đứng public ListAdapter(Context context, int resource, List<clsFood> items) { super(context, resource, items); } @Override public View getView(int position, View convertView, ViewGroup parent) { View v = convertView; if (v == null) { LayoutInflater vi; vi = LayoutInflater.from(getContext()); v = vi.inflate(R.layout.activity_dong_food, null); } clsFood p = getItem(position); if (p != null) { // Anh xa + Gan gia tri TextView tt1 = (TextView) v.findViewById(R.id.textViewNameCourses); tt1.setText(p.Name); TextView tt2 = (TextView) v.findViewById(R.id.textViewYear); tt2.setText(String.valueOf(p.Money)); ImageView im1 = (ImageView)v.findViewById(R.id.imageViewDescription); im1.setImageResource(p.Position); } // giao diện từng dòng return v; } }
package com.example.vilh.happyfull; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ListView; import java.util.ArrayList; public class CustomLView extends AppCompatActivity { Button btnBacked; ListView lstCourses; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_custom_lview); lstCourses = (ListView)findViewById(R.id.listViewCourses); btnBacked = (Button)findViewById(R.id.buttonBack); btnBacked.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent main = new Intent(CustomLView.this, MainActivity.class); startActivity(main); } }); ArrayList<clsFood> arFood = new ArrayList<clsFood>();
arFood
.add(new clsFood("Cơm Xào", 20000, R.drawable.ma1));
arFood
.add(new clsFood("Nước Mía", 5000, R.drawable.ma2));
arFood
.add(new clsFood("Hủ Tiếu Mì", 25000, R.drawable.ma1)); arFood.add(new clsFood("Bún Huế", 30000, R.drawable.ma1)); arFood.add(new clsFood("Phở Hùng", 35000, R.drawable.ma1)); arFood.add(new clsFood("Combo Cơm + nước", 38000, R.drawable.ma2)); ListAdapter adapter = new ListAdapter( CustomLView.this, R.layout.activity_dong_food, // layout tu build arFood ); lstCourses.setAdapter(adapter); } }