Beruflich Dokumente
Kultur Dokumente
-17MIS7172
PRODUCT EXAMPLE
Code :
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:id="@+id/Lview"
android:background="#d1d1d1"
android:orientation="vertical">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listview"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
android:dividerHeight="10dp"
android:divider="#d1d1d1"/>
</LinearLayout>
DESIGN :
XML Code (item_product_list.xml) & DESIGN :
}
ProductListAdapter.java
package hello.example.producteg;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.List;
//constructor
@Override
public int getCount() {
return mProductList.size();
}
@Override
public Object getItem(int position) {
return mProductList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{ View v = View.inflate(mContext,R.layout.item_product_list, null);
TextView pname = (TextView)v.findViewById(R.id.p_name);
TextView pprice = (TextView)v.findViewById(R.id.p_price);
TextView pdesc = (TextView)v.findViewById(R.id.p_desc);
pname.setText(mProductList.get(position).getName());
pprice.setText(String.valueOf(mProductList.get(position).getPrice())+"$");
pdesc.setText(mProductList.get(position).getDescription());
v.setTag(mProductList.get(position).getId());
return v;
}
}
MainActivity.java
package hello.example.producteg;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
@Override
protected void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView)findViewById(R.id.listview);
mProductList = new ArrayList<>();
list.setOnItemClickListener(new AdapterView.OnItemClickListener()
{ @Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{ Toast.makeText(getApplicationContext(),"Product Id"+view.getTag(),
Toast.LENGTH_SHORT).show();
}
});
}
}
OUTPUT :