Widget - Listmap Programatically

ListView listview1 = new ListView(this);

LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
listview1.setLayoutParams(lp1);
linear1.addViee(listview1);
//if in drwaer menu
//_drawer_linear1.addView(listview1);

//if List String Programatically
list.add("aan");
listview1.setAdapter(new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_list_item_1, list));

//if List Map
{
HashMap<String, Object> _item = new HashMap<>();
_item.put("key", "Item 1");
list.add(_item);
}
listview1.setAdapter(new Listview1Adapter(list));
//But you must Have Custom adapter

//Click
listview1.setOnItemClickListener( new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView _parent, View _view, final int _position, long _id) {
switch (_position){
case 0:
showMessage("item 0");
break;
case 1:
showMessage("item 1");
break;
}
}});

//Long Click
listview1.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> _param1, View _param2, int _param3, long _param4) {
final int _position = _param3;
switch (_position){
case 0:
showMessage("item 0");
break;
case 1:
showMessage("item 1");
break;
}
return true;
}
});



//Custom Adapter For List Map
public class Listview1Adapter extends BaseAdapter {
ArrayList<HashMap<String, Object>> _data;
public Listview1Adapter(ArrayList<HashMap<String, Object>> _arr) {
_data = _arr;
}

@Override
public int getCount() {
return _data.size();
}

@Override
public HashMap<String, Object> getItem(int _index) {
return _data.get(_index);
}

@Override
public long getItemId(int _index) {
return _index;
}
@Override
public View getView(final int _position, View _view, ViewGroup _viewGroup) {
LayoutInflater _inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View _v = _view;
if (_v == null) {
_v = _inflater.inflate(R.layout.aaa, null);
}

final LinearLayout linear1 = (LinearLayout) _v.findViewById(R.id.linear1);
final ImageView imageview1 = (ImageView) _v.findViewById(R.id.imageview1);
final TextView textview1 = (TextView) _v.findViewById(R.id.textview1);
textview1.setText("aan");
return _v;
}
}


//Use this For List Map (100% Programatically)
private ArrayList<HashMap<String, Object>> list = new ArrayList<>();

//Use this For List String (100% Programatically)
private ArrayList<String> list = new ArrayList<>();

//Code By Gabriel Gymkhana @Sketchware

Komentar

Cara pembuatan Apps paling Populer

Create Stopwatch App in Android using Sketchware

How to enable download in webview in Sketchware apps?

TextInputLayout in Sketchware

Create Stopwatch App in Android using Sketchware

How to integrate Admob Ads in Sketchware project using AIDE?

How to find and​ highlight a word in a text field in Sketchware?

Code for implementing Notifications in Sketchware

A Flash Light App in Sketchware

Create a Stopwatch App using Chronometer in Sketchware

Create app to save all passwords, protected using voice key