I have a section listview and I have a Row and Header. Header show the month and year whereby Row Shows the profit and stuffs. I have created a dialog box for the Row but whenever i click the Header it prompts me error. How do I make the Header Unclickable ?
My Adapter Class
public class TransactionAdapter extends BaseAdapter {
ArrayList<Object> transactions;
Context c;
LayoutInflater inflater;
static final int ROW = 0;
static final int HEADER = 1;
public TransactionAdapter(Context c, ArrayList<Object> transactions){
this.c = c;
this.transactions = transactions;
}
//Get size of the Transaction ArrayList
@Override
public int getCount() {
return transactions.size();
}
//Get single transaction from the Transaction ArrayList
@Override
public Object getItem(int position) {
return transactions.get(position);
}
//Get Single transaction identifier from the Transaction ArrayList
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemViewType(int position){
//Check the current transaction is Transaction
if(getItem(position) instanceof ProfitTransactions){
return ROW;
}
return HEADER;
}
@Override
public int getViewTypeCount(){
return 2;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
//Type of View which is ROW(0) or HEADER(1)
int type = getItemViewType(position);
//If there is no View create it,
if (convertView == null) {
inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
switch (type) {
case ROW:
convertView = inflater.inflate(R.layout.activity_transaction_items, null);
break;
case HEADER:
convertView = inflater.inflate(R.layout.activity_transaction_header, null);
convertView.setBackgroundColor(Color.rgb(220,220,220));
default:
break;
}
}
switch (type){
case ROW:
ProfitTransactions transaction = (ProfitTransactions)getItem(position);
TextView tvDay = (TextView)convertView.findViewById(R.id.day);
TextView tvTID = (TextView)convertView.findViewById(R.id.tID);
TextView tvTotalPrice = (TextView)convertView.findViewById(R.id.totalPrice);
TextView tvTimeOrder = (TextView)convertView.findViewById(R.id.tvTime);
Log.d("transadapter","-----Test: " + Integer.parseInt(transaction.getDayOfOrder()));
tvDay.setText(transaction.getDayOfOrder()+ getDayNumberSuffix(Integer.parseInt(transaction.getDayOfOrder())));
tvTID.setText("TID: " + transaction.gettId());
tvTotalPrice.setText("+$"+String.format("%.2f", transaction.getTotalPrice()));
tvTimeOrder.setText("At: " + transaction.getTimeOfOrder());
break;
case HEADER:
String header = (String)getItem(position);
// from string to date
SimpleDateFormat inputFormat = new SimpleDateFormat("MM/yyyy");
Date date = null;
try {
date = inputFormat.parse(header);
} catch (ParseException e) {
e.printStackTrace();
}
// from date to string
SimpleDateFormat outputFormat = new SimpleDateFormat("MMM yyyy");
String dateTime = outputFormat.format(date);
TextView tvMonthYear = (TextView)convertView.findViewById(R.id.tvHeaderMonthYear);
tvMonthYear.setText(dateTime);
default:
break;
}
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ProfitTransactions transaction = (ProfitTransactions)getItem(position);
//Create the Dialog Box
AlertDialog.Builder builder = new AlertDialog.Builder(c);
//Put message in the Dialog Box
builder.setMessage("Name: " + transaction.getName() + "\n" +
"Price: " + transaction.getPrice() + "\n" +
"Quantity: " + transaction.getQuantity() + "\n" +
"Total Price: " + transaction.getTotalPrice() + "\n"
)
//If user click Yes
.setPositiveButton("Close", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
//Show the dialog after creating
AlertDialog dialog = builder.show();
}
});
return convertView;
}
}
The expected result I want is I want the Header which contains the Month and Year to be unclickable and will not respond anything.
The same way you are making decisions on what to inflate and how to fill your views in getView
method, you could set the OnClickListener
to null in the desired type. You could use the setClickable
method also.
Inside the getViewMethod use this
case HEADER:
convertView = inflater.inflate(R.layout.activity_transaction_header, null);
convertView.setBackgroundColor(Color.rgb(220,220,220));
convertView.setEnabled(false);
Before the convertView.setOnClickListener, add the if logic to check whether the convertView is a header or a row which is selected! like-
if(!convertView.equals("HEADER"))
{
your onClick listener.....
}
else
{
what you want to do if its headerview being the view selected...
}
Currently you're registering the click listener on every View. You only want to assign it to rows, not headers, because in the handler you are trying to access fields on the ProcessTransaction
which are only present on rows.
So just pull your click listener code into the ROW
branch of your switch.
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
//Type of View which is ROW(0) or HEADER(1)
int type = getItemViewType(position);
//If there is no View create it,
if (convertView == null) {
inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
switch (type) {
case ROW:
convertView = inflater.inflate(R.layout.activity_transaction_items, null);
break;
case HEADER:
convertView = inflater.inflate(R.layout.activity_transaction_header, null);
convertView.setBackgroundColor(Color.rgb(220,220,220));
default:
break;
}
}
switch (type){
case ROW:
ProfitTransactions transaction = (ProfitTransactions)getItem(position);
TextView tvDay = (TextView)convertView.findViewById(R.id.day);
TextView tvTID = (TextView)convertView.findViewById(R.id.tID);
TextView tvTotalPrice = (TextView)convertView.findViewById(R.id.totalPrice);
TextView tvTimeOrder = (TextView)convertView.findViewById(R.id.tvTime);
Log.d("transadapter","-----Test: " + Integer.parseInt(transaction.getDayOfOrder()));
tvDay.setText(transaction.getDayOfOrder()+ getDayNumberSuffix(Integer.parseInt(transaction.getDayOfOrder())));
tvTID.setText("TID: " + transaction.gettId());
tvTotalPrice.setText("+$"+String.format("%.2f", transaction.getTotalPrice()));
tvTimeOrder.setText("At: " + transaction.getTimeOfOrder());
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ProfitTransactions transaction = (ProfitTransactions)getItem(position);
//Create the Dialog Box
AlertDialog.Builder builder = new AlertDialog.Builder(c);
//Put message in the Dialog Box
builder.setMessage("Name: " + transaction.getName() + "\n" +
"Price: " + transaction.getPrice() + "\n" +
"Quantity: " + transaction.getQuantity() + "\n" +
"Total Price: " + transaction.getTotalPrice() + "\n"
)
//If user click Yes
.setPositiveButton("Close", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
//Show the dialog after creating
AlertDialog dialog = builder.show();
}
});
break;
case HEADER:
String header = (String)getItem(position);
// from string to date
SimpleDateFormat inputFormat = new SimpleDateFormat("MM/yyyy");
Date date = null;
try {
date = inputFormat.parse(header);
} catch (ParseException e) {
e.printStackTrace();
}
// from date to string
SimpleDateFormat outputFormat = new SimpleDateFormat("MMM yyyy");
String dateTime = outputFormat.format(date);
TextView tvMonthYear = (TextView)convertView.findViewById(R.id.tvHeaderMonthYear);
tvMonthYear.setText(dateTime);
default:
break;
}
return convertView;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.