2
私はデータベースからプル、ローディング、移送などの値を持つアプリケーションを開発しており、ユーザーはそれに新しいカテゴリを追加することもできます。グリッドから特定のアイテムを選択すると、リストビューに表示されるはずです(プルキンシングを選択すると、コンテンツがタイトル、ツリーおよびその値として表示されるはずです)。androidのlistviewで選択したグリッドアイテムの値を表示するには
主な活動:
public class GridActivity extends AppCompatActivity{
GridView gv;
ListView listview;
ArrayList<CustomizedCharge>listcharges;
AlertDialog alert;
String totalamount;
LinearLayout eview;
private List<CustomizedCharge> listactivitycharge;
FetchlistData listadapter;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.otherchargesscreen);
gv=(GridView) findViewById(R.id.gridview1);
listview=(ListView) findViewById(R.id.list_view);
eview=(LinearLayout)findViewById(R.id.empty);
DatabaseHandlerOtherchgs databaseHandlerOtherchgs=new DatabaseHandlerOtherchgs(getApplicationContext());
ArrayList<OtherChargesType> listet = new ArrayList<OtherChargesType>();
Resources res = getResources();
Drawable drawable = res.getDrawable(R.drawable.p);
Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] bitMapData = stream.toByteArray();
Drawable loading = res.getDrawable(R.drawable.l);
Bitmap bitmaploading = ((BitmapDrawable)loading).getBitmap();
ByteArrayOutputStream streamloading = new ByteArrayOutputStream();
bitmaploading.compress(Bitmap.CompressFormat.PNG, 100, streamloading);
byte[] bitMaploading = streamloading.toByteArray();
Drawable addnew = res.getDrawable(R.mipmap.add);
Bitmap bitmapaddnew = ((BitmapDrawable)addnew).getBitmap();
ByteArrayOutputStream streamadd = new ByteArrayOutputStream();
bitmapaddnew.compress(Bitmap.CompressFormat.PNG, 100, streamadd);
byte[] bitMapaddnew = streamadd.toByteArray();
listet=databaseHandlerOtherchgs.getAllproducttitle();
listet.add(new OtherChargesType("Plucking",bitMapData));
listet.add(new OtherChargesType("Loading",bitMaploading));
listet.add(new OtherChargesType("Add New",bitMapaddnew));
final OtherChargesGridAdapter adapter = new OtherChargesGridAdapter(GridActivity.this, listet);
gv.setAdapter(adapter);
listcharges=new ArrayList<CustomizedCharge>();
listcharges.add(new CustomizedCharge(bitMapData, "Loading", "No of Loads", "0", "Cost per Load", "0", "0"));
listadapter=new FetchlistData(GridActivity.this,listcharges);
if(listadapter!=null) {
if (listadapter.getCount() > 0) {
eview.setVisibility(View.INVISIBLE);
Intent i = getIntent();
String textreceived = i.getStringExtra("Selectedname");
if (textreceived.startsWith("Plucking")) {
listcharges.add(new CustomizedCharge(bitMapData, "Loading", "No of Loads", "0", "Cost per Load", "0", "0"));
listadapter=new FetchlistData(GridActivity.this,listcharges);
} else if (textreceived.startsWith("Loading")) {
listcharges.add(new CustomizedCharge(bitMapData, "Loading", "No of Loads", "0", "Cost per Load", "0", "0"));
} else {
listcharges= databaseHandlerOtherchgs.getAllLabels();
}
listview.setAdapter(listadapter);
}else{
listview.setEmptyView(eview);
}
}
}
Grid Adapter:
public class OtherChargesGridAdapter extends BaseAdapter {
Context context;
String totalamount;
AlertDialog alert;
private Activity activity;
byte[] bitMapData;
private LayoutInflater inflater;
private List<OtherChargesType> otherchargestypes;
private List<CustomizedCharge> listactivitycharge;
public OtherChargesGridAdapter(Activity activity, List<OtherChargesType> otherchargestypes) {
this.activity = activity;
this.otherchargestypes = otherchargestypes;
}
@Override
public int getCount() {
return otherchargestypes.size();
}
@Override
public Object getItem(int i) {
return otherchargestypes.get(i);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertview, ViewGroup parent) {
if (inflater == null) {
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
if (convertview == null) {
convertview = inflater.inflate(R.layout.otherchargesgriditem, null);
}
OtherChargesType m = otherchargestypes.get(position);
byte[] outImage = m.getImage();
ByteArrayInputStream imageStream = new ByteArrayInputStream(outImage);
final Bitmap theImage = BitmapFactory.decodeStream(imageStream);
ImageView otherimages = (ImageView) convertview.findViewById(R.id.imageView1);
final TextView typename = (TextView) convertview.findViewById(R.id.textView1);
// getting movie data for the row
typename.setText(m.getTypename());
otherimages.setImageBitmap(theImage);
convertview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (typename.getText().toString().startsWith("Add New")) {
final LayoutInflater layoutInflater = LayoutInflater.from(activity);
final View promptView = layoutInflater.inflate(R.layout.activity_addnewcharge, null);
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(activity);
alertDialogBuilder.setView(promptView);
alertDialogBuilder.setCancelable(true);
final TextInputLayout txipagetitle = (TextInputLayout) promptView.findViewById(R.id.inputpagetitle);
final TextInputLayout txiquantitytitle = (TextInputLayout) promptView.findViewById(R.id.inputqunatitytitle);
final TextInputLayout txiquantity = (TextInputLayout) promptView.findViewById(R.id.inputquantity);
final TextInputLayout txiratetitle = (TextInputLayout) promptView.findViewById(R.id.inputratetitle);
final TextInputLayout txirate = (TextInputLayout) promptView.findViewById(R.id.inputrate);
final EditText edtpagetitle = (EditText) promptView.findViewById(R.id.edtpagetitle);
final EditText edtquantitytitle = (EditText) promptView.findViewById(R.id.edtquantitytitle);
final EditText edtquantity = (EditText) promptView.findViewById(R.id.edtquantity);
final EditText edtratetitle = (EditText) promptView.findViewById(R.id.edtratetitle);
final EditText edtrate = (EditText) promptView.findViewById(R.id.edtrate);
final TextView tvtotal = (TextView) promptView.findViewById(R.id.tvtotalamount);
final ImageView savedata = (ImageView) promptView.findViewById(R.id.btn_save);
edtquantity.setText("0.0");
edtrate.setText("0.0");
double one = Double.parseDouble(edtquantity.getText().toString());
double two = Double.parseDouble(edtrate.getText().toString());
double three = (one * two);
totalamount = String.valueOf(three);
tvtotal.setText(totalamount);
txipagetitle.setError(null);
txiquantitytitle.setError(null);
txiquantity.setError(null);
txiratetitle.setError(null);
txirate.setError(null);
edtrate.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
double one = Double.parseDouble(edtquantity.getText().toString());
double two = Double.parseDouble(edtrate.getText().toString());
double three = (one * two);
totalamount = String.valueOf(three);
tvtotal.setText(totalamount);
return false;
}
});
savedata.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DatabaseHandlerOtherchgs databaseHandlerOtherchgs = new DatabaseHandlerOtherchgs(activity);
if (edtpagetitle.getText().toString().isEmpty() & edtquantitytitle.getText().toString().isEmpty() & edtquantity.getText().toString().isEmpty() & edtratetitle.getText().toString().isEmpty() & edtrate.getText().toString().isEmpty()) {
txipagetitle.setError("Enter Title");
txiquantitytitle.setError("Enter Quantity Title");
txiquantity.setError("Enter Quantity");
txiratetitle.setError("Enter Rate Title");
txirate.setError("Enter Rate");
} else {
txipagetitle.setError(null);
txiquantitytitle.setError(null);
txiquantity.setError(null);
txiratetitle.setError(null);
txirate.setError(null);
String pagetitle = edtpagetitle.getText().toString();
String quantitytitle = edtquantitytitle.getText().toString();
String quantity = edtquantity.getText().toString();
String ratetitle = edtratetitle.getText().toString();
String rate = edtrate.getText().toString();
String tot = tvtotal.getText().toString();
if (edtpagetitle.getText().toString().startsWith("A") | edtpagetitle.getText().toString().startsWith("a")) {
Resources res = activity.getResources();
Drawable drawable = res.getDrawable(R.drawable.a);
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
bitMapData = stream.toByteArray();
} else if (edtpagetitle.getText().toString().startsWith("B") | edtpagetitle.getText().toString().startsWith("b")) {
Resources res = activity.getResources();
Drawable drawable = res.getDrawable(R.drawable.b);
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
bitMapData = stream.toByteArray();
} else if (edtpagetitle.getText().toString().startsWith("C") | edtpagetitle.getText().toString().startsWith("c")) {
Resources res = activity.getResources();
Drawable drawable = res.getDrawable(R.drawable.c);
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
bitMapData = stream.toByteArray();
} else if (edtpagetitle.getText().toString().startsWith("D") | edtpagetitle.getText().toString().startsWith("d")) {
Resources res = activity.getResources();
Drawable drawable = res.getDrawable(R.drawable.d);
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
bitMapData = stream.toByteArray();
} else if (edtpagetitle.getText().toString().startsWith("E") | edtpagetitle.getText().toString().startsWith("e")) {
Resources res = activity.getResources();
Drawable drawable = res.getDrawable(R.drawable.e);
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
bitMapData = stream.toByteArray();
} else if (edtpagetitle.getText().toString().startsWith("F") | edtpagetitle.getText().toString().startsWith("f")) {
Resources res = activity.getResources();
Drawable drawable = res.getDrawable(R.drawable.f);
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
bitMapData = stream.toByteArray();
} else if (edtpagetitle.getText().toString().startsWith("G") | edtpagetitle.getText().toString().startsWith("g")) {
Resources res = activity.getResources();
Drawable drawable = res.getDrawable(R.drawable.g);
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
bitMapData = stream.toByteArray();
} else if (edtpagetitle.getText().toString().startsWith("H") | edtpagetitle.getText().toString().startsWith("h")) {
Resources res = activity.getResources();
Drawable drawable = res.getDrawable(R.drawable.h);
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
bitMapData = stream.toByteArray();
} else if (edtpagetitle.getText().toString().startsWith("I") | edtpagetitle.getText().toString().startsWith("i")) {
Resources res = activity.getResources();
Drawable drawable = res.getDrawable(R.drawable.i);
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
bitMapData = stream.toByteArray();
} else if (edtpagetitle.getText().toString().startsWith("J") | edtpagetitle.getText().toString().startsWith("j")) {
Resources res = activity.getResources();
Drawable drawable = res.getDrawable(R.drawable.j);
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
bitMapData = stream.toByteArray();
} else if (edtpagetitle.getText().toString().startsWith("K") | edtpagetitle.getText().toString().startsWith("k")) {
Resources res = activity.getResources();
Drawable drawable = res.getDrawable(R.drawable.k);
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
bitMapData = stream.toByteArray();
} else if (edtpagetitle.getText().toString().startsWith("L") | edtpagetitle.getText().toString().startsWith("l")) {
Resources res = activity.getResources();
Drawable drawable = res.getDrawable(R.drawable.l);
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
bitMapData = stream.toByteArray();
} else if (edtpagetitle.getText().toString().startsWith("M") | edtpagetitle.getText().toString().startsWith("m")) {
Resources res = activity.getResources();
Drawable drawable = res.getDrawable(R.drawable.m);
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
bitMapData = stream.toByteArray();
} else if (edtpagetitle.getText().toString().startsWith("N") | edtpagetitle.getText().toString().startsWith("n")) {
Resources res = activity.getResources();
Drawable drawable = res.getDrawable(R.drawable.n);
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
bitMapData = stream.toByteArray();
} else if (edtpagetitle.getText().toString().startsWith("O") | edtpagetitle.getText().toString().startsWith("o")) {
Resources res = activity.getResources();
Drawable drawable = res.getDrawable(R.drawable.o);
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
bitMapData = stream.toByteArray();
} else if (edtpagetitle.getText().toString().startsWith("P") | edtpagetitle.getText().toString().startsWith("p")) {
Resources res = activity.getResources();
Drawable drawable = res.getDrawable(R.drawable.p);
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
bitMapData = stream.toByteArray();
} else if (edtpagetitle.getText().toString().startsWith("Q") | edtpagetitle.getText().toString().startsWith("q")) {
Resources res = activity.getResources();
Drawable drawable = res.getDrawable(R.drawable.q);
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
bitMapData = stream.toByteArray();
} else if (edtpagetitle.getText().toString().startsWith("R") | edtpagetitle.getText().toString().startsWith("r")) {
Resources res = activity.getResources();
Drawable drawable = res.getDrawable(R.drawable.r);
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
bitMapData = stream.toByteArray();
} else if (edtpagetitle.getText().toString().startsWith("S") | edtpagetitle.getText().toString().startsWith("s")) {
Resources res = activity.getResources();
Drawable drawable = res.getDrawable(R.drawable.s);
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
bitMapData = stream.toByteArray();
} else if (edtpagetitle.getText().toString().startsWith("T") | edtpagetitle.getText().toString().startsWith("t")) {
Resources res = activity.getResources();
Drawable drawable = res.getDrawable(R.drawable.t);
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
bitMapData = stream.toByteArray();
} else if (edtpagetitle.getText().toString().startsWith("U") | edtpagetitle.getText().toString().startsWith("u")) {
Resources res = activity.getResources();
Drawable drawable = res.getDrawable(R.drawable.u);
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
bitMapData = stream.toByteArray();
} else if (edtpagetitle.getText().toString().startsWith("V") | edtpagetitle.getText().toString().startsWith("v")) {
Resources res = activity.getResources();
Drawable drawable = res.getDrawable(R.drawable.v);
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
bitMapData = stream.toByteArray();
} else if (edtpagetitle.getText().toString().startsWith("W") | edtpagetitle.getText().toString().startsWith("w")) {
Resources res = activity.getResources();
Drawable drawable = res.getDrawable(R.drawable.w);
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
bitMapData = stream.toByteArray();
} else if (edtpagetitle.getText().toString().startsWith("X") | edtpagetitle.getText().toString().startsWith("x")) {
Resources res = activity.getResources();
Drawable drawable = res.getDrawable(R.drawable.x);
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
bitMapData = stream.toByteArray();
} else if (edtpagetitle.getText().toString().startsWith("Y") | edtpagetitle.getText().toString().startsWith("y")) {
Resources res = activity.getResources();
Drawable drawable = res.getDrawable(R.drawable.y);
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
bitMapData = stream.toByteArray();
} else if (edtpagetitle.getText().toString().startsWith("Z") | edtpagetitle.getText().toString().startsWith("z")) {
Resources res = activity.getResources();
Drawable drawable = res.getDrawable(R.drawable.z);
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
bitMapData = stream.toByteArray();
}
databaseHandlerOtherchgs.insertLabel(bitMapData, pagetitle, quantitytitle, quantity, ratetitle, rate, tot);
alert.dismiss();
Intent k=activity.getIntent();
activity.startActivity(k);
}
}
});
alert = alertDialogBuilder.create();
alert.show();
}else if (typename.getText().toString().startsWith("Plucking")) {
Intent i= new Intent(activity,GridActivity.class);
i.putExtra("Selectedname","Plucking");
activity.startActivity(i);
} else if (typename.getText().toString().startsWith("Loading")) {
Intent i= new Intent(activity,GridActivity.class);
i.putExtra("Selectedname","Loading");
activity.startActivity(i);
}else {
String s=typename.getText().toString();
Intent i= new Intent(activity,GridActivity.class);
i.putExtra("Selectedname",s);
activity.startActivity(i);
}
}
});
return convertview;
}
}